目录
- 1. 起因目的
- 2. 先看效果
- 3. 经过
- 4. 重点拎出来说 + todo
1. 起因目的
看到别人做了类似的效果。因此自己也想试试看。动手。
2. 先看效果
3. 经过
我用的是蓝牙耳机,EDIFIER W820NB
先找到声音,设置为 Hands-Free 模式
代码 1 ,查找设备名称, 看看哪个是能用的
我的设备, 能用的是 index=27
import sounddevice as sdimport numpy as npimport waveimport redef list_input_devices(): print(“&x1f3a4; 可用音频输入设备列表:”) input_devices = [] devices = sd.query_devices() for i, device in enumerate(devices): if device[‘max_input_channels’] > 0: device[‘index’] = i print(f”Index i}: device[‘name’]} – device[‘max_input_channels’]} channels – device[‘default_samplerate’]} Hz”) input_devices.append(device) return input_devicesdef record_audio(device_info, seconds=10): try: device_index = device_info[‘index’] channels = 1 强制单声道 rate = 16000 强制 16000 Hz print(f”n&x1f39b;&xfe0f; 使用设备: device_info[‘name’]}”) print(f”&x27a1;&xfe0f; 设备索引: device_index}”) print(f”&x27a1;&xfe0f; 通道数: channels}”) print(f”&x27a1;&xfe0f; 采样率: rate} Hzn”) print(“&x1f50d; 检查设备配置…”) sd.check_input_settings(device=device_index, channels=channels, samplerate=rate, dtype=’int16′) print(“&x2705; 配置有效”) print(“&x1f399;&xfe0f; 正在录音中…”) audio_data = sd.rec(int(seconds * rate), samplerate=rate, channels=channels, dtype=’int16′, device=device_index) sd.wait() safe_device_name = re.sub(r'[^ws-]’, ‘_’, device_info[‘name’]).replace(‘r’, ”).replace(‘n’, ”).strip() output_file = f”safe_device_name}_output.wav” with wave.open(output_file, ‘wb’) as wf: wf.setnchannels(channels) wf.setsampwidth(2) wf.setframerate(rate) wf.writeframes(audio_data.tobytes()) print(f”&x1f3b5; 录音已保存为 output_file}”) except sd.PortAudioError as pae: print(f”&x274c; 音频设备错误:pae}”) except OSError as ose: print(f”&x274c; 文件体系错误:ose}”) except Exception as e: print(f”&x274c; 未知错误:e}”)if __name__ == “__main__”: print(“&x1f50a; 使用默认音频接口”) input_devices = list_input_devices() if input_devices: for device in input_devices: if ‘EDIFIER W820NB’ in device[‘name’] and ‘Hands-Free’ in device[‘name’]: print(f”正在测试耳机设备: device[‘name’]}”) record_audio(device) else: print(“&x274c; 没有可用的音频输入设备。”)
代码 2 , 使用 whisper 转为文字效果很勉强,见文末拓展资料。
import sounddevice as sdimport numpy as npimport waveimport tempfileimport osimport whisper 加载 Whisper 模型model = whisper.load_model(“medium”) 可改为 “tiny”, “base”, “small”, “large” 音频录制设置CHANNELS = 1 单声道,Hands-Free 模式通常只支持 1 通道RATE = 16000 16000 Hz,适合 Hands-Free 模式RECORD_SECONDS = 5 每次录音时长(秒)DEVICE_INDEX = 27 已验证可用的设备索引DEVICE_NAME = “耳机 (@System32driversbthhfenum.sys,2;%1 Hands-Free AG Audio%0;(EDIFIER W820NB 双金标版))”def record_audio(seconds=RECORD_SECONDS): try: print(f”&x1f3a7; 正在录音 seconds} 秒…”) 使用 sounddevice 录制音频 audio_data = sd.rec( int(seconds * RATE), samplerate=RATE, channels=CHANNELS, dtype=’int16′, device=DEVICE_INDEX ) sd.wait() 等待录音完成 保存临时音频文件 with tempfile.NamedTemporaryFile(delete=False, suffix=”.wav”) as tmpfile: with wave.open(tmpfile.name, ‘wb’) as wf: wf.setnchannels(CHANNELS) wf.setsampwidth(2) 16-bit 音频 wf.setframerate(RATE) wf.writeframes(audio_data.tobytes()) return tmpfile.name except sd.PortAudioError as pae: print(f”&x274c; 音频设备错误:pae}”) return None except Exception as e: print(f”&x274c; 未知错误:e}”) return Nonedef transcribe_audio(audio_file): try: print(“&x1f9e0; 正在识别…”) result = model.transcribe(audio_file, language=”zh”) print(“&x1f4dd; 识别结局:”, result[‘text’].strip()) except Exception as e: print(f”&x274c; 语音识别失败:e}”) finally: if os.path.exists(audio_file): os.remove(audio_file)if __name__ == “__main__”: print(f”&x1f50a; 使用设备: DEVICE_NAME} (索引: DEVICE_INDEX})”) print(“&x1f399;&xfe0f; 开始实时听写,按 Ctrl+C 停止”) try: while True: 录制音频 audio_file = record_audio() if audio_file: 进行语音识别 transcribe_audio(audio_file) else: print(“&x26a0;&xfe0f; 录音失败,跳过识别”) 短暂暂停,避免过于频繁的录音 sd.sleep(100) 100 毫秒 except KeyboardInterrupt: print(“&x1f6d1; 停止实时识别”) except Exception as e: print(f”&x274c; 程序错误:e}”)
4. 重点拎出来说 + todo
- 开始的时候,加载模型比较慢。
- 能实现实时语音识别,但识别效果不佳,我猜测的缘故是:
- 耳机质量太差,有些参数设置不够合理。
到此这篇关于python使用whisper读取蓝牙耳机语音并转为文字的文章就介绍到这了,更多相关python whisper读取蓝牙耳机并转文字内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
无论兄弟们可能感兴趣的文章:
- python语音识别whisper的使用
- CodeWhisperer基于python使用经验分享
- Python实现语音识别Whisper的使用示例
- Python使用whisper实现语音识别(ASR)的示例代码
- Python+OpenAI?Whisper实现视频生成字幕