Search code examples
pythonspeech-recognitionspeech

why speech recognition is not recognizing any sound


I'm working on a Virtual Assistant project that recognizes a speech then convert it to a text and follow instructions according to the speech.

now, my issue is the speech never recognize any word I say, there are no errors popping. however, when I print the speech recognized it is always blank.

please see the below code:

import wikipedia
import webbrowser
import speech_recognition as sr
import pyttsx3
import subprocess
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import pyaudio



#ignore warnings
warnings.filterwarnings('ignore')

 def record_audio():
     #record
     r = sr.Recognizer() #creating recognizer object

     #open the mic and record

     with sr.Microphone() as source:
         print('say somthing!')
         audio = r.listen(source)



     #use google speech recognition
     data = ''
     try:
         date = r.recognize_google(audio)
         print('you said: '+data)
     except sr.UnknownValueError:
         print('google cant understand the audio !')
     except sr.RequestError as e:
         print('request results from google speech recognition service error '+ e)


     return data


record_audio()

the output is always as per the below:

you said :

what I have tried to solve this:

r.adjust_for_ambient_noise(source, duration=1)

in terminal: pip install pipwin

none of the above worked.

indentations are fine, it is just the formatting of the thread.


Solution

  • There is a typo in your code, that's why you will never know what google understood.

    data = r.recognize_google(audio)