|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +from depthai import TrackerType, TrackerIdAssignmentPolicy, Tracklet |
| 4 | + |
| 5 | +from depthai_sdk import OakCamera, BboxStyle, TextPosition |
| 6 | + |
| 7 | +tracked_objects = {} |
| 8 | +counter = {'up': 0, 'down': 0, 'left': 0, 'right': 0} |
| 9 | + |
| 10 | +ROI_POS = 0.5 |
| 11 | +AXIS = 1 |
| 12 | + |
| 13 | + |
| 14 | +class TrackableObject: |
| 15 | + def __init__(self, objectID, centroid): |
| 16 | + # store the object ID, then initialize a list of centroids |
| 17 | + # using the current centroid |
| 18 | + self.objectID = objectID |
| 19 | + self.centroids = [centroid] |
| 20 | + |
| 21 | + # initialize a boolean used to indicate if the object has |
| 22 | + # already been counted or not |
| 23 | + self.counted = False |
| 24 | + |
| 25 | + |
| 26 | +def get_centroid(roi): |
| 27 | + x1 = roi.topLeft().x |
| 28 | + y1 = roi.topLeft().y |
| 29 | + x2 = roi.bottomRight().x |
| 30 | + y2 = roi.bottomRight().y |
| 31 | + return ((x2 - x1) / 2 + x1, (y2 - y1) / 2 + y1) |
| 32 | + |
| 33 | + |
| 34 | +def callback(packet, visualizer): |
| 35 | + height, width = packet.frame.shape[:2] |
| 36 | + |
| 37 | + for t in packet.daiTracklets.tracklets: |
| 38 | + to = tracked_objects.get(t.id, None) |
| 39 | + |
| 40 | + # calculate centroid |
| 41 | + roi = t.roi.denormalize(width, height) |
| 42 | + x1 = int(roi.topLeft().x) |
| 43 | + y1 = int(roi.topLeft().y) |
| 44 | + x2 = int(roi.bottomRight().x) |
| 45 | + y2 = int(roi.bottomRight().y) |
| 46 | + centroid = (int((x2 - x1) / 2 + x1), int((y2 - y1) / 2 + y1)) |
| 47 | + |
| 48 | + # If new tracklet, save its centroid |
| 49 | + if t.status == Tracklet.TrackingStatus.NEW: |
| 50 | + to = TrackableObject(t.id, centroid) |
| 51 | + elif to is not None and not to.counted: |
| 52 | + if AXIS == 0: |
| 53 | + x = [c[0] for c in to.centroids] |
| 54 | + direction = centroid[0] - np.mean(x) |
| 55 | + |
| 56 | + if centroid[0] > ROI_POS * width and direction > 0 and np.mean(x) < ROI_POS * width: |
| 57 | + counter['right'] += 1 |
| 58 | + to.counted = True |
| 59 | + elif centroid[0] < ROI_POS * width and direction < 0 and np.mean(x) > ROI_POS * width: |
| 60 | + counter['left'] += 1 |
| 61 | + to.counted = True |
| 62 | + |
| 63 | + elif AXIS == 1: |
| 64 | + y = [c[1] for c in to.centroids] |
| 65 | + direction = centroid[1] - np.mean(y) |
| 66 | + |
| 67 | + if centroid[1] > ROI_POS * height and direction > 0 and np.mean(y) < ROI_POS * height: |
| 68 | + counter['down'] += 1 |
| 69 | + to.counted = True |
| 70 | + elif centroid[1] < ROI_POS * height and direction < 0 and np.mean(y) > ROI_POS * height: |
| 71 | + counter['up'] += 1 |
| 72 | + to.counted = True |
| 73 | + |
| 74 | + to.centroids.append(centroid) |
| 75 | + |
| 76 | + tracked_objects[t.id] = to |
| 77 | + |
| 78 | + if t.status != Tracklet.TrackingStatus.LOST and t.status != Tracklet.TrackingStatus.REMOVED: |
| 79 | + text = 'ID {}'.format(t.id) |
| 80 | + |
| 81 | + cv2.putText(packet.frame, text, (centroid[0] - 10, centroid[1] - 10), |
| 82 | + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) |
| 83 | + cv2.circle(packet.frame, (centroid[0], centroid[1]), 4, (255, 255, 255), -1) |
| 84 | + |
| 85 | + counter_str = f'Up: {counter["up"]}\nDown: {counter["down"]}\n' \ |
| 86 | + if AXIS == 1 \ |
| 87 | + else f'Left: {counter["left"]}\nRight: {counter["right"]}' |
| 88 | + |
| 89 | + visualizer.add_line(pt1=(0, int(ROI_POS * height)), |
| 90 | + pt2=(width, int(ROI_POS * height)), |
| 91 | + color=(255, 255, 255), |
| 92 | + thickness=2) |
| 93 | + |
| 94 | + visualizer.add_text(counter_str, position=TextPosition.BOTTOM_LEFT) |
| 95 | + frame = visualizer.draw(packet.frame) |
| 96 | + |
| 97 | + cv2.imshow('People tracking', frame) |
| 98 | + |
| 99 | + |
| 100 | +with OakCamera(replay='../demo/example_01.mp4') as oak: |
| 101 | + color = oak.create_camera('color') |
| 102 | + nn = oak.create_nn('mobilenet-ssd', color, nn_type='mobilenet', tracker=True) |
| 103 | + |
| 104 | + nn.config_tracker(tracker_type=TrackerType.ZERO_TERM_COLOR_HISTOGRAM, |
| 105 | + assignment_policy=TrackerIdAssignmentPolicy.SMALLEST_ID) |
| 106 | + |
| 107 | + visualizer = oak.visualize(nn.out.tracker, callback=callback, fps=True) |
| 108 | + visualizer.detections( |
| 109 | + hide_label=True, |
| 110 | + bbox_style=BboxStyle.ROUNDED_RECTANGLE |
| 111 | + ) |
| 112 | + |
| 113 | + oak.start(blocking=True) |
0 commit comments