是否可以在 Django 上使用 Python SpeechRecognition?

Jam*_*ell 3 python django speech-recognition python-2.7

我有以下脚本,可以在终端中运行时运行:

所做的就是将麦克风语音转换为文本。

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))
Run Code Online (Sandbox Code Playgroud)

按下按钮后是否可以在 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})
Run Code Online (Sandbox Code Playgroud)

模板:

<form action="/text/" method="post">
    <input type="button" value="Start listening" />
</form>
Run Code Online (Sandbox Code Playgroud)

这可能吗?我是亲近还是根本不亲近?

Ano*_*ous 5

Django 无法访问用户的计算机,因此如果您尝试从麦克风进行录制,您将使用服务器的麦克风(如果有的话)。

您需要使用 JS/HTML5 进行记录,然后将数据发送到 django 使用 AJAX 进行处理。您甚至可以流式传输它,但这可能不值得付出努力。