3

I have the following script which works when running in a terminal:

All is does is turn microphone speech to text.

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))

Would it be possible to have this work on Django after pushing a button? Something like:

View:

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})

Template:

<form action="/text/" method="post">
    <input type="button" value="Start listening" />
</form>

Is this possible? Am I close or not at all?

James Mitchell
  • 1,987
  • 4
  • 22
  • 49
  • If Django is running on the same machine that is loading the web form, then it might work (although certainly not the way one should do it). If, on the other hand, you want to offer users to use your Django backend to forward data to Google APIs, nope - you have to collect the audio on the client side. – zwer Feb 18 '17 at 05:52
  • Have you found out any solution for this? Is it possible on the desktop app? – Akhil Sahu Jul 26 '19 at 09:18

1 Answers1

4

Django has no access to the users' computers so if you try to record from a microphone you will be using the server's microphone, if it has one.

You need to record using JS/HTML5 and then send the data to django to process with AJAX. You might even be able to stream it but it's probably not worth the effort.

Anonymous
  • 10,357
  • 3
  • 37
  • 49