dora-robomaster / operators /object_detection.py
haixuantao's picture
Adding data
c782e33
raw
history blame
1.46 kB
import numpy as np
import pyarrow as pa
from dora import DoraStatus
from ultralytics import YOLO
pa.array([])
CAMERA_WIDTH = 960
CAMERA_HEIGHT = 540
class Operator:
"""
Object Detection: Infering object from images using Deep Learning model YOLOv8.
The output sent is bounding box representing the corners of the bounding box
as well as their COCO label.
"""
def __init__(self):
self.model = YOLO("yolov8n.pt")
def on_event(
self,
dora_event,
send_output,
) -> DoraStatus:
if dora_event["type"] == "INPUT":
return self.on_input(dora_event, send_output)
return DoraStatus.CONTINUE
def on_input(
self,
dora_input,
send_output,
) -> DoraStatus:
"""Handle image"""
frame = dora_input["value"].to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
results = self.model(frame, verbose=False) # includes NMS
# Process results
boxes = np.array(results[0].boxes.xyxy.cpu())
conf = np.array(results[0].boxes.conf.cpu())
# COCO Label
label = np.array(results[0].boxes.cls.cpu())
# concatenate them together
arrays = np.concatenate((boxes, conf[:, None], label[:, None]), axis=1)
send_output("bbox", pa.array(arrays.ravel()), dora_input["metadata"])
return DoraStatus.CONTINUE