Skip to content

[experiment, SDK] Run inference on selected ROI #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions gen2-inference-on-roi/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import cv2
from depthai_sdk import OakCamera
from depthai_sdk.classes.packets import DetectionPacket
import numpy as np
import depthai as dai

roi = None
new_roi = False
selecting_roi = False
def roi_selection(event, x, y, flags, param):
global start_pts, selecting_roi, roi, new_roi

if event == cv2.EVENT_LBUTTONDOWN:
selecting_roi = True
start_pts = [x, y]

elif event == cv2.EVENT_MOUSEMOVE:
if selecting_roi:
start_pts[2:] = [x, y]
roi = start_pts

elif event == cv2.EVENT_LBUTTONUP:
selecting_roi = False
new_roi = True
if abs(start_pts[0] - x) < 100 or abs(start_pts[1] - y) < 100:
roi = None
return
start_pts[2:] = [x, y]
roi = start_pts


shape = None
def cb(packet: DetectionPacket):
global roi, shape
frame = packet.visualizer.draw(packet.frame)
if roi is not None:
x_start, y_start, x_end, y_end = roi
# frame_roi = frame[y_start:y_end, x_start:x_end]

# Create a mask with the same size as the frame
mask = np.zeros_like(frame)
mask[y_start:y_end, x_start:x_end] = frame[y_start:y_end, x_start:x_end]

# Darken the unselected region
frame_darkened = frame * 0.3
frame_darkened = frame_darkened.astype(np.uint8)

# Blend the mask with the darkened frame
frame = cv2.addWeighted(frame_darkened, 1, mask, 1, 0)
shape = frame.shape
cv2.imshow('color', frame)

with OakCamera() as oak:
cv2.namedWindow("color")
cv2.setMouseCallback("color", roi_selection)

color = oak.create_camera('color')
nn = oak.create_nn('mobilenet-ssd', color)
oak.visualize(color, fps=True, callback=cb)
oak.visualize([nn.out.passthrough, nn], fps=True, scale=2/3)

manipIn = oak.pipeline.create(dai.node.XLinkIn)
manipIn.setStreamName('manip_in')
manipIn.out.link(nn.image_manip.inputConfig)

oak.start() # Initialize the device and start the pipeline

raw_cfg = nn.image_manip.initialConfig.get() # To keep existing settings

manipQ = oak.device.getInputQueue('manip_in')

while oak.running():
oak.poll()
if new_roi:
new_roi = False
conf = dai.ImageManipConfig()
conf.set(raw_cfg)
if roi is not None:
normalized_roi = (roi[0]/shape[1], roi[1]/shape[0], roi[2]/shape[1], roi[3]/shape[0])
conf.setCropRect(normalized_roi)
manipQ.send(conf)
print(roi)

cv2.destroyAllWindows()