我有以下脚本在终端中运行时有效:
一切都是将麦克风语音转为文本。
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>
这可能吗?我是否关闭?
答案 0 :(得分:2)
Django无权访问用户&#39;计算机,所以如果你试图从麦克风录音,你将使用服务器的麦克风(如果有的话)。
您需要record using JS/HTML5然后将数据发送到django以使用AJAX进行处理。你甚至可以流式传输它,但它可能不值得努力。