简体   繁体   中英

How can I use speech recognition in Python on a proxy network?

It works quite well on a proxy free.network but whenever I try to run it on a proxy.network it gives this error.

Could not request results from Google STT; recognition connection failed: [Errno 11001] getaddrinfo failed

Github link for the code

Please help.

I was having similar issues for the last few days and after a lot of research I found out that sadly Google Speech API currently does not support proxies.

https://github.com/GoogleCloudPlatform/java-docs-samples/issues/1061#issuecomment-373478268

After many days working this issue I realized that speech_recognition python package, for desktop aplications (no cloud) is based on urllib that doesn´t support to proxies.

So I had to chage some code lines inside the google package speech recoginition to use requests package (which supports proxies) instead of urllib, such way to make it works:

In your <Home_python>\Lib\site-packages\speech_recognition folder open the init.py file and create a new method named def recognize_google_proxy(): Then copy def recognize_google() codes into def recognize_google_proxy().

Now you have two distict objects that makes the same thing.

Next, In the recognize_google_proxy() object, Change lines like described here:

  1. comment comand line bellow with # #request = Request(url, data=flac_data, headers={"Content-Type": "audio/x-flac; rate={}".format(audio_data.sample_rate)})

Insert to replace bellow code snipet:

import requests
http_proxy  = "http://<your proxy url>:port"
https_proxy = "https://<your proxy url>:port"
proxyDict = {
        "http"  : http_proxy, 
            "https" : https_proxy 
            }
    
    
  1. Comment comand line bellow with # #response = urlopen(request, timeout=self.operation_timeout)

Insert to replace bellow code snipet:

response = requests.post(url, data=flac_data, headers={"Content-Type": "audio/x-flac; rate={}".format(audio_data.sample_rate)},proxies=proxyDict)
  1. Save init.py

import speech_recognition

That's all.

Now you have available one object without proxy support and using urllib named recognize_google() and another object, with proxy support, using requests named recognize_google_proxy().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM