如何在Python3中实现语音识别?

时间:2019-06-12 16:59:50

标签: python-3.x

如何在python中安装Speech_recognition模块以及如何在python中将语音命令转换为字符串?

我已经在python 3.7上做到了,但是我做不到

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Speak Anything :")
    audio = r.listen(source)
    try:
        text = r.recognize_google(audio)
        print(f"You said : {}".format(text))
    except Exception:
        print("Sorry could not recognize what you said")

print(f"You said : {}".format(text))
     ^

SyntaxError:f字符串:不允许使用空表达式

1 个答案:

答案 0 :(得分:0)

F字符串希望您实际指定一个变量。显式使用.format()方法是对常规字符串而不是f字符串执行的操作。所以

print("You said : {}".format(text))

print(f"You said : {text}")

将起作用。