o
    Wh6                     @   sD   d dl 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 )    N)Path)BaseSolutionSolutionResults)save_one_boxc                       s(   e Zd ZdZ fddZdd Z  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: Crops detected objects from the input image and saves 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}")
    c                    s   t  jd
i | |dd| _tj| jst| j | jd r,| j	
d| j  d| _| jd | _| jd durD| jd | _dS d	| _dS )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_dirzcropped-detectionsshowzTshow=True disabled for crop solution, results will be saved in the directory named: r   iouconfNg      ? )super__init__getr   ospathexistsmkdirCFGLOGGERwarningcrop_idxr	   r
   )selfkwargs	__class__r   X/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/object_cropper.pyr   !   s   

(zObjectCropper.__init__c                 C   st   | j j|| j| j| j| jd dd }|jD ]}|  jd7  _t|j	|t
| jd| j d dd qt|| jd	S )
a5  
        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}")
        device)classesr
   r	   r   r      crop_z.jpgT)fileBGR)plot_imtotal_crop_objects)modelpredictr   r
   r	   r   boxesr   r   xyxyr   r   r   )r   im0resultsboxr   r   r   process6   s   
zObjectCropper.process)__name__
__module____qualname____doc__r   r+   __classcell__r   r   r   r   r   
   s    r   )	r   pathlibr   ultralytics.solutions.solutionsr   r   ultralytics.utils.plottingr   r   r   r   r   r   <module>   s
   