File size: 1,457 Bytes
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
c782e33
 
 
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c782e33
12d535c
 
 
f4392e4
12d535c
 
 
c782e33
12d535c
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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