演示

流水线
源代码
Python
PythonGitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6# Create pipeline
7with dai.Pipeline() as pipeline:
8 colorSockets = pipeline.getDefaultDevice().getConnectedCameras(dai.CameraSensorType.COLOR)
9 colorSocket = colorSockets[0] if colorSockets else dai.CameraBoardSocket.CAM_A
10 cameraNode = pipeline.create(dai.node.Camera).build(colorSocket)
11 detectionNetwork = pipeline.create(dai.node.DetectionNetwork).build(cameraNode, dai.NNModelDescription("yolov6-nano"))
12 objectTracker = pipeline.create(dai.node.ObjectTracker)
13 labelMap = detectionNetwork.getClasses()
14 depth = pipeline.create(dai.node.Depth).build(dai.node.Depth.Algorithm.AUTO)
15
16 detectionNetwork.out.link(objectTracker.inputDetections)
17 detectionNetwork.passthrough.link(objectTracker.inputDetectionFrame)
18 detectionNetwork.passthrough.link(objectTracker.inputTrackerFrame)
19
20 qRgb = detectionNetwork.passthrough.createOutputQueue()
21 qTrack = objectTracker.out.createOutputQueue()
22 qDepth = depth.depth.createOutputQueue()
23
24 pipeline.start()
25
26 def displayFrame(name: str, frame: dai.ImgFrame, tracklets: dai.Tracklets):
27 color = (0, 255, 0)
28 assert tracklets.getTransformation() is not None
29 if(frame.getType() == dai.ImgFrame.Type.RAW16):
30 cvFrame = dai.utility.colorizeDepthFrame(frame).getCvFrame()
31 else:
32 cvFrame = frame.getCvFrame()
33 for tracklet in tracklets.tracklets:
34 # Get the shape of the frame from which the detections originated for denormalization
35 normShape = tracklets.getTransformation().getSize()
36
37 # Create rotated rectangle to remap
38 # Here we use an intermediate dai.Rect to create a dai.RotatedRect to simplify construction and denormalization
39 rotRect = dai.RotatedRect(tracklet.roi.denormalize(normShape[0], normShape[1]), 0)
40 # Remap the detection rectangle to target frame
41 remapped = tracklets.getTransformation().remapRectTo(frame.getTransformation(), rotRect)
42 # Remapped rectangle could be rotated, so we get the bounding box
43 bbox = [int(l) for l in remapped.getOuterRect()]
44 cv2.putText(
45 cvFrame,
46 labelMap[tracklet.label],
47 (bbox[0] + 10, bbox[1] + 20),
48 cv2.FONT_HERSHEY_TRIPLEX,
49 0.5,
50 255,
51 )
52 cv2.putText(
53 cvFrame,
54 f"{int(tracklet.srcImgDetection.confidence * 100)}%",
55 (bbox[0] + 10, bbox[1] + 40),
56 cv2.FONT_HERSHEY_TRIPLEX,
57 0.5,
58 255,
59 )
60 cv2.rectangle(cvFrame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
61 # Show the frame
62 cv2.imshow(name, cvFrame)
63
64 while pipeline.isRunning():
65 inRgb: dai.ImgFrame = qRgb.get()
66 inTrack: dai.Tracklets = qTrack.get()
67 inDepth: dai.ImgFrame = qDepth.get()
68 hasRgb = inRgb is not None
69 hasDepth = inDepth is not None
70 hasTrack = inTrack is not None
71 if hasRgb:
72 displayFrame("rgb", inRgb, inTrack)
73 if hasDepth:
74 displayFrame("depth", inDepth, inTrack)
75 if cv2.waitKey(1) == ord("q"):
76 pipeline.stop()
77 break需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。