Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Python SpeechRecognition on Django?

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?

like image 955
James Mitchell Avatar asked Mar 13 '26 23:03

James Mitchell


1 Answers

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.

like image 88
Anonymous Avatar answered Mar 15 '26 13:03

Anonymous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!