1
votes

I want to press a button in an html-template

              <form action="{% url 'speech2text' %}" class="card-text" method="POST">
                  {% csrf_token %}
                  <button class="btn btn-primary btn-sm" type="submit">Start</button>
              </form>

              <p> Sie haben das folgende gesagt: </p>
              <p> {{ speech_text }} </p>

by pressing the button, the code in the following view shall be executed and send back the result into the template:

def speech2textView(request):
    if request.method == "POST":
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            text = r.recognize_google(audio, language="de-DE")
            args = {'speech_text': text}
            return render(request, 'speech2text.html', args)

Whats wrong here? many thanks for your help.

3
Looks like somehow you are calling the view with a method which is not POST, in that case your view is not returning anything (thus is actually returning None). Your form looks right though.bug
you should use print() to check if request.method have value "POST" and if text gets any string from google. You should also check if you don't get any error when run it. And I assume that you run django on local computer because django on server don't have access to your local microphone.furas
many thanks for you support. POST works well and the text is correct when I run it in python. good point with the microphone. what do you suggest? do I need to catch the voice by the frontend?Michael

3 Answers

1
votes

I edited the code again and now it is working. I would like to share this. Many thanks to this community.

def speech2textView(request):
    args = {}
    if request.method == "POST":
        print(request.method)
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            text = r.recognize_google(audio, language="de-DE")
            args = {'speech_text': text}
    return render(request, 'speech2text.html', args)
0
votes

This error occurs because there was no return! It seems that return render(request, 'speech2text.html', args) doesn't work. Try adding return render() outside of the with, and if the same error occurs try adding that outside of the request.method == "POST":. This way u can check where the problem occurs.

0
votes

try to checking the context with adding a print command and inspecting in your command window like this

def speech2textView(request):
    if request.method == "POST":
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            text = r.recognize_google(audio, language="de-DE")
            args = {'speech_text': text}
            print(args)
            return render(request, 'speech2text.html', args)

if you didn't see the data in your command window I think the problem is in your recognition step or on the request step keep tracking with printing some data to see where is the code stuck or fail.