-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
299 lines (243 loc) · 10.6 KB
/
main.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import tkinter as tk
import threading
import signal
import sys
import time
from datetime import datetime, timedelta
from queue import Queue
from io import BytesIO
import speech_recognition as sr
from deep_translator import GoogleTranslator
import webrtcvad
import whisper
import torchaudio
import torch
import os
from tkinter import *
USE_ONNX = False
model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad',
model='silero_vad',
force_reload=False,
onnx=USE_ONNX)
(get_speech_timestamps,
save_audio,
read_audio,
VADIterator,
collect_chunks) = utils
class Subtitler:
def __init__(self, offset_x, offset_y, font_size, color, bg_color, sacrificial_color, tk_timeout,
app_output_id, record_timeout, phrase_timeout, pause_threshold, model_type, vad_aggressiveness=1):
self.offset_x = offset_x
self.offset_y = offset_y
self.font_size = font_size
self.color = color
self.bg_color = bg_color
self.sacrificial_color = sacrificial_color
self.tk_timeout = tk_timeout
self.app_output_id = app_output_id
self.record_timeout = record_timeout
self.phrase_timeout = phrase_timeout
self.pause_threshold = pause_threshold
self.model_type = model_type
self.translation_queue = Queue()
self.translator = GoogleTranslator(source='ja', target='en')
self.vad = webrtcvad.Vad()
self.vad.set_mode(vad_aggressiveness)
def translate_audio(self):
data_queue = Queue()
recorder = sr.Recognizer()
recorder.pause_threshold = self.pause_threshold
source = sr.Microphone(device_index=self.app_output_id)
with source as m:
recorder.adjust_for_ambient_noise(source)
self.sample_rate = 16000
print(f'Sample Rate: {self.sample_rate}')
def record_callback(recorder, audio):
data_queue.put((datetime.now(), audio.get_raw_data(convert_rate=self.sample_rate)))
stop_listening = recorder.listen_in_background(source, record_callback, phrase_time_limit=self.record_timeout)
phrase_time = None
last_sample = bytes()
while True:
now = datetime.now()
if not data_queue.empty():
prev_phrase_time = phrase_time
if phrase_time and now - phrase_time > timedelta(seconds=self.phrase_timeout):
last_sample = bytes()
while not data_queue.empty():
phrase_time, data = data_queue.get()
if phrase_time and now - phrase_time > timedelta(seconds=self.phrase_timeout):
print(now, phrase_time, 'deleted')
continue
else:
print(now, phrase_time, 'used')
last_sample += data
print(datetime.now(), 'start vad')
audio_data = sr.AudioData(last_sample, self.sample_rate, 2)#source.SAMPLE_WIDTH)
wav_data = BytesIO(audio_data.get_wav_data())
with open('en_example.wav', 'w+b') as f:
f.write(wav_data.read())
wav = read_audio('en_example.wav', sampling_rate=self.sample_rate)
speech_timestamps = get_speech_timestamps(wav, model, sampling_rate=self.sample_rate)
if len(speech_timestamps) == 0:
time.sleep(0.05)
continue
frames = collect_chunks(speech_timestamps, wav)
print(len(frames))
buffer_ = BytesIO()
torchaudio.save(buffer_, collect_chunks(speech_timestamps, wav).unsqueeze(0), self.sample_rate, bits_per_sample=16, format='wav')
audio_data = sr.AudioData(buffer_.getbuffer().tobytes(), self.sample_rate, 2)
print(datetime.now(), 'done vad')
text = recorder.recognize_whisper(audio_data, model=self.model_type, language='Japanese')
print(datetime.now(), 'done whisper')
try:
text += '\n' + self.translator.translate(text)
except:
pass
print(datetime.now(), 'done translation')
phrase_time = now
self.translation_queue.put(text)
else:
time.sleep(0.1)
def subtitle_updater(self, root, label):
while not self.translation_queue.empty():
label.destroy()
if root.wm_state() == 'withdrawn':
root.deiconify()
msg = self.translation_queue.get()
label = tk.Label(
text=msg,
font=('Comic Sans MS', self.font_size, 'bold'),
fg=self.color,
bg=self.bg_color
)
label.after(self.tk_timeout, root.withdraw)
label.after(self.tk_timeout, label.destroy)
label.pack(side='bottom', anchor='s')
root.update_idletasks()
root.after(50, lambda: self.subtitle_updater(root, label))
def setup_overlay(self, root=None):
if root is None:
root = tk.Tk()
root.overrideredirect(True)
root.geometry(f'{root.winfo_screenwidth()}x{root.winfo_screenheight()}+{self.offset_x}+{self.offset_y}')
root.lift()
root.wm_attributes('-topmost', True)
root.wm_attributes('-transparentcolor', self.sacrificial_color)
root.config(bg=self.sacrificial_color)
return root
def close_app(self, *_):
print('Closing subtitler.')
sys.exit(0)
def start_app(self, root=None):
signal.signal(signal.SIGINT, self.close_app)
overlay = self.setup_overlay(root)
subtitle = tk.Label()
threading.Thread(target=self.translate_audio, daemon=True).start()
self.subtitle_updater(overlay, subtitle)
overlay.mainloop()
class SubtitlerApp(Frame):
def __init__(self, master):
self.root = master
super().__init__(master)
self.offset_x = Entry(self)
self.offset_x.insert(0, 0)
self.offset_y = Entry(self)
self.offset_y.insert(0, -100)
self.font_size = Entry(self)
self.font_size.insert(0, 20)
self.color = Entry(self)
self.color.insert(0, "black")
self.bg_color = Entry(self)
self.bg_color.insert(0, "white")
self.sacrificial_color = Entry(self)
self.sacrificial_color.insert(0, "yellow")
self.tk_timeout = Entry(self)
self.tk_timeout.insert(0, 5000)
self.app_output_id = StringVar(self)
self.app_output_id.set("1")
self.app_output_id_dropdown = OptionMenu(self, self.app_output_id, "1", "2", "3")
self.record_timeout = Entry(self)
self.record_timeout.insert(0, 3)
self.phrase_timeout = Entry(self)
self.phrase_timeout.insert(0, 3)
self.pause_threshold = Entry(self)
self.pause_threshold.insert(0, 0.75)
self.model_type = Entry(self)
self.model_type.insert(0, "base")
self.start_button = Button(self, text="Start", command=self.start_app)
self.pack()
# Create labels for all the Entry widgets
self.offset_x_label = Label(self, text="Offset X")
self.offset_y_label = Label(self, text="Offset Y")
self.font_size_label = Label(self, text="Font Size")
self.color_label = Label(self, text="Color")
self.bg_color_label = Label(self, text="Background Color")
self.sacrificial_color_label = Label(self, text="Sacrificial Color")
self.tk_timeout_label = Label(self, text="Tk Timeout")
self.app_output_id_label = Label(self, text="App Output ID")
self.record_timeout_label = Label(self, text="Record Timeout")
self.phrase_timeout_label = Label(self, text="Phrase Timeout")
self.pause_threshold_label = Label(self, text="Pause Threshold")
self.model_type_label = Label(self, text="Model Type")
# Grid the labels and Entry widgets
self.offset_x_label.grid(row=0, column=1)
self.offset_x.grid(row=0, column=2)
self.offset_y_label.grid(row=1, column=1)
self.offset_y.grid(row=1, column=2)
self.font_size_label.grid(row=2, column=1)
self.font_size.grid(row=2, column=2)
self.color_label.grid(row=3, column=1)
self.color.grid(row=3, column=2)
self.bg_color_label.grid(row=4, column=1)
self.bg_color.grid(row=4, column=2)
self.sacrificial_color_label.grid(row=5, column=1)
self.sacrificial_color.grid(row=5, column=2)
self.tk_timeout_label.grid(row=6, column=1)
self.tk_timeout.grid(row=6, column=2)
self.app_output_id_label.grid(row=7, column=1)
self.app_output_id_dropdown.grid(row=7, column=2)
self.record_timeout_label.grid(row=8, column=1)
self.record_timeout.grid(row=8, column=2)
self.phrase_timeout_label.grid(row=9, column=1)
self.phrase_timeout.grid(row=9, column=2)
self.pause_threshold_label.grid(row=10, column=1)
self.pause_threshold.grid(row=10, column=2)
self.model_type_label.grid(row=11, column=1)
self.model_type.grid(row=11, column=2)
self.start_button = Button(self, text="Start", command=self.start_app)
self.start_button.grid(row=12, column=0)
def start_app(self):
# Get the values from the user input
offset_x = int(self.offset_x.get())
offset_y = int(self.offset_y.get())
font_size = int(self.font_size.get())
color = self.color.get()
bg_color = self.bg_color.get()
sacrificial_color = self.sacrificial_color.get()
tk_timeout = int(self.tk_timeout.get())
app_output_id = int(self.app_output_id.get())
record_timeout = int(self.record_timeout.get())
phrase_timeout = int(self.phrase_timeout.get())
pause_threshold = float(self.pause_threshold.get())
model_type = self.model_type.get()
subtitler = Subtitler(
offset_x=offset_x,
offset_y=offset_y,
font_size=font_size,
color=color,
bg_color=bg_color,
sacrificial_color=sacrificial_color,
tk_timeout=tk_timeout,
app_output_id=app_output_id,
record_timeout=record_timeout,
phrase_timeout=phrase_timeout,
pause_threshold=pause_threshold,
model_type=model_type
)
self.root.destroy()
# Start the Subtitler app
subtitler.start_app()
if __name__ == '__main__':
root = Tk()
app = SubtitlerApp(root)
root.mainloop()