【问题标题】:Show value to HTML just like how value gets display in CMD prompt when executes print() in python向 HTML 显示值,就像在 python 中执行 print() 时值如何在 CMD 提示符中显示一样
【发布时间】:2020-11-04 17:13:48
【问题描述】:

我正在关注this tutorial 并在 Django 中实现语音识别。我想知道我们执行 print("Listening...") 和 print("Recognizing...") 以让用户了解何时在 CMD 提示符下说话的方式,是否可以在每次用户说话时在 HTML 页面中发送值(聆听、识别)?

def takeCommand(): 
    r = sr.Recognizer() 
    with sr.Microphone() as source:  
        print("Listening...") 
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source)
        print(audio)
    try: 
        print("Recognizing...")     
        query = r.recognize_google(audio, language ='en-in') 
        print("User said:",query) 
    except Exception as e: 
        print(e)     
        print("Unable to Recognizing your voice.")
        return "None"
    return query

【问题讨论】:

    标签: python python-3.x django django-views speech-recognition


    【解决方案1】:

    您可以使用django.contrib.messages 将它们“打印”到模板中。

    在你看来:

    from django.contrib import messages
    
    messages.warning(self.request, f'Unable to Recognizing your voice')
    

    在您的模板中:

        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-{{ message.tags }}">{{ message }}</div>
            {% endfor %}
        {% endif %}
    

    有关消息框架的更多信息,请参阅:docs

    【讨论】:

    • 谢谢,但NameError : 'self' is not defined. 是我得到的错误。我在 takeCommand() 函数中实现了messages.warning(self.request, f'Unable to Recognizing your voice')。如果我在 takeCommand() 上提供 request 参数,那么它会在我使用这个函数的地方产生问题。请告诉我解决方案。
    • 我认为您可以将“self.request”更改为“request”并将请求添加到您的函数中:“def takeCommand(request):”
    【解决方案2】:

    如何将音频发送到服务器?不知何故通过ajax?
    当他点击一个按钮开始说话时,你使用 JS/jquery 放在某个地方:
    $('#some_div').append('Listening...');
    当他停止说话时,你放在那里:
    $('#some_div').append('Recognizing...'); 然后您通过 ajax 将音频发送到服务器。
    然后您从服务器响应 ajax 请求并执行以下任一操作:
    $('#some_div').append('Unable to Recognizing your voice.');
    或:
    $('#some_div').append('User said: ' + response.query);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 2020-04-03
      • 2020-10-03
      • 1970-01-01
      • 2017-05-27
      • 2021-09-30
      • 2013-02-12
      相关资源
      最近更新 更多