Skip to content

Commit

Permalink
Update example to add boundingbox 2d
Browse files Browse the repository at this point in the history
  • Loading branch information
haixuanTao committed Apr 20, 2024
1 parent 1e88749 commit 7b5c31d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
17 changes: 15 additions & 2 deletions examples/rerun-viewer/dataflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,35 @@ nodes:
IMAGE_HEIGHT: 540


- id: object_detection
custom:
source: ./object_detection.py
inputs:
image: webcam/image
outputs:
- bbox
envs:
IMAGE_WIDTH: 960
IMAGE_HEIGHT: 540

- id: rerun
custom:
source: dora-rerun
inputs:
image: webcam/image
text: webcam/text
boxes2d: object_detection/bbox
envs:
IMAGE_WIDTH: 540
IMAGE_HEIGHT: 960
IMAGE_WIDTH: 960
IMAGE_HEIGHT: 540
IMAGE_DEPTH: 3

- id: matplotlib
custom:
source: ./plot.py
inputs:
image: webcam/image
bbox: object_detection/bbox
envs:
IMAGE_WIDTH: 960
IMAGE_HEIGHT: 540
45 changes: 45 additions & 0 deletions examples/rerun-viewer/object_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import cv2
import numpy as np
from ultralytics import YOLO

from dora import Node
import pyarrow as pa

model = YOLO("yolov8n.pt")

node = Node()

IMAGE_WIDTH = int(os.getenv("IMAGE_WIDTH", 960))
IMAGE_HEIGHT = int(os.getenv("IMAGE_HEIGHT", 540))

for event in node:
event_type = event["type"]
if event_type == "INPUT":
event_id = event["id"]
if event_id == "image":
print("[object detection] received image input")
image = event["value"].to_numpy().reshape((IMAGE_HEIGHT, IMAGE_WIDTH, 3))

frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
results = model(frame) # includes NMS
# Process results
boxes = np.array(results[0].boxes.xywh.cpu())
conf = np.array(results[0].boxes.conf.cpu())
label = np.array(results[0].boxes.cls.cpu())
# concatenate them together
arrays = np.concatenate((boxes, conf[:, None], label[:, None]), axis=1)

node.send_output("bbox", pa.array(arrays.ravel()), event["metadata"])
else:
print("[object detection] ignoring unexpected input:", event_id)
elif event_type == "STOP":
print("[object detection] received stop")
elif event_type == "ERROR":
print("[object detection] error: ", event["error"])
else:
print("[object detection] received unexpected event:", event_type)
12 changes: 6 additions & 6 deletions examples/rerun-viewer/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ def on_input(
self.bboxs = np.reshape(bboxs, (-1, 6))
for bbox in self.bboxs:
[
min_x,
min_y,
max_x,
max_y,
x,
y,
w,
h,
confidence,
label,
] = bbox
cv2.rectangle(
self.image,
(int(min_x), int(min_y)),
(int(max_x), int(max_y)),
(int(x - w / 2), int(y - h / 2)),
(int(x + w / 2), int(y + h / 2)),
(0, 255, 0),
2,
)
Expand Down
5 changes: 4 additions & 1 deletion examples/rerun-viewer/webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
start = time.time()

# Run for 20 seconds
while time.time() - start < 60:
while time.time() - start < 1000:
# Wait next dora_input
event = node.next()
if event is None:
break

event_type = event["type"]
if event_type == "INPUT":
ret, frame = video_capture.read()
Expand Down

0 comments on commit 7b5c31d

Please sign in to comment.