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

Google Cloud Speech : "Invalid JSON payload received. Unknown name "speech_context" at 'config': Cannot find field." #334

Open avinashs24 opened 6 years ago

avinashs24 commented 6 years ago

Steps to reproduce

  1. The error occurs when recognize_google_cloud method is used with preferred_phrases parameter.
  2. Use the google cloud speech recognition like this, r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS,preferred_phrases=['hi, hello']))

Expected behaviour

The request should be processed successfully and the audio input from microphone should be recognized as text.

Actual behaviour

Received the following exception,

Could not request results from Google Cloud Speech service; <HttpError 400 when requesting https://speech.googleapis.com/v1/spee
ch:recognize?alt=json returned "Invalid JSON payload received. Unknown name "speech_context" at 'config': Cannot find field.">

I looked into the source of the recognize_google_cloud method,

if preferred_phrases is not None: speech_config["speechContext"] = {"phrases": preferred_phrases}

The key where preferred_phrases is included is given as speechContext, but when referred to official google cloud speech docs, its actually speechContexts. Below is the sample request json obtained from docs,

{ "config": { "encoding":"FLAC", "sampleRateHertz": 16000, "languageCode":"en-US", "speechContexts": { "phrases":["hoful","shwazil"] } }, "audio":{ "uri":"gs://speech-demo/shwazil_hoful.flac" } }

System information

My system is Windows 10 x64. My Python version is 3.6.4. My Pip version is 9.0.1

My SpeechRecognition library version is 3.8.1

My PyAudio library version is 0.2.11

My microphones are: ['Microsoft Sound Mapper - Input', 'Microphone (Realtek Audio)', 'Microsoft Sound Mapper - Output', 'Speakers / Headphones (Realtek ']

dcam0050 commented 6 years ago

This issue was solved with pull request #304. Seems it is still present when installing via pip rather than from sources

partounian commented 6 years ago

@dcam0050 I install from sources but still have the issue somehow, I don't see that in the code. The issue not speechContexts over speechContext at the moment. It is something else, looking into it.

lichengunc commented 6 years ago

I believe the issue is "speechContexts" over "speechContext", please check your pip installed code again. :-)

avinashs24 commented 6 years ago

This must be updated in pypi as well. When will it be done?

pranjaldaga commented 6 years ago

Thanks, @avinashs24 for asking this! I was having the same trouble but on the suggestion of @lichengunc I changed from "SpeechContext" to "SpeechContexts" in the speech_recognition init file and it works fine.

gotjazz commented 6 years ago

same here - changed it locally meanwhile but I'm sure havign this packaged would be nice for a lot of people since you stumble upon this pretty quickly when using google cloud speech

jamilfaisal commented 4 years ago

Bug is still here. Downloaded the latest speech_recognition package (Version 3.8.1) from pip. Had to manually change it from speechContext to SpeechContexts in the speech_recognition init file.

jonahbrennan commented 4 years ago

After the DYI fix on the init file, passphrase doesn't error anymore but I haven't noticed any increase in response time for the short one word commands I'm shouting. shouting doesn't help either

Does anyone have any tips for speeding up these one word requests? preferred_phrases=['yes','no','stop']

ghost commented 3 years ago

Is it really fixed?

 "Invalid JSON payload received. Unknown name "speechContext" at 'config': Cannot find field.". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'config', 'description': 'Invalid JSON payload received. Unknown name "speechContext" at \'config\': Cannot find field.'}]"

I see that it was merged, but it was sent to PyPi? (or better, will it be sent?)

image

Speech Recognition 3.8.1 (holy shit, last release in 2017 ^ - ^ - it looks that the native client can be a good choice for context fine-tune)

This package is just dead!

hiroono commented 2 years ago

@dcam0050 I installed the latest version from source (as suggested above) and ran r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/speech_recognition/__init__.py", line 933, in recognize_google_cloud
    from google.cloud.speech import enums
ImportError: cannot import name 'enums' from 'google.cloud.speech' (/opt/homebrew/lib/python3.9/site-packages/google/cloud/speech/__init__.py)

Perhaps version incompatibility?

brandonbarringer commented 1 year ago

Was having the same issue. I fixed locally by changing it to speech_contexts On line 979 of init.py: config['speech_contexts'] = [speech.SpeechContext(