Hi Guy
Total noob here and working my way through the examples. I am trying to infer on a video sent to the device as I cant mount my OAK-D on my car in the rain. I have seen a example that uses mobilenet but I want to do it with Yolo4. I have managed to piece this python script together but I cant seem to get the oak-d do show the inferencing. Does anyboy have some advice what I might have done wrong.
I apreciate any help
Regards Robby
`from depthai_sdk import Previews, FPSHandler
from depthai_sdk.managers import PipelineManager, PreviewManager, BlobManager, NNetManager
import depthai as dai
import cv2
import argparse
from pathlib import Path
import numpy as np
from time import monotonic
parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model", help="Provide model path for inference",
default='models/yolo_v3_tiny_openvino_2021.3_6shave.blob', type=str)
parser.add_argument("-c", "--config", help="Provide config path for inference",
default='yolo-tiny.json', type=str)
parser.add_argument("--height", help="Input shape height", default=320, type=int)
parser.add_argument("--width", help="Input shape width", default=512, type=int)
args = parser.parse_args()
parentDir = Path(file).parent
videoPath = str((parentDir / Path('car.mp4')).resolve().absolute())
CONFIG_PATH = args.config
H, W = args.height, args.width
create pipeline manager and camera
pm = PipelineManager()
pm.createColorCam(previewSize=(W, H), xout=True)
create yolo node
bm = BlobManager(blobPath=args.model)
nm = NNetManager(inputSize=(W, H), nnFamily="YOLO")
nm.readConfig(CONFIG_PATH)
nn = nm.createNN(pipeline=pm.pipeline, nodes=pm.nodes, source=Previews.color.name,
blobPath=bm.getBlob(shaves=6, openvinoVersion=pm.pipeline.getOpenVINOVersion()))
pm.addNn(nn)
Define sources and outputs
xinFrame = pm.pipeline.create(dai.node.XLinkIn)
nnOut = pm.pipeline.create(dai.node.XLinkOut)
xinFrame.setStreamName("inFrame")
nnOut.setStreamName("nn")
Linking
xinFrame.out.link(nn.input)
nn.out.link(nnOut.input)
initialize pipeline
with dai.Device(pm.pipeline) as device:
# Input queue will be used to send video frames to the device.
qIn = device.getInputQueue(name="inFrame")
# Output queue will be used to get nn data from the video frames.
qDet = device.getOutputQueue(name="nn", maxSize=4, blocking=False)
frame = None
detections = []
# nn data, being the 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 to_planar(arr: np.ndarray, shape: tuple) -> np.ndarray:
return cv2.resize(arr, shape).transpose(2, 0, 1).flatten()
def displayFrame(name, frame):
for detection in detections:
if detection.label == 7:
bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax))
cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2)
# Show the frame
cv2.imshow(name, frame)
cap = cv2.VideoCapture(videoPath)
while cap.isOpened():
read_correctly, frame = cap.read()
if not read_correctly:
break
img = dai.ImgFrame()
img.setData(to_planar(frame, (512, 320)))
img.setTimestamp(monotonic())
img.setWidth(512)
img.setHeight(320)
qIn.send(img)
inDet = qDet.tryGet()
if inDet is not None:
detections = inDet.detections
if frame is not None:
displayFrame("rgb", frame)
if cv2.waitKey(1) == ord('q'):
break
`