0
我有下面的腳本,它在終端中運行時工作:是否有可能在Django上使用Python SpeechRecognition?
所有的工作就是將話筒話筒轉爲文本。
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,則不必 - 您必須在客戶端收集音頻。 – zwer