-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_to_text.py
85 lines (65 loc) · 2.04 KB
/
speech_to_text.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
import pyaudio
import wave
import speech_recognition as sr
from io import BytesIO
from pixels import Pixels
import time
# Respeaker settings
RESPEAKER_RATE = 16000 # Sample rate
RESPEAKER_CHANNELS = 2 # Number of channels
RESPEAKER_WIDTH = 2 # Sample width in bytes
RESPEAKER_INDEX = 1 # Input device ID
CHUNK = 1024 # Buffer size
RECORD_SECONDS = 5 # Duration of recording
pixels = Pixels()
pixels.off()
time.sleep(1)
# Initialize PyAudio
p = pyaudio.PyAudio()
# Open a stream for audio input
stream = p.open(
rate=RESPEAKER_RATE,
format=p.get_format_from_width(RESPEAKER_WIDTH),
channels=RESPEAKER_CHANNELS,
input=True,
input_device_index=RESPEAKER_INDEX
)
pixels.listen()
print("* recording")
frames = []
# Record audio in chunks
for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
# Stop and close the stream
stream.stop_stream()
stream.close()
p.terminate()
pixels.think()
# Save the recorded audio to an in-memory BytesIO stream and add WAV headers
audio_stream = BytesIO()
wf = wave.open(audio_stream, 'wb')
wf.setnchannels(RESPEAKER_CHANNELS)
wf.setsampwidth(p.get_sample_size(p.get_format_from_width(RESPEAKER_WIDTH)))
wf.setframerate(RESPEAKER_RATE)
wf.writeframes(b''.join(frames))
wf.close()
# Reset the stream position to the beginning
audio_stream.seek(0)
# Initialize the recognizer for SpeechRecognition
recognizer = sr.Recognizer()
# Use the recorded audio stream for speech recognition
with sr.AudioFile(audio_stream) as source:
audio = recognizer.record(source) # Read the entire audio stream
# Convert the speech to text using Google's API
try:
# Get the result from Google Speech Recognition
result = recognizer.recognize_google(audio, language='tr-TR', show_all=False)
print(result)
except sr.UnknownValueError:
print("Sorry, I could not understand the audio.")
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
pixels.off()
time.sleep(3)