Pipeline
Source code
Python
PythonGitHub
1import depthai as dai
2import cv2
3import numpy as np
4
5def draw_rotated_rectangle(frame, center, size, angle, color, thickness=2):
6 """
7 Draws a rotated rectangle on the given frame.
8
9 Args:
10 frame (numpy.ndarray): The image/frame to draw on.
11 center (tuple): The (x, y) coordinates of the rectangle's center.
12 size (tuple): The (width, height) of the rectangle.
13 angle (float): The rotation angle of the rectangle in degrees (counter-clockwise).
14 color (tuple): The color of the rectangle in BGR format (e.g., (0, 255, 0) for green).
15 thickness (int): The thickness of the rectangle edges. Default is 2.
16 """
17 # Create a rotated rectangle
18 rect = ((center[0], center[1]), (size[0], size[1]), angle)
19
20 # Get the four vertices of the rotated rectangle
21 box = cv2.boxPoints(rect)
22 box = np.intp(box) # Convert to integer coordinates
23
24 # Draw the rectangle on the frame
25 cv2.polylines(frame, [box], isClosed=True, color=color, thickness=thickness)
26
27with dai.Pipeline() as pipeline:
28 cam = pipeline.create(dai.node.Camera).build()
29 camOut = cam.requestOutput((640, 400), dai.ImgFrame.Type.BGR888i, fps = 30.0)
30 manip1 = pipeline.create(dai.node.ImageManip)
31 manip2 = pipeline.create(dai.node.ImageManip)
32
33 # GPU is not available on RVC2 and some RVC4 devices
34 backend = dai.node.ImageManip.Backend.GPU if pipeline.getDefaultDevice().hasGPU() else dai.node.ImageManip.Backend.CPU
35 manip1.setBackend(backend)
36 manip2.setBackend(backend)
37
38 camOut.link(manip1.inputImage)
39 manip1.out.link(manip2.inputImage)
40
41 manip1.initialConfig.addRotateDeg(90)
42 manip1.initialConfig.setOutputSize(200, 320)
43
44 manip2.initialConfig.addRotateDeg(90)
45 manip2.initialConfig.setOutputSize(320, 200)
46
47 outQcam = camOut.createOutputQueue()
48 outQ1 = manip1.out.createOutputQueue()
49 outQ2 = manip2.out.createOutputQueue()
50
51 pipeline.start()
52
53 while True:
54 camFrame: dai.ImgFrame = outQcam.get()
55 manip1Frame: dai.ImgFrame = outQ1.get()
56 manip2Frame: dai.ImgFrame = outQ2.get()
57
58 camCv = camFrame.getCvFrame()
59 manip1Cv = manip1Frame.getCvFrame()
60 manip2Cv = manip2Frame.getCvFrame()
61
62 rect2 = dai.RotatedRect(dai.Rect(dai.Point2f(100, 100), dai.Point2f(200, 150)), 0)
63 rect1 = manip2Frame.getTransformation().remapRectTo(manip1Frame.getTransformation(), rect2)
64 rectcam = manip1Frame.getTransformation().remapRectTo(camFrame.getTransformation(), rect1)
65
66 draw_rotated_rectangle(manip2Cv, (rect2.center.x, rect2.center.y), (rect2.size.width, rect2.size.height), rect2.angle, (255, 0, 0))
67 draw_rotated_rectangle(manip1Cv, (rect1.center.x, rect1.center.y), (rect1.size.width, rect1.size.height), rect1.angle, (255, 0, 0))
68 draw_rotated_rectangle(camCv, (rectcam.center.x, rectcam.center.y), (rectcam.size.width, rectcam.size.height), rectcam.angle, (255, 0, 0))
69
70 cv2.imshow("cam", camCv)
71 cv2.imshow("manip1", manip1Cv)
72 cv2.imshow("manip2", manip2Cv)
73 if cv2.waitKey(1) == ord('q'):
74 break需要帮助?
请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。