【发布时间】:2017-02-18 05:45:49
【问题描述】:
我有以下在终端中运行的脚本:
只需将麦克风语音转换为文本。
import speech_recognition as sr
# obtain audio from microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
按下按钮后是否可以在 Django 上进行这项工作? 比如:
查看:
import speech_recognition as sr
# Create your views here.
def index(request):
return render(request, 'app/index.html')
def text(request):
r = sr.Recognizer()
with sr.Microphone() as source:
#print("Say something!")
audio = r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
speech = r.recognize_google(audio)
except sr.UnknownValueError:
speech = "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
speech = "Could not request results from Google Speech Recognition service; {0}".format(e)
return render(request, 'app/text', {'speech': speech})
模板:
<form action="/text/" method="post">
<input type="button" value="Start listening" />
</form>
这可能吗?我是接近还是根本不接近?
【问题讨论】:
-
如果 Django 在加载 web 表单的同一台机器上运行,那么它可能会工作(尽管肯定不是应该这样做的方式)。另一方面,如果您想让用户使用您的 Django 后端将数据转发到 Google API,不,您必须在客户端收集音频。
-
您找到解决方案了吗?在桌面应用上可以吗?
标签: python django python-2.7 speech-recognition