dora-idefics2 / operators /idefics2_op.py
haixuantao's picture
Adding latest `WIP`
357c750
raw
history blame
2.85 kB
from dora import DoraStatus
import os
import pyarrow as pa
import cv2
from idefics2_utils import ask_vlm
from RealtimeTTS import TextToAudioStream, SystemEngine
engine = SystemEngine()
stream = TextToAudioStream(engine)
CAMERA_WIDTH = 960
CAMERA_HEIGHT = 540
FONT = cv2.FONT_HERSHEY_SIMPLEX
import pyttsx3
engine = pyttsx3.init("espeak")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[11].id) # English
def speak(text):
engine.say(text)
engine.runAndWait()
class Operator:
def __init__(self):
self.completed = True
self.instruction = "What is in the image?"
self.last_message = ""
def on_event(
self,
dora_event,
send_output,
) -> DoraStatus:
if dora_event["type"] == "INPUT":
if dora_event["id"] == "image":
if True:
image = (
dora_event["value"]
.to_numpy()
.reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
.copy()
)
cv2.imshow("frame2", image)
if cv2.waitKey(1) & 0xFF == ord("q"):
return DoraStatus.CONTINUE
output = ask_vlm(image, self.instruction)
cv2.putText(
image,
output,
(20, 14 + 15 * 25),
FONT,
0.5,
(190, 250, 0),
2,
)
if self.last_message != output:
speak(output)
print("response: ", output, flush=True)
send_output(
"assistant_message",
pa.array([output]),
dora_event["metadata"],
)
# stream.feed(output)
# stream.play()
self.last_message = output
self.completed = False
else:
print("Command not complete", flush=True)
elif dora_event["id"] == "instruction":
self.instruction = dora_event["value"][0].as_py()
print("instructions: ", self.instruction, flush=True)
elif dora_event["id"] == "control_reply":
control_reply = dora_event["value"][0].as_py()
if self.last_message == control_reply:
self.completed = True
else:
print(
f"expected: {self.last_message}, but got: {control_reply}",
flush=True,
)
return DoraStatus.CONTINUE