Formatting and spelling fixes.
This commit is contained in:
parent
a0b5bdbb0a
commit
f213b229b8
@ -26,33 +26,33 @@ def main():
|
|||||||
help="How real time the recording is in seconds.", type=float)
|
help="How real time the recording is in seconds.", type=float)
|
||||||
parser.add_argument("--phrase_timeout", default=3,
|
parser.add_argument("--phrase_timeout", default=3,
|
||||||
help="How much empty space between recordings before we "
|
help="How much empty space between recordings before we "
|
||||||
"consider it a new line in the transcription.", type=float)
|
"consider it a new line in the transcription.", type=float)
|
||||||
if 'linux' in platform:
|
if 'linux' in platform:
|
||||||
parser.add_argument("--default_microphone", default='pulse',
|
parser.add_argument("--default_microphone", default='pulse',
|
||||||
help="Default microphone name for SpeechRecognition. "
|
help="Default microphone name for SpeechRecognition. "
|
||||||
"Run this with 'list' to view available Microphones.", type=str)
|
"Run this with 'list' to view available Microphones.", type=str)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# The last time a recording was retreived from the queue.
|
# The last time a recording was retrieved from the queue.
|
||||||
phrase_time = None
|
phrase_time = None
|
||||||
# Current raw audio bytes.
|
# Current raw audio bytes.
|
||||||
last_sample = bytes()
|
last_sample = bytes()
|
||||||
# Thread safe Queue for passing data from the threaded recording callback.
|
# Thread safe Queue for passing data from the threaded recording callback.
|
||||||
data_queue = Queue()
|
data_queue = Queue()
|
||||||
# We use SpeechRecognizer to record our audio because it has a nice feauture where it can detect when speech ends.
|
# We use SpeechRecognizer to record our audio because it has a nice feature where it can detect when speech ends.
|
||||||
recorder = sr.Recognizer()
|
recorder = sr.Recognizer()
|
||||||
recorder.energy_threshold = args.energy_threshold
|
recorder.energy_threshold = args.energy_threshold
|
||||||
# Definitely do this, dynamic energy compensation lowers the energy threshold dramtically to a point where the SpeechRecognizer never stops recording.
|
# Definitely do this, dynamic energy compensation lowers the energy threshold dramatically to a point where the SpeechRecognizer never stops recording.
|
||||||
recorder.dynamic_energy_threshold = False
|
recorder.dynamic_energy_threshold = False
|
||||||
|
|
||||||
# Important for linux users.
|
# Important for linux users.
|
||||||
# Prevents permanent application hang and crash by using the wrong Microphone
|
# Prevents permanent application hang and crash by using the wrong Microphone
|
||||||
if 'linux' in platform:
|
if 'linux' in platform:
|
||||||
mic_name = args.default_microphone
|
mic_name = args.default_microphone
|
||||||
if not mic_name or mic_name == 'list':
|
if not mic_name or mic_name == 'list':
|
||||||
print("Available microphone devices are: ")
|
print("Available microphone devices are: ")
|
||||||
for index, name in enumerate(sr.Microphone.list_microphone_names()):
|
for index, name in enumerate(sr.Microphone.list_microphone_names()):
|
||||||
print(f"Microphone with name \"{name}\" found")
|
print(f"Microphone with name \"{name}\" found")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
for index, name in enumerate(sr.Microphone.list_microphone_names()):
|
for index, name in enumerate(sr.Microphone.list_microphone_names()):
|
||||||
@ -61,7 +61,7 @@ def main():
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
source = sr.Microphone(sample_rate=16000)
|
source = sr.Microphone(sample_rate=16000)
|
||||||
|
|
||||||
# Load / Download model
|
# Load / Download model
|
||||||
model = args.model
|
model = args.model
|
||||||
if args.model != "large" and not args.non_english:
|
if args.model != "large" and not args.non_english:
|
||||||
@ -73,13 +73,13 @@ def main():
|
|||||||
|
|
||||||
temp_file = NamedTemporaryFile().name
|
temp_file = NamedTemporaryFile().name
|
||||||
transcription = ['']
|
transcription = ['']
|
||||||
|
|
||||||
with source:
|
with source:
|
||||||
recorder.adjust_for_ambient_noise(source)
|
recorder.adjust_for_ambient_noise(source)
|
||||||
|
|
||||||
def record_callback(_, audio:sr.AudioData) -> None:
|
def record_callback(_, audio:sr.AudioData) -> None:
|
||||||
"""
|
"""
|
||||||
Threaded callback function to recieve audio data when recordings finish.
|
Threaded callback function to receive audio data when recordings finish.
|
||||||
audio: An AudioData containing the recorded bytes.
|
audio: An AudioData containing the recorded bytes.
|
||||||
"""
|
"""
|
||||||
# Grab the raw bytes and push it into the thread safe queue.
|
# Grab the raw bytes and push it into the thread safe queue.
|
||||||
@ -124,7 +124,7 @@ def main():
|
|||||||
result = audio_model.transcribe(temp_file, fp16=torch.cuda.is_available())
|
result = audio_model.transcribe(temp_file, fp16=torch.cuda.is_available())
|
||||||
text = result['text'].strip()
|
text = result['text'].strip()
|
||||||
|
|
||||||
# If we detected a pause between recordings, add a new item to our transcripion.
|
# If we detected a pause between recordings, add a new item to our transcription.
|
||||||
# Otherwise edit the existing one.
|
# Otherwise edit the existing one.
|
||||||
if phrase_complete:
|
if phrase_complete:
|
||||||
transcription.append(text)
|
transcription.append(text)
|
||||||
@ -149,4 +149,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user