dora-idefics2 / operators /idefics2_op.py
haixuantao's picture
Use whisper instead of keyboard
ffc2aa4
raw
history blame
1.56 kB
from dora import DoraStatus
import pyarrow as pa
import cv2
from idefics2_utils import ask_vlm
import pyttsx3
CAMERA_WIDTH = 960
CAMERA_HEIGHT = 540
FONT = cv2.FONT_HERSHEY_SIMPLEX
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.instruction = "What is in the image?"
self.last_message = ""
self.image = None
def on_event(
self,
dora_event,
send_output,
) -> DoraStatus:
if dora_event["type"] == "INPUT":
if dora_event["id"] == "image":
self.image = (
dora_event["value"]
.to_numpy()
.reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
)
elif dora_event["id"] == "instruction":
self.instruction = dora_event["value"][0].as_py()
print("instructions: ", self.instruction, flush=True)
if self.image is not None:
output = ask_vlm(self.image, self.instruction)
speak(output)
print("response: ", output, flush=True)
send_output(
"assistant_message",
pa.array([output]),
dora_event["metadata"],
)
self.last_message = output
return DoraStatus.CONTINUE