o
    Whr                     @   sH   d dl Z d dlmZmZmZ d dlmZ d dlmZ G dd deZ	dS )    N)BaseSolutionSolutionAnnotatorSolutionResults)LOGGER)colorsc                       s(   e Zd ZdZ fddZdd Z  ZS )ObjectBlurrera6  
    A class to manage the blurring of detected objects in a real-time video stream.

    This class extends the BaseSolution class and provides functionality for blurring objects based on detected bounding
    boxes. The blurred areas are updated directly in the input image, allowing for privacy preservation or other effects.

    Attributes:
        blur_ratio (int): The intensity of the blur effect applied to detected objects (higher values create more blur).
        iou (float): Intersection over Union threshold for object detection.
        conf (float): Confidence threshold for object detection.

    Methods:
        process: Applies a blurring effect to detected objects in the input image.
        extract_tracks: Extracts tracking information from detected objects.
        display_output: Displays the processed output image.

    Examples:
        >>> blurrer = ObjectBlurrer()
        >>> frame = cv2.imread("frame.jpg")
        >>> processed_results = blurrer.process(frame)
        >>> print(f"Total blurred objects: {processed_results.total_tracks}")
    c                    sF   t  jdi | |dd}|dk rtd d}t|d | _dS )aG  
        Initialize the ObjectBlurrer class for applying a blur effect to objects detected in video streams or images.

        Args:
            **kwargs (Any): Keyword arguments passed to the parent class and for configuration.
                blur_ratio (float): Intensity of the blur effect (0.1-1.0, default=0.5).
        
blur_ratiog      ?g?zDblur ratio cannot be less than 0.1, updating it to default value 0.5d   N )super__init__getr   warningintr   )selfkwargsr   	__class__r
   X/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/object_blurrer.pyr   #   s   
zObjectBlurrer.__init__c              	   C   s   |  | t|| j}t| j| j| jD ]P\}}}t|t	|d t	|d t	|d t	|d f | j
| j
f}||t	|d t	|d t	|d t	|d f< |j|| ||t|dd q| }| | t|t| jdS )a[  
        Apply a blurring effect to detected objects in the input image.

        This method extracts tracking information, applies blur to regions corresponding to detected objects,
        and annotates the image with bounding boxes.

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

        Returns:
            (SolutionResults): Object containing the processed image and number of tracked objects.
                - plot_im (numpy.ndarray): The annotated output image with blurred objects.
                - total_tracks (int): The total number of tracked objects in the frame.

        Examples:
            >>> blurrer = ObjectBlurrer()
            >>> frame = cv2.imread("image.jpg")
            >>> results = blurrer.process(frame)
            >>> print(f"Blurred {results.total_tracks} objects")
              r      T)labelcolor)plot_imtotal_tracks)extract_tracksr   
line_widthzipboxesclssconfscv2blurr   r   	box_labeladjust_box_labelr   resultdisplay_outputr   len	track_ids)r   im0	annotatorboxclsconfblur_objr   r
   r
   r   process2   s   
2
4
zObjectBlurrer.process)__name__
__module____qualname____doc__r   r0   __classcell__r
   r
   r   r   r      s    r   )
r"   ultralytics.solutions.solutionsr   r   r   ultralytics.utilsr   ultralytics.utils.plottingr   r   r
   r
   r
   r   <module>   s
   