-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speech to Text.pyw
163 lines (134 loc) · 4.97 KB
/
Speech to Text.pyw
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import speech_recognition as sr
from tkinter import *
import sys
# import tkinter.font as tkFont
# import time
# import os
# from pydub import AudioSegment
# from pydub.silence import split_on_silence
'small audio files.'
# filename = "machine-learning_speech-recognition_16-122828-0002.wav"
# r = sr.Recognizer()
#
# with sr.AudioFile(filename) as source:
# audio_data = r.record(source)
# text = r.recognize_google(audio_data)
# print(text)
r = sr.Recognizer()
'large audio files'
# def get_large_audio_transcription(path: object) -> str:
# sound = AudioSegment.from_wav(path)
# chunks = split_on_silence(sound, min_silence_len=500,
# silence_thresh=sound.dBFS-14, keep_silence=500)
# folder_name = 'audio-chunks'
#
# if not os.path.isdir(folder_name):
# os.mkdir(folder_name)
# whole_text = ""
#
# for i, audio_chunk in enumerate(chunks, start=1):
# chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
# audio_chunk.export(chunk_filename, format="wav")
#
# with sr.AudioFile(chunk_filename) as source:
# audio_listened = r.record(source)
# try:
# text = r.recognize_google(audio_listened)
# except sr.UnknownValueError as e:
# print("Error:", str(e))
# else:
# text = f"{text.capitalize()}."
# print(chunk_filename, ":", text)
# whole_text += text
#
# return whole_text
#
#
# file = "machine-learning_speech-recognition_7601-291468-0006.wav"
# print("\nFull text:", get_large_audio_transcription(file))
'Microphone to Text'
# def microphone_to_text(languages: dict, language_codes: dict) -> None:
# languages_text = ''
# for language in languages:
# languages_text += f'{language}: {languages[language]}\n'
#
# with sr.Microphone() as source:
# # read the audio data from the default microphone
# lang = language_codes[input(languages_text)]
# print('Start talking now.')
# audio_data = r.record(source, duration=5)
# print("Recognizing...")
# # convert speech to text
# text = r.recognize_google(audio_data, language=lang)
# print(text)
# time.sleep(2)
# again = input('Would you like to try again? \n1: Yes \n2: No')
# if again == '1':
# microphone_to_text(languages, language_codes)
# else:
# exit()
language_dict = {'1': 'English', '2': 'Japanese'}
language_dict_code = {'1': 'en-EN', '2': 'ja'}
# microphone_to_text(language_dict, language_dict_code)
def english():
with sr.Microphone() as source:
# read the audio data from the default microphone
audio_data = r.record(source, duration=5)
# convert speech to text
text = r.recognize_google(audio_data, language='en-En')
display_text(text)
def japanese():
with sr.Microphone() as source:
# read the audio data from the default microphone
audio_data = r.record(source, duration=5)
# convert speech to text
text = r.recognize_google(audio_data, language='ja')
display_text(text)
def display_text(text: str):
label.update()
label.configure(text='')
label2.update()
label2.configure(text=f'{text}')
label2.place(relx=0.475, rely=0.3, anchor=CENTER)
btn0.update()
btn0.configure(text='Retry', command=reset)
btn1.update()
btn1.configure(text='Exit', command=sys.exit)
# exit_btn.update()
# exit_btn.configure(text='Exit', command=exit_func)
# exit_btn.place(relx=0.5, rely=0.5, anchor=S)
def message_ja():
label.update()
label.configure(text='Speak now...')
label.place(relx=0.475, rely=0.3, anchor=CENTER)
window.after(100, japanese)
def message_en():
label.update()
label.configure(text='Speak now...')
label.place(relx=0.475, rely=0.3, anchor=CENTER)
window.after(100, english)
def reset():
label2.update()
label2.configure(text='')
window.after(200, clicked)
def clicked():
btn0.update()
btn0.configure(text="English", command=message_en)
btn0.place(relx=0.4, rely=0.5, relwidth=0.1, relheight=0.1)
btn1.update()
btn1.configure(text='Japanese', command=message_ja)
btn1.place(relx=0.5, rely=0.45, relwidth=0.1, relheight=0.1)
window = Tk()
window.option_add("*font", "Times 15")
window.title("Speech to Text Converter")
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
# window.geometry("%dx%d" % (width, height))
window.geometry("900x600")
btn0 = Button(window, text="Start", command=clicked)
btn0.place(relx=0.5, rely=0.5, anchor=CENTER, relwidth=0.1, relheight=0.1)
btn1 = Button(window, text='')
exit_btn = Button(window, text='Exit', command=sys.exit)
label = Label(window, text='')
label2 = Label(window, text='')
window.mainloop()