Fix `tracks=None` bug in YOLOv8-Region-Counting module (#5977)

main
Muhammad Rizwan Munawar 2 years ago committed by GitHub
parent e58db228c2
commit ae6fa2fb02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 58
      examples/YOLOv8-Region-Counter/yolov8_region_counter.py

@ -120,34 +120,36 @@ def run(
# Extract the results # Extract the results
results = model.track(frame, persist=True) results = model.track(frame, persist=True)
boxes = results[0].boxes.xywh.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist() if results[0].boxes.id is not None:
clss = results[0].boxes.cls.cpu().tolist() boxes = results[0].boxes.xywh.cpu()
names = results[0].names track_ids = results[0].boxes.id.int().cpu().tolist()
clss = results[0].boxes.cls.cpu().tolist()
annotator = Annotator(frame, line_width=line_thickness, example=str(names)) names = results[0].names
for box, track_id, cls in zip(boxes, track_ids, clss): annotator = Annotator(frame, line_width=line_thickness, example=str(names))
x, y, w, h = box
label = str(names[cls]) for box, track_id, cls in zip(boxes, track_ids, clss):
xyxy = (x - w / 2), (y - h / 2), (x + w / 2), (y + h / 2) x, y, w, h = box
label = str(names[cls])
# Bounding box plot xyxy = (x - w / 2), (y - h / 2), (x + w / 2), (y + h / 2)
bbox_color = colors(cls, True)
annotator.box_label(xyxy, label, color=bbox_color) # Bounding box plot
bbox_color = colors(cls, True)
# Tracking Lines plot annotator.box_label(xyxy, label, color=bbox_color)
track = track_history[track_id]
track.append((float(x), float(y))) # Tracking Lines plot
if len(track) > 30: track = track_history[track_id]
track.pop(0) track.append((float(x), float(y)))
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2)) if len(track) > 30:
cv2.polylines(frame, [points], isClosed=False, color=bbox_color, thickness=track_thickness) track.pop(0)
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
# Check if detection inside region cv2.polylines(frame, [points], isClosed=False, color=bbox_color, thickness=track_thickness)
for region in counting_regions:
if region['polygon'].contains(Point((x, y))): # Check if detection inside region
region['counts'] += 1 for region in counting_regions:
if region['polygon'].contains(Point((x, y))):
region['counts'] += 1
# Draw regions (Polygons/Rectangles) # Draw regions (Polygons/Rectangles)
for region in counting_regions: for region in counting_regions:

Loading…
Cancel
Save