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.31k stars 2.4k forks source link

Pycharm: missing google-api-python-client module using Google Cloud Speech #368

Open one2gov opened 6 years ago

one2gov commented 6 years ago

Steps to reproduce

  1. Trying demo code and getting this error
  2. I tried installing google-api-python-client module through Maс os terminal, Pycharm built it pip, build it and added manually to folder and set it as source folder - nothing

Expected behaviour It works great with Google Speech Recognition, but I need some features from Cloud Speech

Actual behaviour Can't authorise to could api, I guess

Could not request results from Google Cloud Speech service; missing google-api-python-client module: ensure that google-api-python-client is set up correctly.

System information

My system is mac OS X Mojave.

My Python version is 3.7.

jhoelzl commented 6 years ago

Check with pip list in your project folder (or virtual environment) if this module is installed: google-api-python-client

one2gov commented 6 years ago

venv

google-api-python-client               1.7.4   
google-api-python-client-helpers       1.2.0   
google-api-python-client-py3           1.2     
google-auth                            1.5.1   
google-auth-httplib2                   0.0.3   
google-cloud                           0.34.0  

project folder

google-api-core          1.3.0    
google-api-python-client 1.7.4    
google-auth              1.5.1    
google-auth-httplib2     0.0.3    
google-cloud             0.34.0   
google-cloud-core        0.28.1   
google-cloud-speech      0.35.0   
google-cloud-storage     1.10.0   
google-resumable-media   0.3.1    
googleapis-common-protos 1.5.3    
jhoelzl commented 6 years ago

Have you created an credentials file for the Google Cloud Speech API? It seems that the library can not read the credentials file. You need to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to your credentials filepath. Please have a look on:

one2gov commented 6 years ago

Yes I did. I am using example code from https://cloud.google.com/speech-to-text/docs/quickstart-client-libraries#client-libraries-install-python and I set it up as

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="MyApp-00000.json"

and it works. But in speech_recognition I am using GOOGLE_CLOUD_SPEECH_CREDENTIALS = r"""json string""" I copied content of "MyApp-00000" to GOOGLE_CLOUD_SPEECH_CREDENTIALS and having this problem with credentials. I am also tried to use os.environ in speech_recognition, but it asks for string.

jhoelzl commented 6 years ago

Try to use absolute path in your GOOGLE_APPLICATION_CREDENTIAL. And, also try to add it to .bashrc like this instead of importing it in python:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

one2gov commented 6 years ago

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="MyApp-00000.json" in speech_recognition doesn't do anything, but GOOGLE_APPLICATION_CREDENTIALS = path.join(path.dirname(path.realpath(__file__)), "path/MyApp-00000.json") Gives me AssertionError: credentials_json must be None or a valid JSON string

p.s. I don't know how to export anything in python, since it's only understand import and I don't know where is .bashrc in Pycharm. I guess it's some kind of package manager, but Pycharm have ui for it.

jhoelzl commented 6 years ago

No, i mean the file on your system: /home/your-user-name/.bashrc

Here, add this line:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

Afterwards, start your terminal again to apply this change.

one2gov commented 6 years ago

I tested it with googles example code and adding export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json" to .bash_profile is the same as adding os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="MyApp-00000.json", but speech_recognition acts the same. If in googles example you can, not specify in the code GOOGLE_APPLICATION_CREDENTIALS variable, I don't see how to do it in speech_recognition. If I just put GOOGLE_APPLICATION_CREDENTIALS in credentials - it's not assigned, obviously. Json string is still not working and pointing out file still give the same not valid JSON string

Interesting, what is the correct structure of json string, maybe I should just copy past my credentials from json to working json string. Maybe something changed

ricardomerces commented 6 years ago

to solve just install the following packages:

google-api-python-client
gcloud

my code:

with open ('xxxxxx.json', 'r') the file:
    credential = file.read ()

print ("Google Speech Recognition thinks you said" + r.recognize_google_cloud (audio, language = "en-us", credentials_json = credential))
Jun0413 commented 5 years ago

Hi, I also encountered the same issue even after pip install google-api-python-client and set the PATH, but I solved it by looking into the source code.

It turned out that the import error came from oauth2client, so just a pip install oauth2client fixed my issue.

MkNxGn commented 5 years ago

Hello! On behalf of MkNxGn I would like to offer a few functions able to help you handle JSON files and help with your issue here.

`import json

def read_file(path): file = open(path, "r") data = file.read() file.close() return data

def read_json(path): while True: try: return json.loads(read_file(path)) except: print("File Opening Failure")

def write_json(path, data): while True: try: return write_file(path, json.dumps(data)) except: print("File Opening Failure")

def write_file(path, data): file = open(path, "w") file.write(str(data)) file.close() return data`

These functions work on multiple platforms. For your issue here you need to pass the string version of your file. Using MkNxGn Essentials; the functions above, you would need to use

r.recognize_google_cloud (audio, language = "en-us", credentials_json = json.dumps(read_json('PATH_TO_JSON_FILE')))

As for any other issues with using the google module. Issues can be fixed with the "All Google" module fix by MkNxGn as well. For windows use a .bat file and so on for other platforms.

`pip uninstall google-api-python-client pip uninstall google-auth pip uninstall google-auth-httplib2 pip uninstall gTTS pip uninstall gTTS-token pip uninstall google-api-core
pip uninstall google-api-python-client
pip uninstall google-auth
pip uninstall google-auth-httplib2
pip uninstall google-cloud
pip uninstall google-cloud-core
pip uninstall google-cloud-speech
pip uninstall google-cloud-storage
pip uninstall google-resumable-media
pip uninstall gcloud pip uninstall oauth2client pip uninstall googleapis-common-protos

pip install --upgrade google-api-python-client pip install --upgrade google-auth pip install --upgrade google-auth-httplib2 pip install --upgrade gTTS pip install --upgrade gTTS-token pip install --upgrade google-api-core
pip install --upgrade google-api-python-client
pip install --upgrade google-auth
pip install --upgrade google-auth-httplib2
pip install --upgrade google-cloud
pip install --upgrade google-cloud-core
pip install --upgrade google-cloud-speech
pip install --upgrade google-cloud-storage
pip install --upgrade google-resumable-media
pip install --upgrade gcloud pip install --upgrade oauth2client pip install --upgrade googleapis-common-protos `

Thanks! MkNxGn

karuhanga commented 5 years ago

In addition to this, pip install --upgrade google-api-python-client. It was missing for me as well. The problem is the import error is swallowed up by one which makes no sense to the user.

Hi, I also encountered the same issue even after pip install google-api-python-client and set the PATH, but I solved it by looking into the source code.

It turned out that the import error came from oauth2client, so just a pip install oauth2client fixed my issue.