|
import cv2 |
|
|
|
|
|
from dora import DoraStatus |
|
|
|
|
|
CAMERA_WIDTH = 960 |
|
CAMERA_HEIGHT = 540 |
|
|
|
FONT = cv2.FONT_HERSHEY_SIMPLEX |
|
|
|
writer = cv2.VideoWriter( |
|
"output01.avi", |
|
cv2.VideoWriter_fourcc(*"MJPG"), |
|
60, |
|
(CAMERA_WIDTH, CAMERA_HEIGHT), |
|
) |
|
|
|
|
|
class Operator: |
|
""" |
|
Plot image and bounding box |
|
""" |
|
|
|
def __init__(self): |
|
self.bboxs = [] |
|
self.buffer = "" |
|
self.submitted = [] |
|
self.lines = [] |
|
|
|
def on_event( |
|
self, |
|
dora_event, |
|
send_output, |
|
): |
|
if dora_event["type"] == "INPUT": |
|
id = dora_event["id"] |
|
value = dora_event["value"] |
|
if id == "image": |
|
|
|
image = ( |
|
value.to_numpy().reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3)).copy() |
|
) |
|
cv2.resize(image, (CAMERA_HEIGHT * 2, CAMERA_WIDTH * 2)) |
|
|
|
cv2.putText( |
|
image, self.buffer, (20, 14 + 15 * 25), FONT, 0.5, (190, 250, 0), 2 |
|
) |
|
|
|
i = 0 |
|
for text in self.submitted[::-1]: |
|
color = ( |
|
(0, 255, 190) |
|
if text["role"] == "user_message" |
|
else (0, 190, 255) |
|
) |
|
cv2.putText( |
|
image, |
|
text["content"], |
|
( |
|
20, |
|
14 + (13 - i) * 25, |
|
), |
|
FONT, |
|
0.5, |
|
color, |
|
2, |
|
) |
|
i += 1 |
|
writer.write(image) |
|
cv2.resize(image, (CAMERA_HEIGHT * 3, CAMERA_WIDTH * 3)) |
|
cv2.imshow("frame", image) |
|
if cv2.waitKey(1) & 0xFF == ord("q"): |
|
return DoraStatus.STOP |
|
elif id == "keyboard_buffer": |
|
self.buffer = value[0].as_py() |
|
elif "message" in id: |
|
self.submitted += [ |
|
{ |
|
"role": id, |
|
"content": value[0] |
|
.as_py() |
|
.replace("\n", " ") |
|
.replace("- ", ""), |
|
} |
|
] |
|
|
|
return DoraStatus.CONTINUE |
|
|