我有一个音频文件(30秒),并希望使用Google将其转换为文本。
我正在遵循本指南:
https://realpython.com/python-speech-recognition/#working-with-audio-files
我的代码如下:
cut = sr.AudioFile(“ cutsound.wav”) 以切为源: 音频= r.record(源) r.recognize_google(音频)
现在,我收到此无尽的错误消息:
---------------------------------------------------------------------------
BrokenPipeError Traceback (most recent call last)
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1317 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error
~/anaconda3/envs/tf/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1238 """Send a complete request to the server."""
-> 1239 self._send_request(method, url, body, headers, encode_chunked)
1240
~/anaconda3/envs/tf/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1284 body = _encode(body, 'body')
-> 1285 self.endheaders(body, encode_chunked=encode_chunked)
1286
~/anaconda3/envs/tf/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked)
1235
~/anaconda3/envs/tf/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
1064 + b'\r\n'
-> 1065 self.send(chunk)
1066
~/anaconda3/envs/tf/lib/python3.6/http/client.py in send(self, data)
985 try:
--> 986 self.sock.sendall(data)
987 except TypeError:
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
~/anaconda3/envs/tf/lib/python3.6/site-packages/speech_recognition/__init__.py in recognize_google(self, audio_data, key, language, show_all)
839 try:
--> 840 response = urlopen(request, timeout=self.operation_timeout)
841 except HTTPError as e:
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
222 opener = _opener
--> 223 return opener.open(url, data, timeout)
224
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
525
--> 526 response = self._open(req, data)
527
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in _open(self, req, data)
543 result = self._call_chain(self.handle_open, protocol, protocol +
--> 544 '_open', req)
545 if result:
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in http_open(self, req)
1345 def http_open(self, req):
-> 1346 return self.do_open(http.client.HTTPConnection, req)
1347
~/anaconda3/envs/tf/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1319 except OSError as err: # timeout error
-> 1320 raise URLError(err)
1321 r = h.getresponse()
URLError: <urlopen error [Errno 32] Broken pipe>
During handling of the above exception, another exception occurred:
RequestError Traceback (most recent call last)
<ipython-input-41-59c33ef07a2e> in <module>()
----> 1 r.recognize_google(audio)
~/anaconda3/envs/tf/lib/python3.6/site-packages/speech_recognition/__init__.py in recognize_google(self, audio_data, key, language, show_all)
842 raise RequestError("recognition request failed: {}".format(e.reason))
843 except URLError as e:
--> 844 raise RequestError("recognition connection failed: {}".format(e.reason))
845 response_text = response.read().decode("utf-8")
846
RequestError: recognition connection failed: [Errno 32] Broken pipe
答案 0 :(得分:0)
您的链接中描述的方法使用了hack,因此不能保证能够可靠地运行。 Google阻止长文件或过多请求。
Google有一个official API,可用于识别长文件,但可以通过以下方式付费: