File size: 2,852 Bytes
357c750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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