|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | | -from pathlib import Path |
4 | 3 | import cv2 |
5 | 4 | import depthai as dai |
6 | | -import numpy as np |
7 | 5 | import time |
8 | | -import argparse |
9 | 6 |
|
10 | | -labelMap = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", |
11 | | - "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] |
12 | 7 |
|
13 | | -nnPathDefault = str((Path(__file__).parent / Path('../../models/mobilenet-ssd_openvino_2021.4_5shave.blob')).resolve().absolute()) |
14 | | -parser = argparse.ArgumentParser() |
15 | | -parser.add_argument('nnPath', nargs='?', help="Path to mobilenet detection network blob", default=nnPathDefault) |
16 | | -parser.add_argument('-ff', '--full_frame', action="store_true", help="Perform tracking on full RGB frame", default=False) |
17 | | - |
18 | | -args = parser.parse_args() |
19 | | - |
20 | | -fullFrameTracking = args.full_frame |
| 8 | +fullFrameTracking = False |
21 | 9 |
|
22 | 10 | # Create pipeline |
23 | 11 | with dai.Pipeline() as pipeline: |
24 | 12 | # Define sources and outputs |
25 | | - camRgb = pipeline.create(dai.node.ColorCamera) |
26 | | - spatialDetectionNetwork = pipeline.create(dai.node.MobileNetSpatialDetectionNetwork) |
27 | | - monoLeft = pipeline.create(dai.node.MonoCamera) |
28 | | - monoRight = pipeline.create(dai.node.MonoCamera) |
29 | | - stereo = pipeline.create(dai.node.StereoDepth) |
30 | | - objectTracker = pipeline.create(dai.node.ObjectTracker) |
31 | | - |
32 | | - # Properties |
33 | | - camRgb.setPreviewSize(300, 300) |
34 | | - camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P) |
35 | | - camRgb.setInterleaved(False) |
36 | | - camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR) |
| 13 | + camRgb = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A) |
| 14 | + monoLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B) |
| 15 | + monoRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C) |
37 | 16 |
|
38 | | - monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
39 | | - monoLeft.setCamera("left") |
40 | | - monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
41 | | - monoRight.setCamera("right") |
| 17 | + stereo = pipeline.create(dai.node.StereoDepth) |
| 18 | + leftOutput = monoLeft.requestOutput((640, 400)) |
| 19 | + rightOutput = monoRight.requestOutput((640, 400)) |
| 20 | + leftOutput.link(stereo.left) |
| 21 | + rightOutput.link(stereo.right) |
42 | 22 |
|
43 | | - # setting node configs |
44 | | - # Align depth map to the perspective of RGB camera, on which inference is done |
45 | | - stereo.setDepthAlign(dai.CameraBoardSocket.CAM_A) |
46 | | - stereo.setOutputSize(monoLeft.getResolutionWidth(), monoLeft.getResolutionHeight()) |
| 23 | + spatialDetectionNetwork = pipeline.create(dai.node.SpatialDetectionNetwork).build(camRgb, stereo, "yolov6-nano") |
| 24 | + objectTracker = pipeline.create(dai.node.ObjectTracker) |
47 | 25 |
|
48 | | - spatialDetectionNetwork.setBlobPath(args.nnPath) |
49 | 26 | spatialDetectionNetwork.setConfidenceThreshold(0.5) |
50 | 27 | spatialDetectionNetwork.input.setBlocking(False) |
51 | 28 | spatialDetectionNetwork.setBoundingBoxScaleFactor(0.5) |
52 | 29 | spatialDetectionNetwork.setDepthLowerThreshold(100) |
53 | 30 | spatialDetectionNetwork.setDepthUpperThreshold(5000) |
| 31 | + labelMap = spatialDetectionNetwork.getClasses() |
54 | 32 |
|
55 | | - objectTracker.setDetectionLabelsToTrack([15]) # track only person |
| 33 | + objectTracker.setDetectionLabelsToTrack([0]) # track only person |
56 | 34 | # possible tracking types: ZERO_TERM_COLOR_HISTOGRAM, ZERO_TERM_IMAGELESS, SHORT_TERM_IMAGELESS, SHORT_TERM_KCF |
57 | 35 | objectTracker.setTrackerType(dai.TrackerType.ZERO_TERM_COLOR_HISTOGRAM) |
58 | 36 | # take the smallest ID when new object is tracked, possible options: SMALLEST_ID, UNIQUE_ID |
59 | 37 | objectTracker.setTrackerIdAssignmentPolicy(dai.TrackerIdAssignmentPolicy.SMALLEST_ID) |
60 | 38 |
|
61 | | - # Linking |
62 | | - monoLeft.out.link(stereo.left) |
63 | | - monoRight.out.link(stereo.right) |
64 | | - |
65 | | - camRgb.preview.link(spatialDetectionNetwork.input) |
66 | 39 | preview = objectTracker.passthroughTrackerFrame.createOutputQueue() |
67 | 40 | tracklets = objectTracker.out.createOutputQueue() |
68 | 41 |
|
69 | 42 | if fullFrameTracking: |
70 | | - camRgb.setPreviewKeepAspectRatio(False) |
71 | | - camRgb.video.link(objectTracker.inputTrackerFrame) |
72 | | - objectTracker.inputTrackerFrame.setBlocking(False) |
| 43 | + camRgb.requestFullResolutionOutput().link(objectTracker.inputTrackerFrame) |
73 | 44 | # do not block the pipeline if it's too slow on full frame |
74 | | - objectTracker.inputTrackerFrame.setQueueSize(2) |
| 45 | + objectTracker.inputTrackerFrame.setBlocking(False) |
| 46 | + objectTracker.inputTrackerFrame.setMaxSize(1) |
75 | 47 | else: |
76 | 48 | spatialDetectionNetwork.passthrough.link(objectTracker.inputTrackerFrame) |
77 | 49 |
|
78 | 50 | spatialDetectionNetwork.passthrough.link(objectTracker.inputDetectionFrame) |
79 | 51 | spatialDetectionNetwork.out.link(objectTracker.inputDetections) |
80 | | - stereo.depth.link(spatialDetectionNetwork.inputDepth) |
81 | 52 |
|
82 | 53 | startTime = time.monotonic() |
83 | 54 | counter = 0 |
|
87 | 58 | while(pipeline.isRunning()): |
88 | 59 | imgFrame = preview.get() |
89 | 60 | track = tracklets.get() |
| 61 | + assert isinstance(imgFrame, dai.ImgFrame), "Expected ImgFrame" |
| 62 | + assert isinstance(track, dai.Tracklets), "Expected Tracklets" |
90 | 63 |
|
91 | 64 | counter+=1 |
92 | 65 | current_time = time.monotonic() |
|
0 commit comments