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
| from aip import AipSpeech import speech_recognition as SpeechRecognition
APP_ID = "<APP_ID>" API_KEY = "<API_KEY>" SECRET_KEY = "<SECRET_KEY>" client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) sr = SpeechRecognition.Recognizer()
debug_file_name = "./debug.wav"
def _record(if_cmu: bool = False, rate=16000): with SpeechRecognition.Microphone(sample_rate=rate) as source: sr.adjust_for_ambient_noise(source, duration=1) print("等待接收音频") audio = sr.listen(source, timeout=15, phrase_time_limit=2) with open(debug_file_name, "wb") as f: f.write(audio.get_wav_data())
if if_cmu: return audio else: return _get_file_content(debug_file_name)
def _get_file_content(file_name): with open(file_name, "rb") as f: audio_data = f.read() return audio_data
def speech_to_text_baidu(audio_path: str = debug_file_name, if_microphone: bool = True): if if_microphone: result = client.asr(_record(), "wav", 16000, { "dev_pid": 1537 }) else: result = client.asr(_get_file_content(audio_path), "wav", 16000, { "dev_pid": 1537 })
if result["err_msg"] != "success.": return "语音识别失败: " + result["err_msg"] else: return result["result"][0]
result = speech_to_text_baidu()
print(result)
|