File size: 3,864 Bytes
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 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 123 124 125 126 127 128 129 130 131 132 133 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from typing import Callable, Optional, Union
import cv2
import numpy as np
import pyarrow as pa
from utils import LABELS
from dora import DoraStatus
pa.array([])
CI = os.environ.get("CI")
CAMERA_WIDTH = 960
CAMERA_HEIGHT = 540
font = cv2.FONT_HERSHEY_SIMPLEX
writer = cv2.VideoWriter(
"output01.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
30,
(CAMERA_WIDTH, CAMERA_HEIGHT),
)
class Operator:
"""
Plot image and bounding box
"""
def __init__(self):
self.image = []
self.bboxs = []
self.bounding_box_messages = 0
self.image_messages = 0
self.text_whisper = ""
def on_event(
self,
dora_event: dict,
send_output: Callable[[str, Union[bytes, pa.UInt8Array], Optional[dict]], None],
) -> DoraStatus:
if dora_event["type"] == "INPUT":
return self.on_input(dora_event, send_output)
return DoraStatus.CONTINUE
def on_input(
self,
dora_input: dict,
send_output: Callable[[str, Union[bytes, pa.UInt8Array], Optional[dict]], None],
) -> DoraStatus:
"""
Put image and bounding box on cv2 window.
Args:
dora_input["id"] (str): Id of the dora_input declared in the yaml configuration
dora_input["value"] (arrow array): message of the dora_input
send_output Callable[[str, bytes | pa.UInt8Array, Optional[dict]], None]:
Function for sending output to the dataflow:
- First argument is the `output_id`
- Second argument is the data as either bytes or `pa.UInt8Array`
- Third argument is dora metadata dict
e.g.: `send_output("bbox", pa.array([100], type=pa.uint8()), dora_event["metadata"])`
"""
if dora_input["id"] == "image":
frame = (
dora_input["value"]
.to_numpy()
.reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
.copy() # copy the image because we want to modify it below
)
self.image = frame
self.image_messages += 1
print("received " + str(self.image_messages) + " images")
elif dora_input["id"] == "text" and len(self.image) != 0:
self.text_whisper = dora_input["value"][0].as_py()
elif dora_input["id"] == "bbox" and len(self.image) != 0:
bboxs = dora_input["value"].to_numpy()
self.bboxs = np.reshape(bboxs, (-1, 6))
self.bounding_box_messages += 1
print("received " + str(self.bounding_box_messages) + " bounding boxes")
for bbox in self.bboxs:
[
min_x,
min_y,
max_x,
max_y,
confidence,
label,
] = bbox
cv2.rectangle(
self.image,
(int(min_x), int(min_y)),
(int(max_x), int(max_y)),
(0, 255, 0),
2,
)
d = ((12 * 22) / (max_y - (CAMERA_HEIGHT / 2))) / 2.77 - 0.08
cv2.putText(
self.image,
LABELS[int(label)] + f", d={d:.2f}",
(int(max_x), int(max_y)),
font,
0.75,
(0, 255, 0),
2,
1,
)
cv2.putText(
self.image, self.text_whisper, (20, 35), font, 1, (250, 250, 250), 2, 1
)
if CI != "true":
writer.write(self.image)
cv2.imshow("frame", self.image)
if cv2.waitKey(1) & 0xFF == ord("q"):
return DoraStatus.STOP
return DoraStatus.CONTINUE
def __del__(self):
cv2.destroyAllWindows()
|