Uberi / Uberi/speech_recognition

What is the correct way of using SpeechRecognition's AudioData function?

Open
#445 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9k
Forks
2.4k
Avg merge
12h 24m
Merged PRs (30d)
3

Description

I am trying to use sceech_recognition to translate my recorded audio data to text and so far I am able to successfully translate the recorded audio using a .wav . I am using pyaudio to record audio to a .wav file. Since pyaudio creates an audio stream, instead of writing it to a file and then using the file in speech_recognition, I want to translate the audio stream directly. Below is the implementation which should take the audio stream and then use sr.AudioData function and translate the audio.

`
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = get_nonexistant_path("voice.wav")

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data) #appending all the intermediate streams to a list

print("* done recording")

r = sr.Recognizer()

audio_bytes = b''.join(frames) #converting list to a byte object
audio_source = sr.AudioData(audio_bytes, RATE, CHANNELS)

try:
print('Trying to convert')
text = r.recognize_google(audio_data=audio_source, language='en-US', show_all=True)
print(text)

except sr.UnknownValueError:
print("Could not understand")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
`

Now, every time I run this code, I get an empty list as output.

`

  • recording
  • done recording
    Trying to convert
    [ ]
    `

Since I am also saving the audio stream to a .wav file for debugging purpose, when I use the same.wav file I get the correct translation.

You said "Google speech" {'alternative': [{'transcript': 'Google speech', 'confidence': 0.98762912}], 'final': True}
Can someone please tell me what is going wrong here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the AudioData entry point and the recognize_google call, then compare the byte stream passed directly with the saved .wav file that succeeds. Confirm what input format AudioData expects and determine why the direct stream returns an empty result; done means the recorded stream produces the same transcription as the .wav path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
audio-video-rtc
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.