
    j                     V    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e          Z	dS )    )Path)Any)BaseSolutionSolutionResults)save_one_boxc                   8     e Zd ZdZdeddf fdZdefdZ xZS )ObjectCroppera  A class to manage the cropping of detected objects in a real-time video stream or images.

    This class extends the BaseSolution class and provides functionality for cropping objects based on detected bounding
    boxes. The cropped images are saved to a specified directory for further analysis or usage.

    Attributes:
        crop_dir (str): Directory where cropped object images are stored.
        crop_idx (int): Counter for the total number of cropped objects.
        iou (float): IoU (Intersection over Union) threshold for non-maximum suppression.
        conf (float): Confidence threshold for filtering detections.

    Methods:
        process: Crop detected objects from the input image and save them to the output directory.

    Examples:
        >>> cropper = ObjectCropper()
        >>> frame = cv2.imread("frame.jpg")
        >>> processed_results = cropper.process(frame)
        >>> print(f"Total cropped objects: {cropper.crop_idx}")
    kwargsreturnNc                 z    t                      j        di | | j        d         | _        t	          | j                                      dd           | j        d         r-| j                            d| j         d           d| j        d<   d| _        | j        d	         | _	        | j        d
         | _
        dS )a6  Initialize the ObjectCropper class for cropping objects from detected bounding boxes.

        Args:
            **kwargs (Any): Keyword arguments passed to the parent class and used for configuration including:
                - crop_dir (str): Path to the directory for saving cropped object images.
        crop_dirT)parentsexist_okshowz?show=True is not supported for ObjectCropper; saving crops to 'z'.Fr   iouconfN )super__init__CFGr   r   mkdirLOGGERwarningcrop_idxr   r   )selfr
   	__class__s     i/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/solutions/object_cropper.pyr   zObjectCropper.__init__    s     	""6""",T]!!$!>>>8F 	%K sbfbo s s sttt$DHV8E?HV$			    c           	         | j         d         5  | j                            || j        | j        | j        | j        d         d          d         }|j        j        	                                | _
        ddd           n# 1 swxY w Y   |j        D ]H}| xj        dz  c_        t          |j        |t          | j                  d| j         dz  d	
           It!          || j                  S )a9  Crop detected objects from the input image and save them as separate images.

        Args:
            im0 (np.ndarray): The input image containing detected objects.

        Returns:
            (SolutionResults): A SolutionResults object containing the total number of cropped objects and processed
                image.

        Examples:
            >>> cropper = ObjectCropper()
            >>> frame = cv2.imread("image.jpg")
            >>> results = cropper.process(frame)
            >>> print(f"Total cropped objects: {results.total_crop_objects}")
        r   deviceF)classesr   r   r    verboseN   crop_z.jpgT)fileBGR)plot_imtotal_crop_objects)	profilersmodelpredictr!   r   r   r   boxesclstolistclssr   r   xyxyr   r   r   )r   im0resultsboxs       r   processzObjectCropper.process2   sF     ^A 		3 		3j((YHx) )   G  )0022DI		3 		3 		3 		3 		3 		3 		3 		3 		3 		3 		3 		3 		3 		3 		3 = 	 	CMMQMM$-((+F4=+F+F+FF	     st}MMMMs   A$A>>BB)	__name__
__module____qualname____doc__r   r   r   r4   __classcell__)r   s   @r   r	   r	   
   sy         *% % % % % % % %$%No %N %N %N %N %N %N %N %Nr   r	   N)
pathlibr   typingr   ultralytics.solutions.solutionsr   r   ultralytics.utils.plottingr   r	   r   r   r   <module>r>      s                I I I I I I I I 3 3 3 3 3 3MN MN MN MN MNL MN MN MN MN MNr   