Uberi / speech_recognition

Speech recognition module for Python, supporting several engines and APIs, online and offline.
https://pypi.python.org/pypi/SpeechRecognition/
BSD 3-Clause "New" or "Revised" License
8.45k stars 2.4k forks source link

Recognizer() opens undesired cmd windows #115

Closed quieromicubita closed 7 years ago

quieromicubita commented 8 years ago

When my application gets to this line 'r.recognize_google(audio).encode('utf-8')' it opens a command window for a couple of seconds and closes itself. That line of code does exactly what I need except for the attempt to open that command window.

My application is a GUI so by no means I´d like other window to open on top.

I´ve tried to look into the modules Pyaudio or Pyttsx for a method or function called 'recognize_google()' since I believe the exceptions are creating those attempts to open command windows but I'm still a noob and it´s being a little difficult for me to figure out how to avoid this command window to open.

My Python version is <2.7>. (You can check this by running python -V.)

My SpeechRecognition library version is <3.4.3>. (You can check this by running python -c "import speech_recognition as sr;print(sr.__version__)".)

My PyAudio library version is <0.2.9> / I don't have PyAudio installed. (You can check this by running python -c "import pyaudio as p;print(p.__version__)".)

PocketSphinx not installed

Uberi commented 8 years ago

Hi @quieromicubita,

This should definitely not happen, as we don't use the shell for making the subprocesses. Could you fill out the missing parts of the issue templates, such as your OS/OS version and the code that you're running?

quieromicubita commented 8 years ago

Thanks @Uberi,

I¨m using Windows 7 and the following are some parts of the code:

import speech_recognition as sr

(...)

''' I believe I´m creating an instance of Recognizer()''' self.r = sr.Recognizer()

''' Next, I have some code to get audio from sr.Microphone(), adjust ambient noise, format(energy threshold), etc''' (...)

try:

This is the exact line that opens a shell window for one or two seconds

print 'Shell window opens after this print runs'
self.message = self.r.recognize_google(audio).encode('utf-8')
print 'Shell window opens and closes somewhere in the middle of the latter line of code'

except: print 'Unable to recognize audio'

I avoided copying all lines of code since it´s a little big .py file. Please, let me know if I should provide you with more info. I´d really like to get rid of that shell window opening.

Uberi commented 8 years ago

Interesting. Do any of the example scripts open command windows?

Does it still happen when using other recognition functions like recognize_wit or recognize_api?

quieromicubita commented 8 years ago

Thanks Uberi,

I´ll try the examples and the other recognition functions you suggest as soon as possible this week and let you know.

Unfortunately, I´ll be a little busy for a couple of days :-(

quieromicubita commented 8 years ago

Hi Uberi, I tried this link 'Recognize speech input from the microphone' from the example scripts. I copied the entire microphone_recognition.py and run it.

Again, it opens a command window for a second or miliseconds... I will try the other recognition functions you suggested and let you know.

Thanks for your help.

quieromicubita commented 8 years ago

I isolated the following code and it doesn´t open the command window:

FYI I don´t have PocketSphinx installed.

!/usr/bin/env python3

NOTE: this example requires PyAudio because it uses the Microphone class

import speech_recognition as sr

obtain audio from the microphone r = sr.Recognizer() with sr.Microphone() as source: print("Say something!") audio = r.listen(source)

recognize speech using Sphinx try: print("Sphinx thinks you said " + r.recognize_sphinx(audio)) except sr.UnknownValueError: print("Sphinx could not understand audio") except sr.RequestError as e: print("Sphinx error; {0}".format(e))

quieromicubita commented 8 years ago

This code does open a command window:

NOTE: this example requires PyAudio because it uses the Microphone class

import speech_recognition as sr

obtain audio from the microphone r = sr.Recognizer() with sr.Microphone() as source: print("Say something!") audio = r.listen(source)

recognize speech using Google Speech Recognition 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))

quieromicubita commented 8 years ago

So, I finally could read on top (title bar) of the command window and it displayed a path with the following at the end '...\flac-win32.exe

Is that a better help?

Uberi commented 8 years ago

Yep, I'm not quite sure how that's happening though - maybe there's some sort of setting that you have set?

Anyways, one way you might be able to work around this is to right click the FLAC executable (run python3 -c "import speech_recognition as sr;print(sr.__file__)" to get the folder this should be in, and it will have the name flac-win32.exe), right click it, and then under one of the tabs, there should be a dropdown that lets you determine whether it should open in a new window, on the command line, etc. Setting this to the appropriate value might prevent the window from showing up.

quieromicubita commented 8 years ago

Thanks, yesterday my OS crashed so, I´ll take at least three days to make my laptop run again and will try your suggestion. I wish I had it running to let you know right away :-( Don´t know if you can leave this thread open. I hope so.

sujitvasanth commented 7 years ago

I have the same problem on windows32 win7 python 2.7 using google speech recognition a shell window opens for a fraction of a second. renaming flac causes a program error but stops the window appearing. i can find no propert to ajust on the flac ile properties to stop a window popping up. same code on linux does not cause this.

Qdrew commented 7 years ago

In the 2 "subprocess.Popen" calls within site-packages\speech_recognition__init__.py Adding ", creationflags=0x08000000" directly after "stdout=subprocess.PIPE" solves this. The flag does seem to be commonly used to get round the Windows 'brief console window appearance' issue.

I am wondering if there is any chance that could be added to updates or if I should find a way to overload/patch that function. I certainly don't feel comfortable changing the code here!

Interested to know your thoughts Uberi

Uberi commented 7 years ago

Hi @Qdrew,

Great suggestion, I've put it into version 3.6.3! This should now no longer happen.