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

标签 python django python-2.7 speech-recognition

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

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

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 无法访问用户的计算机,因此如果您尝试从麦克风录音,您将使用服务器的麦克风(如果有的话)。

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

关于python - 是否可以在 Django 上使用 Python SpeechRecognition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311555/

相关文章:

python-2.7 - Matplotlib 缩放后的图形大小和位置

python - 如何以简单的方式知道对象的类型?

python - Kivy/Buildozer 导入错误 - pymssql.so 是 64 位而不是 32 位

python - 使用来自 2 个数据集的数据绘制多个图形

python - kwargs= {'pk' : self. pk} 在 get_absolute_url 方法中做什么?

python - 两个日期之间的 Django ORM 交集

python - 如何使用 Django include 标签作为单独的 HTML 模板?

python - 尝试,除了 ValueError 替换为 None

python - `length` 和 `name` 排序参数不符合我的要求

python math.acos 反余弦问题