Reference for ultralytics/solutions/heatmap.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/heatmap.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.heatmap.Heatmap
Bases: ObjectCounter
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:
Name | Type | Description |
---|---|---|
initialized |
bool
|
Flag indicating whether the heatmap has been initialized. |
colormap |
int
|
OpenCV colormap used for heatmap visualization. |
heatmap |
ndarray
|
Array storing the cumulative heatmap data. |
annotator |
Annotator
|
Object for drawing annotations on the image. |
Methods:
Name | Description |
---|---|
heatmap_effect |
Calculates and updates the heatmap effect for a given bounding box. |
generate_heatmap |
Generates and applies the heatmap effect to each frame. |
Examples:
>>> from ultralytics.solutions import Heatmap
>>> heatmap = Heatmap(model="yolov8n.pt", colormap=cv2.COLORMAP_JET)
>>> frame = cv2.imread("frame.jpg")
>>> processed_frame = heatmap.generate_heatmap(frame)
Source code in ultralytics/solutions/heatmap.py
generate_heatmap
Generate heatmap for each frame using Ultralytics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
Input image array for processing. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Processed image with heatmap overlay and object counts (if region is specified). |
Examples:
>>> heatmap = Heatmap()
>>> im0 = cv2.imread("image.jpg")
>>> result = heatmap.generate_heatmap(im0)
Source code in ultralytics/solutions/heatmap.py
heatmap_effect
Efficiently calculates heatmap area and effect location for applying colormap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box
|
List[float]
|
Bounding box coordinates [x0, y0, x1, y1]. |
required |
Examples: