Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

multithreading - Using pyaudio and Speech Recognition at the same time

I want to record the audio and get an audiofile while using Speech Recognition. For some reason my program always crashes after a few moments. It also does not come to creating the audiofile. I suspect there is a problem with using threads as both processes worked fine on their own. Unfortunately I could not find anything. Does anyone have an idea how I can fix this or how I can use Speech Recognition while recording sound?

import threading
import speech_recognition as sr
import pyaudio
import wave
import time

status = True

def record():
    chunk = 1024
    sample_format = pyaudio.paInt16
    channels = 1
    fs = 44100
    filename = 'output.wav'
    global status
    p = pyaudio.PyAudio()
    print('Recording')
    stream = p.open(format=sample_format,
                    channels=channels,
                    rate=fs,
                    frames_per_buffer=chunk,
                    input=True)
    frames = []
    while status == True:
        data = stream.read(chunk)
        frames.append(data)

    stream.stop_stream()
    stream.close()

    p.terminate()
    print('Finished recording')
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(sample_format))
    wf.setframerate(fs)
    wf.writeframes(b''.join(frames))
    wf.close()


        
def get_audio():
    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("H?re zu ...")
            audio = r.listen(source)
            said = ''
            try:
                said = r.recognize_google(audio, language="de_DE")
                print(said)
            except Exception as e:
                print('')







thread1=threading.Thread(target=record)
thread1.start()


thread2=threading.Thread(target=get_audio)
thread2.start()


time.sleep(5)

status=False

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can record and save sound with Speech Recognition. Just use this part of the code and it will create a speech.wav file:

def get_audio():
while True:
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("H?re zu ...")
        audio = r.listen(source)
        with open('speech.wav', 'wb') as f:
            f.write(audio.get_wav_data())
        try:
            said = r.recognize_google(audio, language="de_DE")
            print(said)
        except Exception as e:
            print('')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...