
    h                     V    S SK r S SKJr  S SKJr  S SKJrJr  S SKJ	r	   " S S\5      r
g)    N)Path)Any)BaseSolutionSolutionResults)save_one_boxc                   F   ^  \ rS rSrSrS\SS4U 4S jjrS\4S jrSr	U =r
$ )	ObjectCropper   a  
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U ]  " S0 UD6  U R                  S   U l        [        R
                  R                  U R                  5      (       d   [        R                  " U R                  5        U R                  S   (       a(  U R                  R                  SU R                   35        SU l
        U R                  S   U l        U R                  S   U l        g)	a  
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     ^/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/solutions/object_cropper.pyr   ObjectCropper.__init__"   s     	"6",ww~~dmm,,HHT]]#88FKKfgkgtgtfuv 88E?HHV$	    c           
      (   U R                   S      U R                  R                  UU R                  U R                  U R
                  U R                  S   SS9S   nUR                  R                  R                  5       U l
        SSS5        WR                   HR  nU =R                  S-  sl        [        UR                  U[        U R                  5      SU R                   S3-  S	S
9  MT     [!        XR                  S9$ ! , (       d  f       N= f)a  
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   processObjectCropper.process7   s    " ^^Ajj((YYHHxx) )  G  ))002DI  ==CMMQM$--(U4==/+FF	 ! s}}MM+ s   A6D
D)r2   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r7   __static_attributes____classcell__)r   s   @r   r	   r	      s/    ,% % %*&No &N &Nr!   r	   )r   pathlibr   typingr   ultralytics.solutions.solutionsr   r   ultralytics.utils.plottingr   r	   r   r!   r   <module>rD      s&    
   I 3RNL RNr!   