o
    Ďi                     @  st   d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ G d	d
 d
e	ZdS )    )annotations)AnyN)logger)DetectionModel)ObjectPrediction)fix_full_shape_listfix_shift_amount_list)check_package_minimum_versionc                      sr   e Zd Z fddZdd ZdddZdddZedd Zedd Z	edd Z
ddggdfdddZ  ZS )Yolov5DetectionModelc                   s<   t | dd pg }g t|dd| _t j|i | d S )Nrequired_packagesyolov5torch)getattrlistr   super__init__)selfargskwargsexisting_packages	__class__ N/home/jeff/fluffinator/venv/lib/python3.10/site-packages/sahi/models/yolov5.pyr      s   zYolov5DetectionModel.__init__c              
   C  sN   ddl }z|j| j| jd}| | W dS  ty& } ztd|d}~ww )z5Detection model is initialized and set to self.model.r   N)devicez-model_path is not a valid yolov5 model path: )r   load
model_pathr   	set_model	Exception	TypeError)r   r   modeler   r   r   
load_model   s   
zYolov5DetectionModel.load_modelr    r   c                 C  sT   |j jdvrtdt| | j|_|| _| js(dd t| j	D }|| _dS dS )zoSets the underlying YOLOv5 model.

        Args:
            model: Any
                A YOLOv5 model
        )zyolov5.models.commonzmodels.commonzNot a yolov5 model: c                 S  s   i | ]	\}}t ||qS r   )str).0indcategory_namer   r   r   
<dictcomp>.   s    z2Yolov5DetectionModel.set_model.<locals>.<dictcomp>N)
r   
__module__r   typeconfidence_thresholdconfr    category_mapping	enumeratecategory_names)r   r    r,   r   r   r   r      s   
zYolov5DetectionModel.set_modelimage
np.ndarrayc                 C  sB   | j du r	td| jdur| j || jd}n|  |}|| _dS )a  Prediction is performed using self.model and the prediction result is set to self._original_predictions.

        Args:
            image: np.ndarray
                A numpy array that contains the image to be predicted. 3 channel image should be in RGB order.
        Nz5Model is not loaded, load it by calling .load_model())size)r    
ValueError
image_size_original_predictions)r   r/   prediction_resultr   r   r   perform_inference1   s   
	


z&Yolov5DetectionModel.perform_inferencec                 C  s   t | jjS )zReturns number of categories.)lenr    namesr   r   r   r   num_categoriesC   s   z#Yolov5DetectionModel.num_categoriesc                 C  s   dS )z3Returns if model output contains segmentation mask.Fr   r9   r   r   r   has_maskH   s   zYolov5DetectionModel.has_maskc                 C  s"   t ddrt| jj S | jjS )Nr   z6.2.0)r	   r   r    r8   valuesr9   r   r   r   r.   N   s   
z#Yolov5DetectionModel.category_namesr   Nshift_amount_listlist[list[int]] | Nonefull_shape_listc                 C  s  | j }t|}t|}g }t|jD ]\}}|| }|du r dn|| }g }	|   D ]}
|
d }|
d }|
d }|
d }||||g}|
d }t|
d }| j	t
| }td|d |d< td|d |d< td|d |d< td|d |d< |durt|d |d |d< t|d |d |d< t|d |d |d< t|d |d |d< |d |d k r|d |d k std|  q.t|||d|||d	}|	| q.||	 q|| _dS )
a9  self._original_predictions is converted to a list of prediction.ObjectPrediction and set to
        self._object_prediction_list_per_image.

        Args:
            shift_amount_list: list of list
                To shift the box and mask predictions from sliced image to full sized image, should
                be in the form of List[[shift_x, shift_y],[shift_x, shift_y],...]
            full_shape_list: list of list
                Size of the full image after shifting, should be in the form of
                List[[height, width],[height, width],...]
        Nr                  z'ignoring invalid prediction with bbox: )bboxcategory_idscoresegmentationr&   shift_amount
full_shape)r4   r   r   r-   xyxycpudetachnumpyintr,   r#   maxminr   warningr   append!_object_prediction_list_per_image)r   r=   r?   original_predictions object_prediction_list_per_image	image_ind image_predictions_in_xyxy_formatrI   rJ   object_prediction_list
predictionx1y1x2y2rE   rG   rF   r&   object_predictionr   r   r   8_create_object_prediction_list_from_original_predictionsU   sR    	
zMYolov5DetectionModel._create_object_prediction_list_from_original_predictions)r    r   )r/   r0   )r=   r>   r?   r>   )__name__r(   __qualname__r   r"   r   r6   propertyr:   r;   r.   r`   __classcell__r   r   r   r   r
      s    





r
   )
__future__r   typingr   rN   npsahi.loggerr   sahi.models.baser   sahi.predictionr   sahi.utils.compatibilityr   r   sahi.utils.import_utilsr	   r
   r   r   r   r   <module>   s    