
    |h                     V    d dl 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e      Z
y)    N)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                    t        |   di | | j                  d   | _        t        j
                  j                  | j                        st	        j                  | j                         | j                  d   r(| j                  j                  d| j                          d| _
        | j                  d   | _        | j                  d   | _        y)	a3  
        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.
                crop_dir (str): Path to the directory for saving cropped object images.
        crop_dirshowzTshow=True disabled for crop solution, results will be saved in the directory named: r   iouconfN )super__init__CFGr   ospathexistsmkdirLOGGERwarningcrop_idxr   r   )selfr
   	__class__s     c/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/solutions/object_cropper.pyr   zObjectCropper.__init__"   s     	"6",ww~~dmm,HHT]]#88FKKfgkgtgtfuv 88E?HHV$	    c           	         | j                   d   5  | j                  j                  || j                  | j                  | j
                  | j                  d   d      d   }ddd       j                  D ]T  }| xj                  dz  c_        t        |j                  |t        | j                        d| j                   dz  d	
       V t        || j                        S # 1 sw Y   xY w)aE  
        Crop detected objects from the input image and save them as separate images.

        Args:
            im0 (numpy.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   boxesr   r   xyxyr   r   r   )r   im0resultsboxs       r   processzObjectCropper.process7   s    " ^^A 	jj((YYHHxx) )  G	 == 	CMMQM$--(U4==/+FF		 st}}MM)	 	s   AC""C+)	__name__
__module____qualname____doc__r   r   r   r2   __classcell__)r   s   @r   r	   r	      s)    ,% % %*%No %Nr   r	   )r   pathlibr   typingr   ultralytics.solutions.solutionsr   r   ultralytics.utils.plottingr   r	   r   r   r   <module>r<      s&    
   I 3QNL QNr   