在Python里做语音识别,常见链路是录音、音频预处理与特征展示、调用语音转文本接口。原文给出的示例覆盖了sounddevice录音、librosa提取MFCC、Google Speech-to-Text识别三段,但代码片段之间存在导入缺失和参数衔接问题。下面按可复现的模块重组这些实现,保留原文的库、参数和调用逻辑,并补上排查点。
一、环境与依赖
原文给出的安装命令是:- pip install SpeechRecognition
- pip install google-cloud-speech
复制代码 代码中还引用了sounddevice、soundfile、librosa、matplotlib等库,原文未逐一给出安装命令,复现前需要确保这些依赖已可用。录音文件路径原文固定为/mnt/data/recording.wav,目录必须存在且有写权限。
二、录音模块
原文用sounddevice设置采样率44100、录音时长5秒、双通道,然后通过sd.wait()等待录音结束,最后用soundfile.write保存wav。原片段调用了soundfile.write却没有给出import soundfile,直接运行会触发NameError;整理后的代码如下:- import sounddevice as sd
- import soundfile
- fs = 44100
- duration = 5
- print('开始录音...')
- recording = sd.rec(int(duration * fs), samplerate=fs, channels=2)
- sd.wait()
- print('录音完成')
- recording_path = '/mnt/data/recording.wav'
- soundfile.write(recording_path, recording, fs)
- print(f'录音已保存至 {recording_path}')
复制代码 这里的fs同时参与采样点数和文件写入采样率,channels=2表示双通道录音。原文后续识别配置没有显式声道参数,因此复现时要确认送入识别接口的音频与配置匹配。
三、音频加载、归一化与MFCC
原文用librosa.load(recording_path, sr=None)加载录音,sr=None表示沿用文件原始采样率;随后用librosa.util.normalize做归一化,再通过librosa.feature.mfcc提取MFCC,并用matplotlib绘图展示。代码结构如下:- import librosa
- import librosa.display
- import matplotlib.pyplot as plt
- audio, sr = librosa.load(recording_path, sr=None)
- audio = librosa.util.normalize(audio)
- mfccs = librosa.feature.mfcc(y=audio, sr=sr)
- plt.figure(figsize=(10, 4))
- librosa.display.specshow(mfccs, x_axis='time', y_axis='linear')
- plt.colorbar(format='%+2.0f dB')
- plt.title('MFCC')
- plt.show()
复制代码 从原文流程看,MFCC主要用于特征提取和可视化,并没有作为Google Speech-to-Text的输入;识别阶段直接读取wav文件二进制。因此MFCC图更适合调试音频质量和观察特征,不是该示例的识别输入。
四、调用Google Speech-to-Text
原文使用google.cloud.speech_v1p1beta1,设置project_id和language_code='zh-CN',创建SpeechClient,读取录音内容构造RecognitionAudio,再构造RecognitionConfig并调用recognize。整理后如下:- from google.cloud import speech_v1p1beta1 as speech
- project_id = 'your_project_id'
- language_code = 'zh-CN'
- client = speech.SpeechClient(project=project_id)
- with open(recording_path, 'rb') as audio_file:
- content = audio_file.read()
- audio = speech.RecognitionAudio(content=content)
- config = speech.RecognitionConfig(
- encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
- sample_rate_hertz=sr,
- language_code=language_code,
- enable_word_time_offsets=True,
- )
- response = client.recognize(config=config, audio=audio)
- for result in response.results:
- print('Transcript: {}'.format(result.alternatives[0].transcript))
复制代码 RecognitionConfig中的encoding指定LINEAR16,sample_rate_hertz使用librosa加载得到的sr,language_code指定中文识别,enable_word_time_offsets开启词级时间偏移。识别结果从response.results中遍历,取result.alternatives[0].transcript输出文本。
五、整合顺序与常见错误
整合时按“录音并保存wav→librosa加载并归一化→提取MFCC并展示→读取wav字节调用STT→遍历response.results”执行即可。原文实战案例重复了这些步骤,核心依赖关系没有变化。
常见问题主要有:1)soundfile未导入导致保存录音时报NameError,补import soundfile;2)/mnt/data/recording.wav路径不存在或无权限导致写文件失败;3)识别配置使用sr而不是录音变量fs,若混用会让sample_rate_hertz与音频实际采样率不一致;4)project_id仍是占位符,且原文未展示Google客户端认证配置,复现时要检查凭据和客户端初始化;5)录音为双通道,而RecognitionConfig未显式配置声道,需保证音频输入与接口参数一致;6)config指定LINEAR16,但原文没有转码步骤,需确保wav文件编码与配置字段对应。
从实现分层看,录音、MFCC展示和云STT调用是三个独立环节,替换识别后端时只需调整RecognitionConfig和客户端调用;保留MFCC可视化则便于在识别失败时先检查音频是否被正确加载和归一化。原文提到的应用场景包括智能音箱、智能手机和自动驾驶汽车,这个示例适合作为本地录音加云接口识别的入门骨架,但在实际使用中应优先处理导入、路径、采样率和认证这几类可复现问题。 |