|
|
|
@ -24,6 +24,8 @@ class Heatmap: |
|
|
|
|
self.view_img = False |
|
|
|
|
self.shape = "circle" |
|
|
|
|
|
|
|
|
|
self.names = None # Classes names |
|
|
|
|
|
|
|
|
|
# Image information |
|
|
|
|
self.imw = None |
|
|
|
|
self.imh = None |
|
|
|
@ -52,10 +54,13 @@ class Heatmap: |
|
|
|
|
# Object Counting Information |
|
|
|
|
self.in_counts = 0 |
|
|
|
|
self.out_counts = 0 |
|
|
|
|
self.counting_list = [] |
|
|
|
|
self.count_ids = [] |
|
|
|
|
self.class_wise_count = {} |
|
|
|
|
self.count_txt_thickness = 0 |
|
|
|
|
self.count_txt_color = (0, 0, 0) |
|
|
|
|
self.count_color = (255, 255, 255) |
|
|
|
|
self.count_txt_color = (255, 255, 255) |
|
|
|
|
self.line_color = (255, 255, 255) |
|
|
|
|
self.cls_txtdisplay_gap = 50 |
|
|
|
|
self.fontsize = 0.6 |
|
|
|
|
|
|
|
|
|
# Decay factor |
|
|
|
|
self.decay_factor = 0.99 |
|
|
|
@ -67,6 +72,7 @@ class Heatmap: |
|
|
|
|
self, |
|
|
|
|
imw, |
|
|
|
|
imh, |
|
|
|
|
classes_names=None, |
|
|
|
|
colormap=cv2.COLORMAP_JET, |
|
|
|
|
heatmap_alpha=0.5, |
|
|
|
|
view_img=False, |
|
|
|
@ -74,13 +80,15 @@ class Heatmap: |
|
|
|
|
view_out_counts=True, |
|
|
|
|
count_reg_pts=None, |
|
|
|
|
count_txt_thickness=2, |
|
|
|
|
count_txt_color=(0, 0, 0), |
|
|
|
|
count_color=(255, 255, 255), |
|
|
|
|
count_txt_color=(255, 255, 255), |
|
|
|
|
fontsize=0.8, |
|
|
|
|
line_color=(255, 255, 255), |
|
|
|
|
count_reg_color=(255, 0, 255), |
|
|
|
|
region_thickness=5, |
|
|
|
|
line_dist_thresh=15, |
|
|
|
|
decay_factor=0.99, |
|
|
|
|
shape="circle", |
|
|
|
|
cls_txtdisplay_gap=50, |
|
|
|
|
): |
|
|
|
|
""" |
|
|
|
|
Configures the heatmap colormap, width, height and display parameters. |
|
|
|
@ -89,6 +97,7 @@ class Heatmap: |
|
|
|
|
colormap (cv2.COLORMAP): The colormap to be set. |
|
|
|
|
imw (int): The width of the frame. |
|
|
|
|
imh (int): The height of the frame. |
|
|
|
|
classes_names (dict): Classes names |
|
|
|
|
heatmap_alpha (float): alpha value for heatmap display |
|
|
|
|
view_img (bool): Flag indicating frame display |
|
|
|
|
view_in_counts (bool): Flag to control whether to display the incounts on video stream. |
|
|
|
@ -96,13 +105,16 @@ class Heatmap: |
|
|
|
|
count_reg_pts (list): Object counting region points |
|
|
|
|
count_txt_thickness (int): Text thickness for object counting display |
|
|
|
|
count_txt_color (RGB color): count text color value |
|
|
|
|
count_color (RGB color): count text background color value |
|
|
|
|
fontsize (float): Text display font size |
|
|
|
|
line_color (RGB color): count highlighter line color |
|
|
|
|
count_reg_color (RGB color): Color of object counting region |
|
|
|
|
region_thickness (int): Object counting Region thickness |
|
|
|
|
line_dist_thresh (int): Euclidean Distance threshold for line counter |
|
|
|
|
decay_factor (float): value for removing heatmap area after object passed |
|
|
|
|
shape (str): Heatmap shape, rect or circle shape supported |
|
|
|
|
cls_txtdisplay_gap (int): Display gap between each class count |
|
|
|
|
""" |
|
|
|
|
self.names = classes_names |
|
|
|
|
self.imw = imw |
|
|
|
|
self.imh = imh |
|
|
|
|
self.heatmap_alpha = heatmap_alpha |
|
|
|
@ -116,29 +128,29 @@ class Heatmap: |
|
|
|
|
if len(count_reg_pts) == 2: |
|
|
|
|
print("Line Counter Initiated.") |
|
|
|
|
self.count_reg_pts = count_reg_pts |
|
|
|
|
self.counting_region = LineString(count_reg_pts) |
|
|
|
|
|
|
|
|
|
elif len(count_reg_pts) == 4: |
|
|
|
|
print("Region Counter Initiated.") |
|
|
|
|
self.counting_region = LineString(self.count_reg_pts) |
|
|
|
|
elif len(count_reg_pts) >= 3: |
|
|
|
|
print("Polygon Counter Initiated.") |
|
|
|
|
self.count_reg_pts = count_reg_pts |
|
|
|
|
self.counting_region = Polygon(self.count_reg_pts) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
print("Region or line points Invalid, 2 or 4 points supported") |
|
|
|
|
print("Invalid Region points provided, region_points must be 2 for lines or >= 3 for polygons.") |
|
|
|
|
print("Using Line Counter Now") |
|
|
|
|
self.counting_region = Polygon([(20, 400), (1260, 400)]) # dummy points |
|
|
|
|
self.counting_region = LineString(self.count_reg_pts) |
|
|
|
|
|
|
|
|
|
# Heatmap new frame |
|
|
|
|
self.heatmap = np.zeros((int(self.imh), int(self.imw)), dtype=np.float32) |
|
|
|
|
|
|
|
|
|
self.count_txt_thickness = count_txt_thickness |
|
|
|
|
self.count_txt_color = count_txt_color |
|
|
|
|
self.count_color = count_color |
|
|
|
|
self.fontsize = fontsize |
|
|
|
|
self.line_color = line_color |
|
|
|
|
self.region_color = count_reg_color |
|
|
|
|
self.region_thickness = region_thickness |
|
|
|
|
self.decay_factor = decay_factor |
|
|
|
|
self.line_dist_thresh = line_dist_thresh |
|
|
|
|
self.shape = shape |
|
|
|
|
self.cls_txtdisplay_gap = cls_txtdisplay_gap |
|
|
|
|
|
|
|
|
|
# shape of heatmap, if not selected |
|
|
|
|
if self.shape not in ["circle", "rect"]: |
|
|
|
@ -183,6 +195,12 @@ class Heatmap: |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
for box, cls, track_id in zip(self.boxes, self.clss, self.track_ids): |
|
|
|
|
# Store class info |
|
|
|
|
if self.names[cls] not in self.class_wise_count: |
|
|
|
|
if len(self.names[cls]) > 5: |
|
|
|
|
self.names[cls] = self.names[cls][:5] |
|
|
|
|
self.class_wise_count[self.names[cls]] = {"in": 0, "out": 0} |
|
|
|
|
|
|
|
|
|
if self.shape == "circle": |
|
|
|
|
center = (int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)) |
|
|
|
|
radius = min(int(box[2]) - int(box[0]), int(box[3]) - int(box[1])) // 2 |
|
|
|
@ -203,23 +221,39 @@ class Heatmap: |
|
|
|
|
if len(track_line) > 30: |
|
|
|
|
track_line.pop(0) |
|
|
|
|
|
|
|
|
|
# Count objects |
|
|
|
|
if len(self.count_reg_pts) == 4: |
|
|
|
|
if self.counting_region.contains(Point(track_line[-1])) and track_id not in self.counting_list: |
|
|
|
|
self.counting_list.append(track_id) |
|
|
|
|
if box[0] < self.counting_region.centroid.x: |
|
|
|
|
self.out_counts += 1 |
|
|
|
|
else: |
|
|
|
|
prev_position = self.track_history[track_id][-2] if len(self.track_history[track_id]) > 1 else None |
|
|
|
|
|
|
|
|
|
# Count objects in any polygon |
|
|
|
|
if len(self.count_reg_pts) >= 3: |
|
|
|
|
is_inside = self.counting_region.contains(Point(track_line[-1])) |
|
|
|
|
|
|
|
|
|
if prev_position is not None and is_inside and track_id not in self.count_ids: |
|
|
|
|
self.count_ids.append(track_id) |
|
|
|
|
|
|
|
|
|
if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0: |
|
|
|
|
self.in_counts += 1 |
|
|
|
|
self.class_wise_count[self.names[cls]]["in"] += 1 |
|
|
|
|
else: |
|
|
|
|
self.out_counts += 1 |
|
|
|
|
self.class_wise_count[self.names[cls]]["out"] += 1 |
|
|
|
|
|
|
|
|
|
# Count objects using line |
|
|
|
|
elif len(self.count_reg_pts) == 2: |
|
|
|
|
distance = Point(track_line[-1]).distance(self.counting_region) |
|
|
|
|
if distance < self.line_dist_thresh and track_id not in self.counting_list: |
|
|
|
|
self.counting_list.append(track_id) |
|
|
|
|
if box[0] < self.counting_region.centroid.x: |
|
|
|
|
self.out_counts += 1 |
|
|
|
|
else: |
|
|
|
|
self.in_counts += 1 |
|
|
|
|
is_inside = (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0 |
|
|
|
|
|
|
|
|
|
if prev_position is not None and is_inside and track_id not in self.count_ids: |
|
|
|
|
distance = Point(track_line[-1]).distance(self.counting_region) |
|
|
|
|
|
|
|
|
|
if distance < self.line_dist_thresh and track_id not in self.count_ids: |
|
|
|
|
self.count_ids.append(track_id) |
|
|
|
|
|
|
|
|
|
if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0: |
|
|
|
|
self.in_counts += 1 |
|
|
|
|
self.class_wise_count[self.names[cls]]["in"] += 1 |
|
|
|
|
else: |
|
|
|
|
self.out_counts += 1 |
|
|
|
|
self.class_wise_count[self.names[cls]]["out"] += 1 |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
for box, cls in zip(self.boxes, self.clss): |
|
|
|
|
if self.shape == "circle": |
|
|
|
@ -240,26 +274,30 @@ class Heatmap: |
|
|
|
|
heatmap_normalized = cv2.normalize(self.heatmap, None, 0, 255, cv2.NORM_MINMAX) |
|
|
|
|
heatmap_colored = cv2.applyColorMap(heatmap_normalized.astype(np.uint8), self.colormap) |
|
|
|
|
|
|
|
|
|
incount_label = f"In Count : {self.in_counts}" |
|
|
|
|
outcount_label = f"OutCount : {self.out_counts}" |
|
|
|
|
|
|
|
|
|
# Display counts based on user choice |
|
|
|
|
counts_label = None |
|
|
|
|
if not self.view_in_counts and not self.view_out_counts: |
|
|
|
|
counts_label = None |
|
|
|
|
elif not self.view_in_counts: |
|
|
|
|
counts_label = outcount_label |
|
|
|
|
elif not self.view_out_counts: |
|
|
|
|
counts_label = incount_label |
|
|
|
|
else: |
|
|
|
|
counts_label = f"{incount_label} {outcount_label}" |
|
|
|
|
label = "Ultralytics Analytics \t" |
|
|
|
|
|
|
|
|
|
for key, value in self.class_wise_count.items(): |
|
|
|
|
if value["in"] != 0 or value["out"] != 0: |
|
|
|
|
if not self.view_in_counts and not self.view_out_counts: |
|
|
|
|
label = None |
|
|
|
|
elif not self.view_in_counts: |
|
|
|
|
label += f"{str.capitalize(key)}: IN {value['in']} \t" |
|
|
|
|
elif not self.view_out_counts: |
|
|
|
|
label += f"{str.capitalize(key)}: OUT {value['out']} \t" |
|
|
|
|
else: |
|
|
|
|
label += f"{str.capitalize(key)}: IN {value['in']} OUT {value['out']} \t" |
|
|
|
|
|
|
|
|
|
label = label.rstrip() |
|
|
|
|
label = label.split("\t") |
|
|
|
|
|
|
|
|
|
if self.count_reg_pts is not None and counts_label is not None: |
|
|
|
|
self.annotator.count_labels( |
|
|
|
|
counts=counts_label, |
|
|
|
|
count_txt_size=self.count_txt_thickness, |
|
|
|
|
if self.count_reg_pts is not None and label is not None: |
|
|
|
|
self.annotator.display_counts( |
|
|
|
|
counts=label, |
|
|
|
|
tf=self.count_txt_thickness, |
|
|
|
|
fontScale=self.fontsize, |
|
|
|
|
txt_color=self.count_txt_color, |
|
|
|
|
color=self.count_color, |
|
|
|
|
line_color=self.line_color, |
|
|
|
|
classwise_txtgap=self.cls_txtdisplay_gap, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.im0 = cv2.addWeighted(self.im0, 1 - self.heatmap_alpha, heatmap_colored, self.heatmap_alpha, 0) |
|
|
|
|