我在 google 识别中遇到查找错误

I am getting lookup error in google recognition

我正在尝试使用 Python 中的以下代码将语音转换为文本。我已经安装了 speech_recognition 库和 pyaudio。

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
    # read the audio data from the default microphone
    audio_data = r.record(source, duration=5)
    print("Recognizing...")
    # convert speech to text
    text = r.recognize_google(audio_data)
    print(text)

但是我遇到了很多错误

Recognizing...
Traceback (most recent call last):
  File "D:/NEC, K.R.Nagar, Kovilpatti/1st SEM/Python3/voice.py", line 9, in <module>
    text = r.recognize_google(audio_data)
  File "C:\Users\ELCOT\AppData\Roaming\Python\Python39\site-packages\speech_recognition\__init__.py", line 840, in recognize_google
    response = urlopen(request, timeout=self.operation_timeout)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 517, in open
    response = self._open(req, data)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Program Files\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 1371, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 1342, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Program Files\Python39\lib\http\client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Program Files\Python39\lib\http\client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python39\lib\http\client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python39\lib\http\client.py", line 1010, in _send_output
    self.send(msg)
  File "C:\Program Files\Python39\lib\http\client.py", line 950, in send
    self.connect()
  File "C:\Program Files\Python39\lib\http\client.py", line 921, in connect
    self.sock = self._create_connection(
  File "C:\Program Files\Python39\lib\socket.py", line 822, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Program Files\Python39\lib\socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
LookupError: unknown encoding: idna

知道是什么导致了这个错误吗?我想要这个代码用于我的语音识别项目。

试试这个1

import speech_recognition as sr

def takeCommand():
r=sr.Recognizer()
with sr.Microphone() as source:
    print("Listening...")
    audio=r.listen(source)

    try:
        statement=r.recognize_google(audio,language='en-in')
        print(f"user said:{statement}\n")


    except Exception as e:
        #speak("Sorry, please say that again")
        print('Sorry, please say that again')
        return "None"
    return statement


if __name__=='__main__':

statement = takeCommand().lower()
        print('detecting.....')
        print(statement)