Hello @jakaskerl
The example code really helps me a lot ,Thank you,After modify my mode,It works,but the output frame is continuously change frame size from 320x320 to much bigger frame size and to 320x320,I don't know where I setting wrong,Can you help me check my code.
import cv2
import depthai as dai
import numpy as np
model_weights_path = "C:/catdog320.blob"
custom_model_blob = dai.OpenVINO.Blob(model_weights_path)
numClasses = 80
dim = next(iter(custom_model_blob.networkInputs.values())).dims
output_name, output_tenser = next(iter(custom_model_blob.networkOutputs.items()))
if "yolov6" in output_name:
numClasses = output_tenser.dims[2] - 5
else:
numClasses = output_tenser.dims[2] // 3 - 5
labelMap = [ "class_%s" % i
for i in range(numClasses)]
pipeline = dai.Pipeline()
monoL = pipeline.create(dai.node.MonoCamera)
manip = pipeline.create(dai.node.ImageManip)
manipOut = pipeline.create(dai.node.XLinkOut)
detectionNetwork = pipeline.create(dai.node.YoloDetectionNetwork)
monoL.setNumFramesPool(24)
xoutNN = pipeline.create(dai.node.XLinkOut)
manipOut.setStreamName('flood-left')
xoutNN.setStreamName("nn")
# Script node for frame routing and IR dot/flood alternate
script = pipeline.create(dai.node.Script)
script.setProcessor(dai.ProcessorType.LEON_CSS)
script.setScript("""
dotBright = 0.8
floodBright = 0.1
LOGGING = False # Set True for latency/timings debugging
node.warn(f'IR drivers detected: {str(Device.getIrDrivers())}')
flagDot = False
while True: # Wait first for a frame event, received at MIPI start-of-frame event = node.io['event'].get()
if LOGGING: tEvent = Clock.now()
# Immediately reconfigure the IR driver. # Note the logic is inverted, as it applies for next frame
Device.setIrLaserDotProjectorIntensity(0 if flagDot else dotBright)
Device.setIrFloodLightIntensity(floodBright if flagDot else 0)
if LOGGING: tIrSet = Clock.now()
# Wait for the actual frame (after MIPI capture and ISP proc is done)
frameL = node.io['frameL'].get()
if LOGGING: tLeft = Clock.now()
if LOGGING:
latIR = (tIrSet - tEvent ).total_seconds() * 1000
latEv = (tEvent - event.getTimestamp() ).total_seconds() * 1000
latProcL = (tLeft - event.getTimestamp() ).total_seconds() * 1000
node.warn(f'T[ms] latEv:{latEv:5.3f} latIR:{latIR:5.3f} latProcL:{latProcL:6.3f}')
if not flagDot: node.io['floodL'].send(frameL)
flagDot = not flagDot""")
detectionNetwork.setBlob(custom_model_blob)
detectionNetwork.setConfidenceThreshold(0.4)
detectionNetwork.setNumClasses(numClasses)
detectionNetwork.setCoordinateSize(4)
detectionNetwork.setAnchors([])
detectionNetwork.setAnchorMasks({})
detectionNetwork.setIouThreshold(0.5)
detectionNetwork.input.setBlocking(False)
manip.initialConfig.setResize(320, 320)
manip.initialConfig.setFrameType(dai.ImgFrame.Type.BGR888p)
monoL.out.link(manip.inputImage)
manip.out.link(detectionNetwork.input)
manip.out.link(manipOut.input)
detectionNetwork.out.link(xoutNN.input)
monoL.frameEvent.link(script.inputs['event'])
monoL.out.link(script.inputs['frameL'])
script.outputs['floodL'].link(manipOut .input)
# Connect to device and start pipeline
with dai.Device(pipeline) as device:
qRight = device.getOutputQueue("flood-left", maxSize=4, blocking=False)
qDet = device.getOutputQueue("nn", maxSize=4, blocking=False)
frame = None
detections = []
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 drawText(frame, text, org, color=(255, 255, 255), thickness=1):
cv2.putText(frame, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), thickness + 3, cv2.LINE_AA)
cv2.putText(frame, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness, cv2.LINE_AA )
def drawRect(frame, topLeft, bottomRight, color=(255, 255, 255), thickness=1):
cv2.rectangle(frame, topLeft, bottomRight, (0, 0, 0), thickness + 3)
cv2.rectangle(frame, topLeft, bottomRight, color, thickness)
def displayFrame(name, frame):
color = (128, 128, 128)
for detection in detections:
bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax) ) drawText( frame=frame, text=labelMap[detection.label], org=(bbox[0] + 10, bbox[1] + 20), ) drawText(frame=frame, text=f"{detection.confidence:.2%}", org=(bbox[0] + 10, bbox[1] + 35),) drawRect(frame=frame,topLeft=(bbox[0], bbox[1]),bottomRight=(bbox[2], bbox[3]),color=color,) cv2.putText(frame, '77777', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) cv2.imshow(name, frame)
while True:
imageQueueData = qRight.tryGet()
detectQueueData = qDet.tryGet()
if imageQueueData is not None:
frame = imageQueueData.getCvFrame()
if detectQueueData is not None:
detections = detectQueueData.detections
if frame is not None:
displayFrame("rgb", frame)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()
The text format may be wrong. Please understand.
Thanks,
LI