-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscribe.py
131 lines (107 loc) · 4.24 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
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
import os
import queue
import threading
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import pyaudio
from google.cloud import speech
# Set the Google Cloud service account key file for authentication
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'E:/ClearSpeak_Keys/clearspeak-418017-8c3c58665bdc.json'
# Audio recording parameters optimized for speech recognition
RATE = 16000
CHUNK = int(RATE / 10)
stop_transcription = False
class MicrophoneStream:
def __init__(self, rate, chunk):
self.rate = rate
self.chunk = chunk
self.buffer = queue.Queue()
self.closed = True
def __enter__(self):
self.audio_interface = pyaudio.PyAudio()
self.audio_stream = self.audio_interface.open(
format=pyaudio.paInt16,
channels=1, rate=self.rate,
input=True, frames_per_buffer=self.chunk,
stream_callback=self.fill_buffer,
)
self.closed = False
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.audio_stream.stop_stream()
self.audio_stream.close()
self.closed = True
self.buffer.put(None)
self.audio_interface.terminate()
def fill_buffer(self, in_data, frame_count, time_info, status_flags):
self.buffer.put(in_data)
return None, pyaudio.paContinue
def generator(self):
while not self.closed:
chunk = self.buffer.get()
if chunk is None:
return
data = [chunk]
while True:
try:
chunk = self.buffer.get(block=False)
if chunk is None:
return
data.append(chunk)
except queue.Empty:
break
yield b''.join(data)
def listen_print_loop(responses, text_widget, stop_flag):
for response in responses:
if stop_flag():
break
if not response.results or not response.results[0].alternatives:
continue
result = response.results[0]
transcript = result.alternatives[0].transcript
if result.is_final:
text_widget.insert(tk.END, transcript + '\n')
text_widget.see(tk.END)
def start_transcription(text_widget, stop_flag):
client = speech.SpeechClient()
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=RATE,
language_code='en-US',
enable_automatic_punctuation=True,
)
streaming_config = speech.StreamingRecognitionConfig(config=config, interim_results=True)
with MicrophoneStream(RATE, CHUNK) as stream:
audio_generator = stream.generator()
requests = (speech.StreamingRecognizeRequest(audio_content=content) for content in audio_generator)
responses = client.streaming_recognize(streaming_config, requests)
listen_print_loop(responses, text_widget, stop_flag)
def main():
def stop_transcription_callback():
global stop_transcription
stop_transcription = True
def start_transcription_callback():
global stop_transcription
stop_transcription = False
threading.Thread(target=start_transcription, args=(text_widget, lambda: stop_transcription), daemon=True).start()
root = tk.Tk()
root.title("Clear Speak")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
text_widget = ScrolledText(root, wrap=tk.WORD)
text_widget.grid(row=0, column=0, columnspan=2, sticky="nsew", padx=10, pady=10)
start_button = ttk.Button(root, text="Start Transcription", command=start_transcription_callback)
start_button.grid(row=1, column=0, sticky="ew", padx=10, pady=10)
stop_button = ttk.Button(root, text="Stop Transcription", command=stop_transcription_callback)
stop_button.grid(row=1, column=1, sticky="ew", padx=10, pady=10)
# Make the buttons equal width and responsive
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
# Adjust the main window's minimum size
root.update()
root.minsize(root.winfo_width(), root.winfo_height())
# Start the GUI event loop
root.mainloop()
if __name__ == '__main__':
main()