From 9fd624b579b15865b36614ea4a27c9938c46e795 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Sun, 11 Apr 2021 12:35:25 +0200 Subject: [PATCH] fixed the depth/right frames, so they're flipped correctly now --- examples/10_mono_depth_mobilenetssd.py | 43 ++++++++++++++++---------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/examples/10_mono_depth_mobilenetssd.py b/examples/10_mono_depth_mobilenetssd.py index 4b1aba93f..e05b973d5 100755 --- a/examples/10_mono_depth_mobilenetssd.py +++ b/examples/10_mono_depth_mobilenetssd.py @@ -6,6 +6,9 @@ import depthai as dai import numpy as np + +flipRectified = True + # Get argument first nnPath = str((Path(__file__).parent / Path('models/mobilenet-ssd_openvino_2021.2_6shave.blob')).resolve().absolute()) if len(sys.argv) > 1: @@ -91,6 +94,16 @@ def frameNorm(frame, bbox): normVals[::2] = frame.shape[1] return (np.clip(np.array(bbox), 0, 1) * normVals).astype(int) + # Add bounding boxes and text to the frame and show it to the user + def show(name, frame): + for detection in detections: + bbox = frameNorm(frame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax)) + cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2) + cv2.putText(frame, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255) + cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255) + # Show the frame + cv2.imshow(name, frame) + while True: # Instead of get (blocking), we use tryGet (nonblocking) which will return the available data or None otherwise inRight = qRight.tryGet() @@ -99,32 +112,30 @@ def frameNorm(frame, bbox): if inRight is not None: rightFrame = inRight.getCvFrame() + if flipRectified: + rightFrame = cv2.flip(rightFrame, 1) + if inDet is not None: detections = inDet.detections + if flipRectified: + for detection in detections: + swap = detection.xmin + detection.xmin = 1 - detection.xmax + detection.xmax = 1 - swap if inDepth is not None: # Frame is transformed, the color map will be applied to highlight the depth info - depthFrame = cv2.flip(inDepth.getFrame(), 1) # Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html - depthFrame = cv2.applyColorMap(depthFrame, cv2.COLORMAP_JET) + depthFrame = cv2.applyColorMap(inDepth.getFrame(), cv2.COLORMAP_JET) + + if depthFrame is not None: + show("depth", depthFrame) if rightFrame is not None: - for detection in detections: - bbox = frameNorm(rightFrame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax)) - cv2.rectangle(rightFrame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2) - cv2.putText(rightFrame, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255) - cv2.putText(rightFrame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255) - cv2.imshow("rectified right", rightFrame) + show("rectified right", rightFrame) - if depthFrame is not None: - for detection in detections: - bbox = frameNorm(croppedFrame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax)) - bbox[::2] += offsetX - cv2.rectangle(depthFrame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2) - cv2.putText(depthFrame, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255) - cv2.putText(depthFrame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255) - cv2.imshow("depth", depthFrame) + detections = [] if cv2.waitKey(1) == ord('q'): break