Working wake word (porcupine) and audio (speech) request to server :)
This commit is contained in:
parent
a5bddad9cb
commit
37d7849090
@ -0,0 +1,46 @@
|
|||||||
|
import struct
|
||||||
|
|
||||||
|
import pvporcupine
|
||||||
|
import pyaudio
|
||||||
|
import speech_recognition as sr
|
||||||
|
|
||||||
|
from jarvis.utils import server_utils
|
||||||
|
|
||||||
|
wake_word_handler = pvporcupine.create(keywords=['jarvis'])
|
||||||
|
|
||||||
|
|
||||||
|
def wake_word_listening():
|
||||||
|
pa = pyaudio.PyAudio()
|
||||||
|
|
||||||
|
audio_stream = pa.open(
|
||||||
|
rate=wake_word_handler.sample_rate,
|
||||||
|
channels=1,
|
||||||
|
format=pyaudio.paInt16,
|
||||||
|
input=True,
|
||||||
|
frames_per_buffer=wake_word_handler.frame_length
|
||||||
|
)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pcm = audio_stream.read(wake_word_handler.frame_length)
|
||||||
|
pcm = struct.unpack_from("h" * wake_word_handler.frame_length, pcm)
|
||||||
|
|
||||||
|
keyword_index = wake_word_handler.process(pcm)
|
||||||
|
|
||||||
|
if keyword_index >= 0:
|
||||||
|
print("Recognized")
|
||||||
|
record()
|
||||||
|
|
||||||
|
|
||||||
|
def record():
|
||||||
|
# obtain audio from the microphone
|
||||||
|
r = sr.Recognizer()
|
||||||
|
with sr.Microphone() as source:
|
||||||
|
print("Say something!")
|
||||||
|
r.adjust_for_ambient_noise(source=source, duration=0.5)
|
||||||
|
audio = r.listen(source, timeout=2, phrase_time_limit=5)
|
||||||
|
|
||||||
|
server_utils.send_record_to_server(audio.frame_data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
wake_word_listening()
|
0
jarvis/utils/__init__.py
Normal file
0
jarvis/utils/__init__.py
Normal file
17
jarvis/utils/server_utils.py
Normal file
17
jarvis/utils/server_utils.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import requests
|
||||||
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
|
|
||||||
|
def send_record_to_server(frame_data):
|
||||||
|
# TODO: use config or even ping to find the server on local network ?
|
||||||
|
url_service = "http://192.168.1.12:5000" + "/process_audio_request"
|
||||||
|
|
||||||
|
headers = CaseInsensitiveDict()
|
||||||
|
headers["Content-Type"] = "text/xml; charset=utf8"
|
||||||
|
# headers["Authorization"] = config_utils.get_in_config("API_KEY")
|
||||||
|
|
||||||
|
response = requests.post(url_service,
|
||||||
|
headers=headers,
|
||||||
|
data=frame_data)
|
||||||
|
|
||||||
|
print(response.content)
|
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
pvporcupine~=1.9.5
|
||||||
|
PyAudio~=0.2.11
|
||||||
|
SpeechRecognition~=3.8.1
|
||||||
|
requests~=2.26.0
|
Reference in New Issue
Block a user