mirror of
https://github.com/MarkParker5/STARK.git
synced 2024-11-24 08:12:13 +02:00
43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import speech_recognition as sr
|
|
import config
|
|
|
|
r = sr.Recognizer()
|
|
m = sr.Microphone(device_index=config.device_index)
|
|
|
|
class SpeechToText:
|
|
def __init__(this, device = config.device_index, language = config.language_code, pause_threshold = 0.5):
|
|
this.device = device
|
|
this.language = language
|
|
this.m = sr.Microphone(device_index = this.device)
|
|
this.r = sr.Recognizer()
|
|
this.r.pause_threshold = pause_threshold
|
|
this.r.energy_threshold = 2000
|
|
this.r.dynamic_energy_threshold = False
|
|
|
|
def listen(this):
|
|
with this.m as source:
|
|
audio = this.r.listen(source)
|
|
try:
|
|
responce = {'text': this.r.recognize_google(audio, language = this.language).lower(), 'status': 'ok'}
|
|
except sr.UnknownValueError:
|
|
responce = {'text': None, 'status': 'void'}
|
|
except sr.RequestError:
|
|
responce = {'text': None, 'status': 'error'}
|
|
return responce
|
|
|
|
def recognize(this, speech):
|
|
with sr.AudioFile(speech.getPath()) as source:
|
|
audio = r.record(source)
|
|
try:
|
|
return r.recognize_google(audio)
|
|
except:
|
|
return ''
|
|
|
|
def listen_noise(this):
|
|
with this.m as source:
|
|
this.r.adjust_for_ambient_noise(source)
|
|
|
|
def set_device(this, index):
|
|
this.device = 1
|
|
this.m = sr.Microphone(device_index = this.device)
|