问题遇到的现象和发生背景
使用树莓派时,其只能录取到树莓派本身发出的声音(即打开树莓派的输出声音被其视为麦克风输入声音),但是这个代码在笔记本中是完全正常的,可以接收到麦克风接收的声音
问题相关代码,请勿粘贴截图
pip install baidu-aip
from aip import AipSpeech
import speech_recognition as sr
from win32com.client import Dispatch
import datetime
import time
speaker=Dispatch('SAPI.SpVoice')
APP_ID = '26601831'
API_KEY = 'kmN2ewIzpw8zLaD6S5gZ4Tyi'
SECRET_KEY = 'RUfN09FhKVYG1zVHsjmcL7NXe1XKqXeZ'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
def get_text(wav_bytes):
result = client.asr(wav_bytes, 'wav', 16000, {'dev_pid': 1537, })
try:
text = result['result'][0]
except Exception as e:
print(e)
text = ""
return text
r = sr.Recognizer()
mic = sr.Microphone()
print("请说话")
while True:
with mic as source:
r.adjust_for_ambient_noise(source)
# speaker.Speak('请说话')
audio = r.listen(source)
audio_data = audio.get_wav_data(convert_rate=16000)
print("\n正在分析")
text = get_text(audio_data)
arealist = [text]
# 随便打的一个list想检查其中元素是否含有 '经验' 这个内容
for i in arealist:
# print('i=',i)
print("i.find(u'大咩')", i.find(u'大咩'))
if i.find(u'大咩') != -1:
print('yes')
else:
print('no')
print('-----')
print('时间:', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
#print(text)
运行结果及报错内容
运行一切正常,但只是识别到了树莓派本身的输出声音
我想要达到的结果
要识别出麦克风接收到的声音(即输入声音)