-
Notifications
You must be signed in to change notification settings - Fork 19
/
yolo_tracker_preview.py
227 lines (192 loc) · 11.3 KB
/
yolo_tracker_preview.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
"""Show OAK camera livestream with detection model and object tracker output.
Source: https://github.com/maxsitt/insect-detect
License: GNU GPLv3 (https://choosealicense.com/licenses/gpl-3.0/)
Author: Maximilian Sittinger (https://github.com/maxsitt)
Docs: https://maxsitt.github.io/insect-detect-docs/
- run a custom YOLO object detection model (.blob format) on-device (Luxonis OAK)
-> inference on downscaled + stretched/cropped LQ frames (default: 320x320 px)
- use an object tracker to track detected objects and assign unique tracking IDs
-> accuracy depends on object motion speed and inference speed of the detection model
- show downscaled LQ frames + model/tracker output (bounding box, label, confidence,
tracking ID, tracking status) + FPS in a new window (e.g. via X11 forwarding)
- optional arguments:
'-fov' default: stretch frames to square for model input and visualization ('-fov stretch')
-> FOV is preserved, only aspect ratio of LQ frames is changed (adds distortion)
optional: crop frames to square for model input and visualization ('-fov crop')
-> FOV is reduced due to cropping of LQ frames (no distortion)
'-af' set auto focus range in cm (min - max distance to camera)
-> e.g. '-af 14 20' to restrict auto focus range to 14-20 cm
'-mf' set manual focus position in cm (distance to camera)
-> e.g. '-mf 14' to set manual focus position to 14 cm
'-ae' use bounding box coordinates from detections to set auto exposure region
'-log' print available Raspberry Pi memory (MB), RPi CPU utilization (%) + temperature,
OAK memory + CPU usage and OAK chip temperature at specified interval (default: 1 s)
based on open source scripts available at https://github.com/luxonis
"""
import argparse
import json
import logging
import time
from pathlib import Path
import cv2
import depthai as dai
import numpy as np
from apscheduler.schedulers.background import BackgroundScheduler
from utils.log import print_logs
from utils.oak_cam import convert_bbox_roi, convert_cm_lens_position
# Define optional arguments
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
parser.add_argument("-fov", "--field_of_view", type=str, choices=["stretch", "crop"], default="stretch",
help=("Stretch frames to square and preserve FOV ('stretch') or "
"crop frames to square and reduce FOV ('crop') (default: 'stretch')."))
group.add_argument("-af", "--auto_focus_range", type=int, nargs=2, metavar=("CM_MIN", "CM_MAX"),
help="Set auto focus range in cm (min - max distance to camera).")
group.add_argument("-mf", "--manual_focus", type=int, metavar="CM",
help="Set manual focus position in cm (distance to camera).")
parser.add_argument("-ae", "--auto_exposure_region", action="store_true",
help="Use bounding box coordinates from detections to set auto exposure region.")
parser.add_argument("-log", "--print_logs", action="store_true",
help=("Print RPi available memory (MB), RPi CPU utilization (%%) + temperature, "
"OAK memory + CPU usage and OAK chip temperature."))
args = parser.parse_args()
# Set file paths to the detection model and corresponding config JSON
MODEL_PATH = Path.home() / "insect-detect" / "models" / "yolov5n_320_openvino_2022.1_4shave.blob"
CONFIG_PATH = Path.home() / "insect-detect" / "models" / "json" / "yolov5_v7_320.json"
# Set camera frame rate
FPS = 20 # default: 20 FPS
# Set time interval at which RPi logs are printed if "-log" is used
LOG_INT = 1 # default: 1 second
# Get detection model metadata from config JSON
with CONFIG_PATH.open(encoding="utf-8") as config_json:
config = json.load(config_json)
nn_config = config.get("nn_config", {})
nn_metadata = nn_config.get("NN_specific_metadata", {})
classes = nn_metadata.get("classes", {})
coordinates = nn_metadata.get("coordinates", {})
anchors = nn_metadata.get("anchors", {})
anchor_masks = nn_metadata.get("anchor_masks", {})
iou_threshold = nn_metadata.get("iou_threshold", {})
confidence_threshold = nn_metadata.get("confidence_threshold", {})
nn_mappings = config.get("mappings", {})
labels = nn_mappings.get("labels", {})
# Create depthai pipeline
pipeline = dai.Pipeline()
# Create and configure color camera node
cam_rgb = pipeline.create(dai.node.ColorCamera)
cam_rgb.setFps(FPS) # frames per second available for auto focus/exposure and model input
cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
cam_rgb.setPreviewSize(320, 320) # downscale frames for model input -> LQ frames (1:1)
if args.field_of_view == "stretch":
cam_rgb.setPreviewKeepAspectRatio(False) # stretch LQ frames to square for model input
cam_rgb.setInterleaved(False) # planar layout
cam_rgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
SENSOR_RES = cam_rgb.getResolutionSize()
img_width, img_height = cam_rgb.getPreviewSize()
norm_vals = np.array([img_width, img_height, img_width, img_height]) # used for bbox conversion
if args.auto_focus_range:
# Convert cm to lens position values and set auto focus range
lens_pos_min = convert_cm_lens_position(args.auto_focus_range[1])
lens_pos_max = convert_cm_lens_position(args.auto_focus_range[0])
cam_rgb.initialControl.setAutoFocusLensRange(lens_pos_min, lens_pos_max)
elif args.manual_focus:
# Convert cm to lens position value and set manual focus position
lens_pos = convert_cm_lens_position(args.manual_focus)
cam_rgb.initialControl.setManualFocus(lens_pos)
# Create and configure YOLO detection network node and define input
yolo = pipeline.create(dai.node.YoloDetectionNetwork)
yolo.setBlobPath(MODEL_PATH)
yolo.setNumClasses(classes)
yolo.setCoordinateSize(coordinates)
yolo.setAnchors(anchors)
yolo.setAnchorMasks(anchor_masks)
yolo.setIouThreshold(iou_threshold)
yolo.setConfidenceThreshold(confidence_threshold)
yolo.setNumInferenceThreads(2)
cam_rgb.preview.link(yolo.input) # downscaled + stretched/cropped LQ frames as model input
yolo.input.setBlocking(False) # non-blocking input stream
# Create and configure object tracker node and define inputs
tracker = pipeline.create(dai.node.ObjectTracker)
tracker.setTrackerType(dai.TrackerType.ZERO_TERM_IMAGELESS)
tracker.setTrackerIdAssignmentPolicy(dai.TrackerIdAssignmentPolicy.UNIQUE_ID)
yolo.passthrough.link(tracker.inputTrackerFrame) # passthrough LQ frames as tracker input
yolo.passthrough.link(tracker.inputDetectionFrame)
yolo.out.link(tracker.inputDetections) # detections from YOLO model as tracker input
xout_rgb = pipeline.create(dai.node.XLinkOut)
xout_rgb.setStreamName("frame")
tracker.passthroughTrackerFrame.link(xout_rgb.input) # passthrough LQ frames for visualization
xout_tracker = pipeline.create(dai.node.XLinkOut)
xout_tracker.setStreamName("track")
tracker.out.link(xout_tracker.input) # tracker + model output
if args.auto_exposure_region:
# Create XLinkIn node to send control commands to color camera node
xin_ctrl = pipeline.create(dai.node.XLinkIn)
xin_ctrl.setStreamName("control")
xin_ctrl.out.link(cam_rgb.inputControl)
# Connect to OAK device and start pipeline in USB2 mode
with dai.Device(pipeline, maxUsbSpeed=dai.UsbSpeed.HIGH) as device:
# Create output queues to get the LQ frames and tracker + model output
q_frame = device.getOutputQueue(name="frame", maxSize=4, blocking=False)
q_track = device.getOutputQueue(name="track", maxSize=4, blocking=False)
if args.auto_exposure_region:
# Create input queue to send control commands to OAK camera
q_ctrl = device.getInputQueue(name="control", maxSize=4, blocking=False)
if args.print_logs:
# Print RPi + OAK info at specified interval
logging.getLogger("apscheduler").setLevel(logging.WARNING) # decrease apscheduler logging level
scheduler = BackgroundScheduler()
scheduler.add_job(print_logs, "interval", seconds=LOG_INT, id="log")
scheduler.start()
device.setLogLevel(dai.LogLevel.INFO)
device.setLogOutputLevel(dai.LogLevel.INFO)
# Set start time of recording and create counter to measure FPS
start_time = time.monotonic()
counter = 0
while True:
if q_frame.has():
# Get LQ frame and show in new window together with FPS
frame_lq = q_frame.get().getCvFrame()
counter += 1
fps = round(counter / (time.monotonic() - start_time), 2)
if q_track.has():
# Get tracker output (including passthrough model output)
tracklets = q_track.get().tracklets
for tracklet in tracklets:
# Get bounding box from model output
bbox_norm = (tracklet.srcImgDetection.xmin, tracklet.srcImgDetection.ymin,
tracklet.srcImgDetection.xmax, tracklet.srcImgDetection.ymax) # normalized bounding box
bbox = (np.clip(bbox_norm, 0, 1) * norm_vals).astype(int) # convert to pixel coordinates
# Get bounding box from object tracker
roi = tracklet.roi.denormalize(img_width, img_height) # convert to pixel coordinates
bbox_tracker = (int(roi.topLeft().x), int(roi.topLeft().y),
int(roi.bottomRight().x), int(roi.bottomRight().y))
# Get metadata from tracker + model output
label = labels[tracklet.srcImgDetection.label]
confidence = round(tracklet.srcImgDetection.confidence, 2)
track_id = tracklet.id
tracklet_status = tracklet.status.name
if args.auto_exposure_region and tracklet_status == "TRACKED" and tracklet is tracklets[-1]:
# Use model bbox from latest active tracking ID to set auto exposure region
roi_x, roi_y, roi_w, roi_h = convert_bbox_roi(bbox_norm, SENSOR_RES)
q_ctrl.send(dai.CameraControl().setAutoExposureRegion(roi_x, roi_y, roi_w, roi_h))
cv2.putText(frame_lq, f"{label}", (bbox[0], bbox[3] + 13),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
cv2.putText(frame_lq, f"{confidence}", (bbox[0], bbox[3] + 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
cv2.putText(frame_lq, f"ID:{track_id}", (bbox[0], bbox[3] + 40),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(frame_lq, tracklet_status, (bbox[0], bbox[3] + 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1)
cv2.rectangle(frame_lq, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2)
cv2.rectangle(frame_lq, (bbox_tracker[0], bbox_tracker[1]),
(bbox_tracker[2], bbox_tracker[3]), (0, 255, 130), 1)
cv2.putText(frame_lq, f"FPS: {fps}", (4, img_height - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.imshow("tracker_preview", frame_lq)
#print(f"FPS: {fps}")
# streaming the frames via SSH (X11 forwarding) will slow down FPS
# comment out "cv2.imshow()" and print FPS to console for true FPS
# Stop script and close window by pressing "Q"
if cv2.waitKey(1) == ord("q"):
break