|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import depthai as dai |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +# Create pipeline |
| 8 | +pipeline = dai.Pipeline() |
| 9 | + |
| 10 | +# Define sources and outputs |
| 11 | +camRgb = pipeline.createColorCamera() |
| 12 | +monoLeft = pipeline.createMonoCamera() |
| 13 | +monoRight = pipeline.createMonoCamera() |
| 14 | + |
| 15 | +edgeDetectorLeft = pipeline.createEdgeDetector() |
| 16 | +edgeDetectorRight = pipeline.createEdgeDetector() |
| 17 | +edgeDetectorRgb = pipeline.createEdgeDetector() |
| 18 | + |
| 19 | +xoutEdgeLeft = pipeline.createXLinkOut() |
| 20 | +xoutEdgeRight = pipeline.createXLinkOut() |
| 21 | +xoutEdgeRgb = pipeline.createXLinkOut() |
| 22 | +xinEdgeCfg = pipeline.createXLinkIn() |
| 23 | + |
| 24 | +edgeLeftStr = "edge left" |
| 25 | +edgeRightStr = "edge right" |
| 26 | +edgeRgbStr = "edge rgb" |
| 27 | +edgeCfgStr = "edge cfg" |
| 28 | + |
| 29 | +xoutEdgeLeft.setStreamName(edgeLeftStr) |
| 30 | +xoutEdgeRight.setStreamName(edgeRightStr) |
| 31 | +xoutEdgeRgb.setStreamName(edgeRgbStr) |
| 32 | +xinEdgeCfg.setStreamName(edgeCfgStr) |
| 33 | + |
| 34 | +# Properties |
| 35 | +camRgb.setBoardSocket(dai.CameraBoardSocket.RGB) |
| 36 | +camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P) |
| 37 | + |
| 38 | +monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
| 39 | +monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT) |
| 40 | +monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
| 41 | +monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT) |
| 42 | + |
| 43 | +edgeDetectorRgb.setMaxOutputFrameSize(camRgb.getVideoWidth() * camRgb.getVideoHeight()) |
| 44 | + |
| 45 | +# Linking |
| 46 | +monoLeft.out.link(edgeDetectorLeft.inputImage) |
| 47 | +monoRight.out.link(edgeDetectorRight.inputImage) |
| 48 | +camRgb.video.link(edgeDetectorRgb.inputImage) |
| 49 | + |
| 50 | +edgeDetectorLeft.outputImage.link(xoutEdgeLeft.input) |
| 51 | +edgeDetectorRight.outputImage.link(xoutEdgeRight.input) |
| 52 | +edgeDetectorRgb.outputImage.link(xoutEdgeRgb.input) |
| 53 | + |
| 54 | +xinEdgeCfg.out.link(edgeDetectorLeft.inputConfig) |
| 55 | +xinEdgeCfg.out.link(edgeDetectorRight.inputConfig) |
| 56 | +xinEdgeCfg.out.link(edgeDetectorRgb.inputConfig) |
| 57 | + |
| 58 | +# Connect to device and start pipeline |
| 59 | +with dai.Device(pipeline) as device: |
| 60 | + |
| 61 | + # Output/input queues |
| 62 | + edgeLeftQueue = device.getOutputQueue(edgeLeftStr, 8, False) |
| 63 | + edgeRightQueue = device.getOutputQueue(edgeRightStr, 8, False) |
| 64 | + edgeRgbQueue = device.getOutputQueue(edgeRgbStr, 8, False) |
| 65 | + edgeCfgQueue = device.getInputQueue(edgeCfgStr) |
| 66 | + |
| 67 | + print("Switch between sobel filter kernels using keys '1' and '2'") |
| 68 | + |
| 69 | + while(True): |
| 70 | + edgeLeft = edgeLeftQueue.get() |
| 71 | + edgeRight = edgeRightQueue.get() |
| 72 | + edgeRgb = edgeRgbQueue.get() |
| 73 | + |
| 74 | + edgeLeftFrame = edgeLeft.getFrame() |
| 75 | + edgeRightFrame = edgeRight.getFrame() |
| 76 | + edgeRgbFrame = edgeRgb.getFrame() |
| 77 | + |
| 78 | + # Show the frame |
| 79 | + cv2.imshow(edgeLeftStr, edgeLeftFrame) |
| 80 | + cv2.imshow(edgeRightStr, edgeRightFrame) |
| 81 | + cv2.imshow(edgeRgbStr, edgeRgbFrame) |
| 82 | + |
| 83 | + key = cv2.waitKey(1) |
| 84 | + if key == ord('q'): |
| 85 | + break |
| 86 | + |
| 87 | + if key == ord('1'): |
| 88 | + print("Switching sobel filter kernel.") |
| 89 | + cfg = dai.EdgeDetectorConfig() |
| 90 | + sobelHorizontalKernel = [[1, 0, -1], [2, 0, -2], [1, 0, -1]] |
| 91 | + sobelVerticalKernel = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]] |
| 92 | + cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel) |
| 93 | + edgeCfgQueue.send(cfg) |
| 94 | + |
| 95 | + if key == ord('2'): |
| 96 | + print("Switching sobel filter kernel.") |
| 97 | + cfg = dai.EdgeDetectorConfig() |
| 98 | + sobelHorizontalKernel = [[3, 0, -3], [10, 0, -10], [3, 0, -3]] |
| 99 | + sobelVerticalKernel = [[3, 10, 3], [0, 0, 0], [-3, -10, -3]] |
| 100 | + cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel) |
| 101 | + edgeCfgQueue.send(cfg) |
| 102 | + |
0 commit comments