Ë
    Fêñii  ã                  ó^   — d dl mZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
mZ  G d„ de«      Zy)é    )Úannotations)ÚAnyN)ÚObjectCounter)ÚSolutionAnnotatorÚSolutionResultsc                  ó4   ‡ — e Zd ZdZdˆ fd„Zdd„Zdd„Zˆ xZS )ÚHeatmapa  A class to draw heatmaps in real-time video streams based on object tracks.

    This class extends the ObjectCounter class to generate and visualize heatmaps of object movements in video
    streams. It uses tracked object positions to create a cumulative heatmap effect over time.

    Attributes:
        initialized (bool): Flag indicating whether the heatmap has been initialized.
        colormap (int): OpenCV colormap used for heatmap visualization.
        heatmap (np.ndarray): Array storing the cumulative heatmap data.
        annotator (SolutionAnnotator): Object for drawing annotations on the image.

    Methods:
        heatmap_effect: Calculate and update the heatmap effect for a given bounding box.
        process: Generate and apply the heatmap effect to each frame.

    Examples:
        >>> from ultralytics.solutions import Heatmap
        >>> heatmap = Heatmap(model="yolo26n.pt", colormap=cv2.COLORMAP_JET)
        >>> frame = cv2.imread("frame.jpg")
        >>> processed_frame = heatmap.process(frame)
    c                ó    •— t        ‰| �  di |¤Ž d| _        | j                  �| j	                  «        | j
                  d   | _        d| _        y)zÒInitialize the Heatmap class for real-time video stream heatmap generation based on object tracks.

        Args:
            **kwargs (Any): Keyword arguments passed to the parent ObjectCounter class.
        FNÚcolormap© )ÚsuperÚ__init__ÚinitializedÚregionÚinitialize_regionÚCFGr   Úheatmap)ÚselfÚkwargsÚ	__class__s     €ú_/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/solutions/heatmap.pyr   zHeatmap.__init__%   sL   ø€ ô 	‰ÑÑ"˜6Ò"à ˆÔØ�;‰;Ð"Ø×"Ñ"Ô$ð Ÿ™ Ñ,ˆŒØˆ�ó    c                ód  — t        t        |«      \  }}}}t        ||z
  ||z
  «      dz  dz  }t        j                  t        j
                  ||«      t        j
                  ||«      «      \  }}|||z   dz  z
  dz  |||z   dz  z
  dz  z   }	|	|k  }
| j                  ||…||…f   |
xx   dz  cc<   y)z¯Efficiently calculate heatmap area and effect location for applying colormap.

        Args:
            box (list[float]): Bounding box coordinates [x0, y0, x1, y1].
        é   N)ÚmapÚintÚminÚnpÚmeshgridÚaranger   )r   ÚboxÚx0Úy0Úx1Úy1Úradius_squaredÚxvÚyvÚdist_squaredÚwithin_radiuss              r   Úheatmap_effectzHeatmap.heatmap_effect5   sÅ   € ô œS #›‰ˆˆB��BÜ˜b 2™g r¨B¡wÓ/°1Ñ4¸Ñ:ˆô —‘œRŸY™Y r¨2Ó.´·	±	¸"¸bÓ0AÓB‰ˆˆBð ˜r B™w¨1™nÑ-°!Ñ3°r¸bÀ2¹gÈ!¹^Ñ7LÐQRÑ6RÑRˆð %¨Ñ6ˆð 	�‰�R˜�U˜B˜r˜E�\Ñ" =Ó1°QÑ6Ô1r   c           	     ó  — | j                   s4t        j                  |t        j                  ¬«      dz  | _        d| _         | j                  |«       t        || j                  ¬«      | _        t        | j                  | j                  | j                  «      D ]¾  \  }}}| j                  |«       | j                  €Œ%| j                  j                  | j                  d| j                  dz  ¬«       | j!                  ||«       d}t#        | j$                  |   «      d	kD  r| j$                  |   d
   }| j'                  | j$                  |   d   |||«       ŒÀ | j                  j)                  «       }| j                  �| j+                  |«       | j,                  j.                  r‡t1        j2                  | j                  dddt0        j4                  «      j7                  t        j8                  «      }t1        j:                  || j<                  «      }t1        j>                  |d|dd«      }| jA                  |«       tC        || jD                  | jF                  tI        | jJ                  «      t#        | j                  «      ¬«      S )aå  Generate heatmap for each frame using Ultralytics tracking.

        Args:
            im0 (np.ndarray): Input image array for processing.

        Returns:
            (SolutionResults): Contains processed image `plot_im`, 'in_count' (int, count of objects entering the
                region), 'out_count' (int, count of objects exiting the region), 'classwise_count' (dict, per-class
                object count), and 'total_tracks' (int, total number of tracked objects).
        )Údtypeg®Gáz®ï?T)Ú
line_widthN)éh   r   é{   r   )Úreg_ptsÚcolorÚ	thicknessé   éþÿÿÿéÿÿÿÿr   éÿ   g      à?)Úplot_imÚin_countÚ	out_countÚclasswise_countÚtotal_tracks)&r   r   Ú
zeros_likeÚfloat32r   Úextract_tracksr   r.   Ú	annotatorÚzipÚboxesÚ	track_idsÚclssr+   r   Údraw_regionÚstore_tracking_historyÚlenÚtrack_historyÚcount_objectsÚresultÚdisplay_countsÚ
track_dataÚis_trackÚcv2Ú	normalizeÚNORM_MINMAXÚastypeÚuint8ÚapplyColorMapr   ÚaddWeightedÚdisplay_outputr   r9   r:   Údictr;   )	r   Úim0r!   Útrack_idÚclsÚprev_positionr8   Únormalized_heatmapÚcolored_heatmaps	            r   ÚprocesszHeatmap.processJ   s	  € ð ×ÒÜŸ=™=¨´B·J±JÔ?À$ÑFˆDŒLØ#ˆDÔà×Ñ˜CÔ Ü*¨3¸4¿?¹?ÔKˆŒô #& d§j¡j°$·.±.À$Ç)Á)Ó"Lò 	cÑˆC�˜3à×Ñ Ô$à�{‰{Ñ&Ø—‘×*Ñ*°4·;±;ÀmÐ_c×_nÑ_nÐqrÑ_rÐ*ÔsØ×+Ñ+¨H°cÔ:à $�Ü�t×)Ñ)¨(Ñ3Ó4°qÒ8Ø$(×$6Ñ$6°xÑ$@ÀÑ$D�MØ×"Ñ" 4×#5Ñ#5°hÑ#?ÀÑ#CÀXÈ}Ð^aÕbð	cð —.‘.×'Ñ'Ó)ˆØ�;‰;Ð"Ø×Ñ Ô(ð �?‰?×#Ò#Ü!$§¡¨t¯|©|¸TÀ1ÀcÌ3Ï?É?Ó![×!bÑ!bÔce×ckÑckÓ!lÐÜ!×/Ñ/Ð0BÀDÇMÁMÓRˆOÜ—o‘o g¨s°OÀSÈ!ÓLˆGà×Ñ˜GÔ$ô ØØ—]‘]Ø—n‘nÜ  ×!5Ñ!5Ó6Ü˜TŸ^™^Ó,ô
ð 	
r   )r   r   ÚreturnÚNone)r!   zlist[float]r^   r_   )rW   z
np.ndarrayr^   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   r]   Ú__classcell__)r   s   @r   r	   r	      s   ø„ ñõ,ó 7÷*3
r   r	   )Ú
__future__r   Útypingr   rN   Únumpyr   Ú$ultralytics.solutions.object_counterr   Úultralytics.solutions.solutionsr   r   r	   r   r   r   ú<module>rj      s'   ðõ #å ã 
Û å >ß Nôo
ˆmõ o
r   