Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#18534 Create .dockerignore

Merged
Glenn Jocher merged 1 commits into Ultralytics:main from ultralytics:dockerignore
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  1. # Ultralytics YOLO ๐Ÿš€, AGPL-3.0 license
  2. import cv2
  3. import numpy as np
  4. from ultralytics.solutions.object_counter import ObjectCounter
  5. from ultralytics.utils.plotting import Annotator
  6. class Heatmap(ObjectCounter):
  7. """
  8. A class to draw heatmaps in real-time video streams based on object tracks.
  9. This class extends the ObjectCounter class to generate and visualize heatmaps of object movements in video
  10. streams. It uses tracked object positions to create a cumulative heatmap effect over time.
  11. Attributes:
  12. initialized (bool): Flag indicating whether the heatmap has been initialized.
  13. colormap (int): OpenCV colormap used for heatmap visualization.
  14. heatmap (np.ndarray): Array storing the cumulative heatmap data.
  15. annotator (Annotator): Object for drawing annotations on the image.
  16. Methods:
  17. heatmap_effect: Calculates and updates the heatmap effect for a given bounding box.
  18. generate_heatmap: Generates and applies the heatmap effect to each frame.
  19. Examples:
  20. >>> from ultralytics.solutions import Heatmap
  21. >>> heatmap = Heatmap(model="yolov8n.pt", colormap=cv2.COLORMAP_JET)
  22. >>> frame = cv2.imread("frame.jpg")
  23. >>> processed_frame = heatmap.generate_heatmap(frame)
  24. """
  25. def __init__(self, **kwargs):
  26. """Initializes the Heatmap class for real-time video stream heatmap generation based on object tracks."""
  27. super().__init__(**kwargs)
  28. self.initialized = False # bool variable for heatmap initialization
  29. if self.region is not None: # check if user provided the region coordinates
  30. self.initialize_region()
  31. # store colormap
  32. self.colormap = cv2.COLORMAP_PARULA if self.CFG["colormap"] is None else self.CFG["colormap"]
  33. self.heatmap = None
  34. def heatmap_effect(self, box):
  35. """
  36. Efficiently calculates heatmap area and effect location for applying colormap.
  37. Args:
  38. box (List[float]): Bounding box coordinates [x0, y0, x1, y1].
  39. Examples:
  40. >>> heatmap = Heatmap()
  41. >>> box = [100, 100, 200, 200]
  42. >>> heatmap.heatmap_effect(box)
  43. """
  44. x0, y0, x1, y1 = map(int, box)
  45. radius_squared = (min(x1 - x0, y1 - y0) // 2) ** 2
  46. # Create a meshgrid with region of interest (ROI) for vectorized distance calculations
  47. xv, yv = np.meshgrid(np.arange(x0, x1), np.arange(y0, y1))
  48. # Calculate squared distances from the center
  49. dist_squared = (xv - ((x0 + x1) // 2)) ** 2 + (yv - ((y0 + y1) // 2)) ** 2
  50. # Create a mask of points within the radius
  51. within_radius = dist_squared <= radius_squared
  52. # Update only the values within the bounding box in a single vectorized operation
  53. self.heatmap[y0:y1, x0:x1][within_radius] += 2
  54. def generate_heatmap(self, im0):
  55. """
  56. Generate heatmap for each frame using Ultralytics.
  57. Args:
  58. im0 (np.ndarray): Input image array for processing.
  59. Returns:
  60. (np.ndarray): Processed image with heatmap overlay and object counts (if region is specified).
  61. Examples:
  62. >>> heatmap = Heatmap()
  63. >>> im0 = cv2.imread("image.jpg")
  64. >>> result = heatmap.generate_heatmap(im0)
  65. """
  66. if not self.initialized:
  67. self.heatmap = np.zeros_like(im0, dtype=np.float32) * 0.99
  68. self.initialized = True # Initialize heatmap only once
  69. self.annotator = Annotator(im0, line_width=self.line_width) # Initialize annotator
  70. self.extract_tracks(im0) # Extract tracks
  71. # Iterate over bounding boxes, track ids and classes index
  72. for box, track_id, cls in zip(self.boxes, self.track_ids, self.clss):
  73. # Draw bounding box and counting region
  74. self.heatmap_effect(box)
  75. if self.region is not None:
  76. self.annotator.draw_region(reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width * 2)
  77. self.store_tracking_history(track_id, box) # Store track history
  78. self.store_classwise_counts(cls) # store classwise counts in dict
  79. current_centroid = ((box[0] + box[2]) / 2, (box[1] + box[3]) / 2)
  80. # Store tracking previous position and perform object counting
  81. prev_position = None
  82. if len(self.track_history[track_id]) > 1:
  83. prev_position = self.track_history[track_id][-2]
  84. self.count_objects(current_centroid, track_id, prev_position, cls) # Perform object counting
  85. if self.region is not None:
  86. self.display_counts(im0) # Display the counts on the frame
  87. # Normalize, apply colormap to heatmap and combine with original image
  88. if self.track_data.id is not None:
  89. im0 = cv2.addWeighted(
  90. im0,
  91. 0.5,
  92. cv2.applyColorMap(
  93. cv2.normalize(self.heatmap, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8), self.colormap
  94. ),
  95. 0.5,
  96. 0,
  97. )
  98. self.display_output(im0) # display output with base class function
  99. return im0 # return output image for more usage
Discard
Tip!

Press p or to see the previous file or, n or to see the next file