Skip to content

Instantly share code, notes, and snippets.

@Erol444
Created August 5, 2021 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erol444/abb7258a583dca581737ffc6285341eb to your computer and use it in GitHub Desktop.
Save Erol444/abb7258a583dca581737ffc6285341eb to your computer and use it in GitHub Desktop.
DepthAI running mobilenet on rotated rgb frames
#!/usr/bin/env python3
import cv2
import depthai as dai
from pathlib import Path
import numpy as np
nnPathDefault = str((Path(__file__).parent / Path('models/mobilenet-ssd_openvino_2021.2_6shave.blob')).resolve().absolute())
# MobilenetSSD label texts
labelMap = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow",
"diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
# Create pipeline
pipeline = dai.Pipeline()
# Rotate color frames
camRgb = pipeline.createColorCamera()
camRgb.setPreviewSize(640, 400)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
camRgb.setInterleaved(False)
manipRgb = pipeline.createImageManip()
rgbRr = dai.RotatedRect()
rgbRr.center.x, rgbRr.center.y = camRgb.getPreviewWidth() // 2, camRgb.getPreviewHeight() // 2
rgbRr.size.width, rgbRr.size.height = camRgb.getPreviewHeight(), camRgb.getPreviewWidth()
rgbRr.angle = 90
manipRgb.initialConfig.setCropRotatedRect(rgbRr, False)
camRgb.preview.link(manipRgb.inputImage)
cropManip = pipeline.createImageManip()
cropManip.initialConfig.setResize(300, 300)
manipRgb.out.link(cropManip.inputImage)
manipRgbOut = pipeline.createXLinkOut()
manipRgbOut.setStreamName("manip_rgb")
cropManip.out.link(manipRgbOut.input)
nn = pipeline.createMobileNetDetectionNetwork()
# Define a neural network that will make predictions based on the source frames
nn.setConfidenceThreshold(0.5)
nn.setBlobPath(nnPathDefault)
nn.input.setBlocking(False)
cropManip.out.link(nn.input)
nnOut = pipeline.createXLinkOut()
nnOut.setStreamName("det")
nn.out.link(nnOut.input)
with dai.Device(pipeline) as device:
qRgb = device.getOutputQueue(name="manip_rgb", maxSize=8, blocking=False)
qDet = device.getOutputQueue(name="det", maxSize=8, blocking=False)
detections = []
# nn data (bounding box locations) are in <0..1> range - they need to be normalized with frame width/height
def frameNorm(frame, bbox):
normVals = np.full(len(bbox), frame.shape[0])
normVals[::2] = frame.shape[1]
return (np.clip(np.array(bbox), 0, 1) * normVals).astype(int)
def displayFrame(name, frame):
color = (255, 0, 0)
for detection in detections:
bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax))
cv2.putText(frame, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color)
cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color)
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
# Show the frame
cv2.imshow(name, frame)
while True:
inDet = qDet.tryGet()
if inDet is not None:
detections = inDet.detections
inRgb = qRgb.tryGet()
if inRgb is not None:
frame = inRgb.getCvFrame()
displayFrame("Color rotated", frame)
if cv2.waitKey(1) == ord('q'):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment