通过python代码将语音转换为文本时出现错误的文本

时间:2019-04-05 18:43:58

标签: python-3.x speech-recognition wav pyaudio pocketsphinx

我写了两个python代码,用于语音到文本的转换 一个python代码用于保存audio.wav文件,另一个用于转换已保存的audio.wav文件的文本

但是转换后我得到的文字不同于保存的语音

  

保存音频的代码:

import pyaudio
import wave

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "himfile.wav"

audio = pyaudio.PyAudio()

# start Recording
stream = audio.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)
print("finished recording")


# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()

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

要转换为文本的代码:

import speech_recognition as sr
r = sr.Recognizer()

def convertSpeech(file=None):
    if file:
        harvard = sr.AudioFile(file)
        with harvard as source: 
            r.adjust_for_ambient_noise(source)
            audio = r.record(source)
        print('converting...')
        print(file)
        print(r.recognize_sphinx(audio))

    else:
        with sr.Microphone() as source:
            print('say something...')
            audio = r.listen(source)
            print(r.recognize_sphinx(audio))

if __name__ == '__main__':
    convertSpeech("himfile.wav")

0 个答案:

没有答案