-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcribe.py
101 lines (88 loc) · 3.66 KB
/
transcribe.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
import speech_recognition as sr
import overrides as mysr
from googletrans import Translator
import threading
from better_profanity import profanity
from langchooser import LangChooser
# Config
OUTPUT_FILE_NAME = "transcription.txt"
# These next two have to be in ISO 639-1 format (see: https://www.w3schools.com/tags/ref_language_codes.asp)
# Prompt for language
langchooser = LangChooser()
langchooser.prompt()
IN_LANGUAGE = langchooser.from_lang
OUT_LANGUAGE = langchooser.to_lang
SHORT_IN = IN_LANGUAGE.split("-")[0]
SHORT_OUT = OUT_LANGUAGE.split("-")[0]
def listen(recognizer, microphone):
print("Listening...")
with microphone as source:
audio = recognizer.listen(source)
print("Stopped listening")
return audio
def transcribe(audio, recognizer, translator):
# print("Transcribing...")
try:
uk_text = recognizer.recognize_google(audio, language=IN_LANGUAGE)
translated_text = translator.translate(uk_text, src=SHORT_IN, dest=SHORT_OUT)
# Comment out the following line to activate "transcribe50 after dark"
processed_text = profanity.censor(translated_text.text)
write_to_file(OUTPUT_FILE_NAME, processed_text)
except sr.UnknownValueError:
print("Could not understand audio.")
write_to_file(OUTPUT_FILE_NAME, "")
except sr.RequestError as e:
print(f"Error occurred during recognition: {e}")
def write_to_file(file_path, text):
with open(file_path, "w", encoding="utf-8") as file:
file.write(text)
def get_mic():
for index, source in enumerate(sr.Microphone.list_microphone_names()):
print(f"{index}: {source}")
while True:
index = input("Select an index from the list above: ")
try:
return int(index)
except ValueError:
print("Invalid index")
def test_recording():
# This function never gets used in runtime, it is simply here to test the microphone
# Run it with the following command: `python -c 'from transcribe import *; test_recording()'`
recognizer = mysr.NewRecognizer()
recognizer.energy_threshold = 1000
recognizer.pause_threshold=0.3
recognizer.non_speaking_duration=0.3
microphone = sr.Microphone()
with microphone as source:
print("Adjusting for ambient noise, please don't say anything...")
recognizer.adjust_for_ambient_noise(source, duration=3)
print("Listening")
audio = recognizer.listen(source)
with open("microphone-results.wav", "wb") as f:
f.write(audio.get_wav_data())
if __name__ == "__main__":
mic_index = get_mic()
translator = Translator()
recognizer = mysr.NewRecognizer()
recognizer.energy_threshold = 1000
recognizer.pause_threshold=0.3
recognizer.non_speaking_duration=0.3
microphone = sr.Microphone(device_index=mic_index)
profanity.load_censor_words()
print("Adjusting for ambient noise, please don't say anything...")
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
try:
while True:
audio = listen(recognizer, microphone)
transcription_thread = threading.Thread(
target=transcribe,
kwargs={"audio":audio,
"recognizer":recognizer,
"translator":translator}
)
transcription_thread.setDaemon(True)
transcription_thread.start()
except KeyboardInterrupt:
print("\nShutting down recognition service...")
write_to_file(OUTPUT_FILE_NAME, "Recognition service inactive. This is sample text.")