-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_listener.py
106 lines (89 loc) · 3.3 KB
/
bot_listener.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
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
99
100
101
102
103
104
105
106
'''
bot_listener.py
ウェイクワードを認識してサウンドを鳴らし、コマンド入力を待機する音声認識プログラムです。
ウェイクワードが入力されるとコマンド受付モードに入り、終了コマンドが入力されると待機モードに戻ります。
音声認識結果を返し、ChatGPTにデータを渡します。
'''
import json, time
from pathlib import Path
from vosk import Model, KaldiRecognizer
import pyaudio
from bot_motor_controller import neopixels_face, neopixels_hearing, neopixels_off
from bot_voice_synthesizer import notification
# Jsonファイルからウェイクワードとコマンドの配列を読み込む
with open(Path("data/command_data.json"), "rb") as f:
data = json.load(f)
WAKE = data["wake"]
EXIT = data["exit"]
# Voskモデルの読み込み
model = Model(str(Path("vosk-model-small-ja-0.22").resolve()))
#model = Model(str(Path("vosk-model-ja-0.22").resolve()))
# マイクの初期化
recognizer = KaldiRecognizer(model, 16000)
mic = pyaudio.PyAudio()
# voskの初期化
def engine():
stream = mic.open(format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=8192)
while True:
stream.start_stream()
try:
data = stream.read(4096)
if recognizer.AcceptWaveform(data):
result = recognizer.Result()
response_json = json.loads(result)
print("🖥️ SYSTEM: ", response_json)
response = response_json["text"].replace(" ","")
return response
else:
pass
except OSError:
pass
# ウェイクワード待機をlistening コマンド待機をhearingと設定
listening = True
hearing = False
# listeningをループして音声認識 ウェイクワード認識でhearingループする
def bot_listen_hear():
global listening, hearing
# neopixelsの目を点灯
neopixels_face()
if hearing == True: print("🖥️ SYSTEM: ","-"*22, "GPTに話しかけてください","-"*22)
else: print("🖥️ SYSTEM: ","-"*22, "ウェイクワード待機中","-"*22)
while listening:
response = engine()
if response in WAKE:
listening = False
hearing = True
neopixels_off()
notification()
time.sleep(0.5)
neopixels_hearing()
print("🖥️ SYSTEM: ","-"*22, "GPTに話しかけてください","-"*22)
elif response.strip() == "":
continue # 空白の場合はループを続ける
else:
pass
while hearing:
response = engine()
if response in EXIT:
listening = True
hearing = False
neopixels_off()
notification()
time.sleep(0.5)
neopixels_hearing()
elif response.strip() == "":
continue # 空白の場合はループを続ける
else:
neopixels_off()
notification()
time.sleep(0.5)
neopixels_hearing()
return response
if __name__ == "__main__":
while True:
response = bot_listen_hear()
print("response: ",response)