-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
57 lines (46 loc) · 1.72 KB
/
main.py
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
from __future__ import print_function
from object_watcher.object_watcher import ObjectWatcher
from object_watcher.object_watcher import create_dlib_frontal_face_detector
from simple_audio_recorder.simple_audio_recorder import SimpleAudioRecorder
from stt.simple_stt import SimpleSTT
from websocket import create_connection
import json
import time
import traceback
transcriber = SimpleSTT()
URL_TEMPLATE = "{scheme}://{host}:{port}{path}"
def send_message(message, host="localhost", port=8181, path="/core", scheme="ws"):
payload = json.dumps({
"type": "recognizer_loop:utterance",
"context": "",
"data": {
"utterances": [message]
}
})
url = URL_TEMPLATE.format(scheme=scheme, host=host, port=str(port), path=path)
ws = create_connection(url)
ws.send(payload)
ws.close()
def transcribe_and_send(path_to_source):
try:
text = transcriber.transcribe(path_to_source)
print(text)
send_message(text)
except Exception:
traceback.print_exc()
if __name__ == "__main__":
recorder = SimpleAudioRecorder()
with SimpleAudioRecorder() as recorder:
fw = ObjectWatcher(detector=create_dlib_frontal_face_detector())
fw.register_object_entered_callback(recorder.start_recording, "output.wav")
fw.register_object_entered_callback(print, "recording!")
fw.register_object_left_callback(recorder.stop_recording)
fw.register_object_left_callback(print, "done recording!")
fw.register_object_left_callback(transcribe_and_send, "output.wav")
fw.start()
try:
while True:
time.sleep(2)
except KeyboardInterrupt:
fw.terminate()
fw.join()