1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2024-11-24 08:12:13 +02:00
STARK/SpeechRecognition/SpeechRecognition.py

43 lines
1.4 KiB
Python
Raw Normal View History

2020-07-17 22:24:40 +02:00
import speech_recognition as sr
2020-08-29 18:58:39 +02:00
import config
2020-07-17 22:24:40 +02:00
r = sr.Recognizer()
2020-08-29 18:58:39 +02:00
m = sr.Microphone(device_index=config.device_index)
2020-07-17 22:24:40 +02:00
class SpeechToText:
2020-08-29 18:58:39 +02:00
def __init__(this, device = config.device_index, language = config.language_code, pause_threshold = 0.5):
2020-10-30 22:25:25 +02:00
this.device = device
2020-07-17 22:24:40 +02:00
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 ''
2020-07-17 22:24:40 +02:00
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)