File size: 4,087 Bytes
b98ffbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# import numpy as np
# import pyarrow as pa
# from dora import Node
# from dora import DoraStatus
# from ultralytics import YOLO
# import cv2
# pa.array([])

# CAMERA_WIDTH = 720
# CAMERA_HEIGHT = 1280

# model = YOLO("/home/peiji/yolov8n.pt")
# node = Node()

# # class Operator:
# #     """
# #     Infering object from images
# #     """

# #     def on_event(
# #         self,
# #         dora_event,
# #         send_output,
# #     ) -> DoraStatus:
# #         if dora_event["type"] == "INPUT":
# #             frame = (
# #                 dora_event["value"].to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
# #             )
# #             frame = frame[:, :, ::-1]  # OpenCV image (BGR to RGB)
# #             results = model(frame, verbose=False)  # includes NMS
# #             boxes = np.array(results[0].boxes.xyxy.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)

# #             send_output("bbox", pa.array(arrays.ravel()), dora_event["metadata"])

# #         return DoraStatus.CONTINUE
# for event in node:
#     print("djieoajdsaosijoi")
#     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((CAMERA_HEIGHT, CAMERA_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)









#!/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
node = Node()
model = YOLO("/home/peiji/yolov8n.pt")



IMAGE_WIDTH = int(os.getenv("IMAGE_WIDTH", 1280))
IMAGE_HEIGHT = int(os.getenv("IMAGE_HEIGHT", 720))

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)