gpt4 book ai didi

python - 语音识别,断言错误 "Source must be an audio source"

转载 作者:行者123 更新时间:2023-12-03 17:12:27 31 4
gpt4 key购买 nike

这是我的代码:

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
print('Say Something')
audio = r.listen(source)
voice_data = r.record(audio)
print(voice_data)

当我在终端上输入“python main.py”并启动程序时,它开始监听但没有听懂我说的话。我试过使用 adjust_for_ambient_noise()而不是 listen()但它也没有改变任何东西。

我使用的是 macOS Catalina 和 Python 3.8.1。

这是我得到的错误:

Traceback (most recent call last):
File "main.py", line 8, in <module>
voice_data = r.record(audio)
File "/Users/sefailyasoz/PycharmProjects/SpeechAssistant/venv/lib/python3.8/site-packages/speech_recognition/__init__.py", line 483, in record
assert isinstance(source, AudioSource), "Source must be an audio source"
AssertionError: Source must be an audio source

这是我使用 adjust_for_ambient_noise() 时得到的结果,如果我使用 listen ,它并没有结束,它只是在听,我以 Ctrl+C 结束它。

最佳答案

好吧,我稍微改变了我的功能

def record_audio(ask=False):
with sr.Microphone() as source:
if ask:
turkishSiri_speak(ask)
audio = r.listen(source)
voice_data = ''
try:
voice_data = r.recognize_google(audio , language='tr-TR')
except sr.UnknownValueError:
turkishSiri_speak('Ne söylediğini anlayamadım')
except sr.RequestError:
turkishSiri_speak('Google konuşma servisinde bir problem var')
return voice_data

但最大的问题是 macOS 问题,我必须允许 pycharm 或 Visual Studio 代码使用我的麦克风。在更改功能之前,它已经在授予权限后工作

关于python - 语音识别,断言错误 "Source must be an audio source",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338599/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com