Add line counting and circular heatmaps in Ultralytics Solutions (#7113)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
a5735724c5
commit
38eaf5e29f
5 changed files with 526 additions and 247 deletions
|
|
@ -10,8 +10,7 @@ from ultralytics.utils.plotting import Annotator
|
|||
|
||||
check_requirements('shapely>=2.0.0')
|
||||
|
||||
from shapely.geometry import Polygon
|
||||
from shapely.geometry.point import Point
|
||||
from shapely.geometry import LineString, Point, Polygon
|
||||
|
||||
|
||||
class Heatmap:
|
||||
|
|
@ -23,6 +22,7 @@ class Heatmap:
|
|||
# Visual information
|
||||
self.annotator = None
|
||||
self.view_img = False
|
||||
self.shape = 'circle'
|
||||
|
||||
# Image information
|
||||
self.imw = None
|
||||
|
|
@ -38,17 +38,22 @@ class Heatmap:
|
|||
self.boxes = None
|
||||
self.track_ids = None
|
||||
self.clss = None
|
||||
self.track_history = None
|
||||
self.track_history = defaultdict(list)
|
||||
|
||||
# Counting info
|
||||
# Region & Line Information
|
||||
self.count_reg_pts = None
|
||||
self.count_region = None
|
||||
self.counting_region = None
|
||||
self.line_dist_thresh = 15
|
||||
self.region_thickness = 5
|
||||
self.region_color = (255, 0, 255)
|
||||
|
||||
# Object Counting Information
|
||||
self.in_counts = 0
|
||||
self.out_counts = 0
|
||||
self.count_list = []
|
||||
self.counting_list = []
|
||||
self.count_txt_thickness = 0
|
||||
self.count_reg_color = (0, 255, 0)
|
||||
self.region_thickness = 5
|
||||
self.count_txt_color = (0, 0, 0)
|
||||
self.count_color = (255, 255, 255)
|
||||
|
||||
# Decay factor
|
||||
self.decay_factor = 0.99
|
||||
|
|
@ -64,9 +69,13 @@ class Heatmap:
|
|||
view_img=False,
|
||||
count_reg_pts=None,
|
||||
count_txt_thickness=2,
|
||||
count_txt_color=(0, 0, 0),
|
||||
count_color=(255, 255, 255),
|
||||
count_reg_color=(255, 0, 255),
|
||||
region_thickness=5,
|
||||
decay_factor=0.99):
|
||||
line_dist_thresh=15,
|
||||
decay_factor=0.99,
|
||||
shape='circle'):
|
||||
"""
|
||||
Configures the heatmap colormap, width, height and display parameters.
|
||||
|
||||
|
|
@ -78,27 +87,55 @@ class Heatmap:
|
|||
view_img (bool): Flag indicating frame display
|
||||
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
|
||||
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
|
||||
"""
|
||||
self.imw = imw
|
||||
self.imh = imh
|
||||
self.colormap = colormap
|
||||
self.heatmap_alpha = heatmap_alpha
|
||||
self.view_img = view_img
|
||||
self.colormap = colormap
|
||||
|
||||
self.heatmap = np.zeros((int(self.imw), int(self.imh)), dtype=np.float32) # Heatmap new frame
|
||||
|
||||
# Region and line selection
|
||||
if count_reg_pts is not None:
|
||||
self.track_history = defaultdict(list)
|
||||
self.count_reg_pts = count_reg_pts
|
||||
self.count_region = Polygon(self.count_reg_pts)
|
||||
|
||||
self.count_txt_thickness = count_txt_thickness # Counting text thickness
|
||||
self.count_reg_color = count_reg_color
|
||||
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.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('Using Line Counter Now')
|
||||
self.counting_region = Polygon([(20, 400), (1260, 400)]) # dummy points
|
||||
|
||||
# Heatmap new frame
|
||||
self.heatmap = np.zeros((int(self.imw), int(self.imh)), dtype=np.float32)
|
||||
|
||||
self.count_txt_thickness = count_txt_thickness
|
||||
self.count_txt_color = count_txt_color
|
||||
self.count_color = count_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
|
||||
|
||||
# shape of heatmap, if not selected
|
||||
if self.shape not in ['circle', 'rect']:
|
||||
print("Unknown shape value provided, 'circle' & 'rect' supported")
|
||||
print('Using Circular shape now')
|
||||
self.shape = 'circle'
|
||||
|
||||
def extract_results(self, tracks):
|
||||
"""
|
||||
|
|
@ -128,13 +165,26 @@ class Heatmap:
|
|||
self.annotator = Annotator(self.im0, self.count_txt_thickness, None)
|
||||
|
||||
if self.count_reg_pts is not None:
|
||||
|
||||
# Draw counting region
|
||||
self.annotator.draw_region(reg_pts=self.count_reg_pts,
|
||||
color=self.count_reg_color,
|
||||
color=self.region_color,
|
||||
thickness=self.region_thickness)
|
||||
|
||||
for box, cls, track_id in zip(self.boxes, self.clss, self.track_ids):
|
||||
self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 1
|
||||
|
||||
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
|
||||
|
||||
y, x = np.ogrid[0:self.heatmap.shape[0], 0:self.heatmap.shape[1]]
|
||||
mask = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= radius ** 2
|
||||
|
||||
self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += \
|
||||
(2 * mask[int(box[1]):int(box[3]), int(box[0]):int(box[2])])
|
||||
|
||||
else:
|
||||
self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 2
|
||||
|
||||
# Store tracking hist
|
||||
track_line = self.track_history[track_id]
|
||||
|
|
@ -143,16 +193,39 @@ class Heatmap:
|
|||
track_line.pop(0)
|
||||
|
||||
# Count objects
|
||||
if self.count_region.contains(Point(track_line[-1])):
|
||||
if track_id not in self.count_list:
|
||||
self.count_list.append(track_id)
|
||||
if box[0] < self.count_region.centroid.x:
|
||||
self.out_counts += 1
|
||||
else:
|
||||
self.in_counts += 1
|
||||
if len(self.count_reg_pts) == 4:
|
||||
if self.counting_region.contains(Point(track_line[-1])):
|
||||
if 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
|
||||
|
||||
elif len(self.count_reg_pts) == 2:
|
||||
distance = Point(track_line[-1]).distance(self.counting_region)
|
||||
if distance < self.line_dist_thresh:
|
||||
if 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
|
||||
else:
|
||||
for box, cls in zip(self.boxes, self.clss):
|
||||
self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 1
|
||||
|
||||
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
|
||||
|
||||
y, x = np.ogrid[0:self.heatmap.shape[0], 0:self.heatmap.shape[1]]
|
||||
mask = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= radius ** 2
|
||||
|
||||
self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += \
|
||||
(2 * mask[int(box[1]):int(box[3]), int(box[0]):int(box[2])])
|
||||
|
||||
else:
|
||||
self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 2
|
||||
|
||||
# Normalize, apply colormap to heatmap and combine with original image
|
||||
heatmap_normalized = cv2.normalize(self.heatmap, None, 0, 255, cv2.NORM_MINMAX)
|
||||
|
|
@ -161,7 +234,11 @@ class Heatmap:
|
|||
if self.count_reg_pts is not None:
|
||||
incount_label = 'InCount : ' + f'{self.in_counts}'
|
||||
outcount_label = 'OutCount : ' + f'{self.out_counts}'
|
||||
self.annotator.count_labels(in_count=incount_label, out_count=outcount_label)
|
||||
self.annotator.count_labels(in_count=incount_label,
|
||||
out_count=outcount_label,
|
||||
count_txt_size=self.count_txt_thickness,
|
||||
txt_color=self.count_txt_color,
|
||||
color=self.count_color)
|
||||
|
||||
im0_with_heatmap = cv2.addWeighted(self.im0, 1 - self.heatmap_alpha, heatmap_colored, self.heatmap_alpha, 0)
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ from ultralytics.utils.plotting import Annotator, colors
|
|||
|
||||
check_requirements('shapely>=2.0.0')
|
||||
|
||||
from shapely.geometry import Polygon
|
||||
from shapely.geometry.point import Point
|
||||
from shapely.geometry import LineString, Point, Polygon
|
||||
|
||||
|
||||
class ObjectCounter:
|
||||
|
|
@ -23,10 +22,12 @@ class ObjectCounter:
|
|||
self.is_drawing = False
|
||||
self.selected_point = None
|
||||
|
||||
# Region Information
|
||||
self.reg_pts = None
|
||||
# Region & Line Information
|
||||
self.reg_pts = [(20, 400), (1260, 400)]
|
||||
self.line_dist_thresh = 15
|
||||
self.counting_region = None
|
||||
self.region_color = (255, 255, 255)
|
||||
self.region_color = (255, 0, 255)
|
||||
self.region_thickness = 5
|
||||
|
||||
# Image and annotation Information
|
||||
self.im0 = None
|
||||
|
|
@ -40,11 +41,15 @@ class ObjectCounter:
|
|||
self.in_counts = 0
|
||||
self.out_counts = 0
|
||||
self.counting_list = []
|
||||
self.count_txt_thickness = 0
|
||||
self.count_txt_color = (0, 0, 0)
|
||||
self.count_color = (255, 255, 255)
|
||||
|
||||
# Tracks info
|
||||
self.track_history = defaultdict(list)
|
||||
self.track_thickness = 2
|
||||
self.draw_tracks = False
|
||||
self.track_color = (0, 255, 0)
|
||||
|
||||
# Check if environment support imshow
|
||||
self.env_check = check_imshow(warn=True)
|
||||
|
|
@ -52,11 +57,17 @@ class ObjectCounter:
|
|||
def set_args(self,
|
||||
classes_names,
|
||||
reg_pts,
|
||||
region_color=None,
|
||||
count_reg_color=(255, 0, 255),
|
||||
line_thickness=2,
|
||||
track_thickness=2,
|
||||
view_img=False,
|
||||
draw_tracks=False):
|
||||
draw_tracks=False,
|
||||
count_txt_thickness=2,
|
||||
count_txt_color=(0, 0, 0),
|
||||
count_color=(255, 255, 255),
|
||||
track_color=(0, 255, 0),
|
||||
region_thickness=5,
|
||||
line_dist_thresh=15):
|
||||
"""
|
||||
Configures the Counter's image, bounding box line thickness, and counting region points.
|
||||
|
||||
|
|
@ -65,18 +76,43 @@ class ObjectCounter:
|
|||
view_img (bool): Flag to control whether to display the video stream.
|
||||
reg_pts (list): Initial list of points defining the counting region.
|
||||
classes_names (dict): Classes names
|
||||
region_color (tuple): color for region line
|
||||
track_thickness (int): Track thickness
|
||||
draw_tracks (Bool): draw tracks
|
||||
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
|
||||
count_reg_color (RGB color): Color of object counting region
|
||||
track_color (RGB color): color for tracks
|
||||
region_thickness (int): Object counting Region thickness
|
||||
line_dist_thresh (int): Euclidean Distance threshold for line counter
|
||||
"""
|
||||
self.tf = line_thickness
|
||||
self.view_img = view_img
|
||||
self.track_thickness = track_thickness
|
||||
self.draw_tracks = draw_tracks
|
||||
self.reg_pts = reg_pts
|
||||
self.counting_region = Polygon(self.reg_pts)
|
||||
|
||||
# Region and line selection
|
||||
if len(reg_pts) == 2:
|
||||
print('Line Counter Initiated.')
|
||||
self.reg_pts = reg_pts
|
||||
self.counting_region = LineString(self.reg_pts)
|
||||
elif len(reg_pts) == 4:
|
||||
print('Region Counter Initiated.')
|
||||
self.reg_pts = reg_pts
|
||||
self.counting_region = Polygon(self.reg_pts)
|
||||
else:
|
||||
print('Invalid Region points provided, region_points can be 2 or 4')
|
||||
print('Using Line Counter Now')
|
||||
self.counting_region = LineString(self.reg_pts)
|
||||
|
||||
self.names = classes_names
|
||||
self.region_color = region_color if region_color else self.region_color
|
||||
self.track_color = track_color
|
||||
self.count_txt_thickness = count_txt_thickness
|
||||
self.count_txt_color = count_txt_color
|
||||
self.count_color = count_color
|
||||
self.region_color = count_reg_color
|
||||
self.region_thickness = region_thickness
|
||||
self.line_dist_thresh = line_dist_thresh
|
||||
|
||||
def mouse_event_for_region(self, event, x, y, flags, params):
|
||||
"""
|
||||
|
|
@ -113,11 +149,14 @@ class ObjectCounter:
|
|||
clss = tracks[0].boxes.cls.cpu().tolist()
|
||||
track_ids = tracks[0].boxes.id.int().cpu().tolist()
|
||||
|
||||
# Annotator Init and region drawing
|
||||
self.annotator = Annotator(self.im0, self.tf, self.names)
|
||||
self.annotator.draw_region(reg_pts=self.reg_pts, color=(0, 255, 0))
|
||||
self.annotator.draw_region(reg_pts=self.reg_pts, color=self.region_color, thickness=self.region_thickness)
|
||||
|
||||
# Extract tracks
|
||||
for box, track_id, cls in zip(boxes, track_ids, clss):
|
||||
self.annotator.box_label(box, label=self.names[cls], color=colors(int(cls), True)) # Draw bounding box
|
||||
self.annotator.box_label(box, label=str(track_id) + ':' + self.names[cls],
|
||||
color=colors(int(cls), True)) # Draw bounding box
|
||||
|
||||
# Draw Tracks
|
||||
track_line = self.track_history[track_id]
|
||||
|
|
@ -125,28 +164,45 @@ class ObjectCounter:
|
|||
if len(track_line) > 30:
|
||||
track_line.pop(0)
|
||||
|
||||
# Draw track trails
|
||||
if self.draw_tracks:
|
||||
self.annotator.draw_centroid_and_tracks(track_line,
|
||||
color=(0, 255, 0),
|
||||
color=self.track_color,
|
||||
track_thickness=self.track_thickness)
|
||||
|
||||
# Count objects
|
||||
if self.counting_region.contains(Point(track_line[-1])):
|
||||
if 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
|
||||
if len(self.reg_pts) == 4:
|
||||
if self.counting_region.contains(Point(track_line[-1])):
|
||||
if 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
|
||||
|
||||
incount_label = 'InCount : ' + f'{self.in_counts}'
|
||||
elif len(self.reg_pts) == 2:
|
||||
distance = Point(track_line[-1]).distance(self.counting_region)
|
||||
if distance < self.line_dist_thresh:
|
||||
if 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
|
||||
|
||||
incount_label = 'In Count : ' + f'{self.in_counts}'
|
||||
outcount_label = 'OutCount : ' + f'{self.out_counts}'
|
||||
self.annotator.count_labels(in_count=incount_label, out_count=outcount_label)
|
||||
self.annotator.count_labels(in_count=incount_label,
|
||||
out_count=outcount_label,
|
||||
count_txt_size=self.count_txt_thickness,
|
||||
txt_color=self.count_txt_color,
|
||||
color=self.count_color)
|
||||
|
||||
if self.env_check and self.view_img:
|
||||
cv2.namedWindow('Ultralytics YOLOv8 Object Counter')
|
||||
cv2.setMouseCallback('Ultralytics YOLOv8 Object Counter', self.mouse_event_for_region,
|
||||
{'region_points': self.reg_pts})
|
||||
if len(self.reg_pts) == 4: # only add mouse event If user drawn region
|
||||
cv2.setMouseCallback('Ultralytics YOLOv8 Object Counter', self.mouse_event_for_region,
|
||||
{'region_points': self.reg_pts})
|
||||
cv2.imshow('Ultralytics YOLOv8 Object Counter', self.im0)
|
||||
# Break Window
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
|
|
@ -161,6 +217,7 @@ class ObjectCounter:
|
|||
tracks (list): List of tracks obtained from the object tracking process.
|
||||
"""
|
||||
self.im0 = im0 # store image
|
||||
|
||||
if tracks[0].boxes.id is None:
|
||||
return
|
||||
self.extract_and_process_tracks(tracks)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue