流水线
源代码
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 labelMap = detectionNetwork.getClasses()
13 depth = pipeline.create(dai.node.Depth).build(dai.node.Depth.Algorithm.AUTO, None)
14
15 qRgb = detectionNetwork.passthrough.createOutputQueue()
16 qDet = detectionNetwork.out.createOutputQueue()
17 qDepth = depth.depth.createOutputQueue()
18
19 pipeline.start()
20
21 def displayFrame(name: str, frame: dai.ImgFrame, imgDetections: dai.ImgDetections):
22 color = (0, 255, 0)
23 assert imgDetections.getTransformation() is not None
24 if(frame.getType() == dai.ImgFrame.Type.RAW16):
25 cvFrame = dai.utility.colorizeDepthFrame(frame).getCvFrame()
26 else:
27 cvFrame = frame.getCvFrame()
28 for detection in imgDetections.detections:
29 # Get the shape of the frame from which the detections originated for denormalization
30 normShape = imgDetections.getTransformation().getSize()
31
32 # Create rotated rectangle to remap
33 # Here we use an intermediate dai.Rect to create a dai.RotatedRect to simplify construction and denormalization
34 rotRect = dai.RotatedRect(dai.Rect(dai.Point2f(detection.xmin, detection.ymin), dai.Point2f(detection.xmax, detection.ymax)).denormalize(normShape[0], normShape[1]), 0)
35 # Remap the detection rectangle to target frame
36 remapped = imgDetections.getTransformation().remapRectTo(frame.getTransformation(), rotRect)
37 # Remapped rectangle could be rotated, so we get the bounding box
38 bbox = [int(l) for l in remapped.getOuterRect()]
39 cv2.putText(
40 cvFrame,
41 labelMap[detection.label],
42 (bbox[0] + 10, bbox[1] + 20),
43 cv2.FONT_HERSHEY_TRIPLEX,
44 0.5,
45 255,
46 )
47 cv2.putText(
48 cvFrame,
49 f"{int(detection.confidence * 100)}%",
50 (bbox[0] + 10, bbox[1] + 40),
51 cv2.FONT_HERSHEY_TRIPLEX,
52 0.5,
53 255,
54 )
55 cv2.rectangle(cvFrame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
56 # Show the frame
57 cv2.imshow(name, cvFrame)
58
59 while pipeline.isRunning():
60 inRgb: dai.ImgFrame = qRgb.get()
61 inDet: dai.ImgDetections = qDet.get()
62 inDepth: dai.ImgFrame = qDepth.get()
63 hasRgb = inRgb is not None
64 hasDepth = inDepth is not None
65 hasDet = inDet is not None
66 if hasRgb:
67 displayFrame("rgb", inRgb, inDet)
68 if hasDepth:
69 displayFrame("depth", inDepth, inDet)
70 if cv2.waitKey(1) == ord("q"):
71 pipeline.stop()
72 break需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。