|
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] |
|
results = self.model(frame, verbose=False) |
|
|
|
boxes = np.array(results[0].boxes.xyxy.cpu()) |
|
conf = np.array(results[0].boxes.conf.cpu()) |
|
|
|
label = np.array(results[0].boxes.cls.cpu()) |
|
|
|
arrays = np.concatenate((boxes, conf[:, None], label[:, None]), axis=1) |
|
send_output("bbox", pa.array(arrays.ravel()), dora_input["metadata"]) |
|
return DoraStatus.CONTINUE |
|
|