我有以下代码
import speech_recognition as sr
filename = 'audio.flac'
r = sr.Recognizer()
with sr.AudioFile(filename) as source:
print('Recording started....')
audio_data = r.record(source)
print('Recording completed....')
with open(service_auth_file) as f:
text = r.recognize_google_cloud(audio_data)
print('completed the recognition')
print(text)
它需要一个按名称命名的环境变量GOOGLE_APPLICATION_CREDENTIAL
。参考:https ://cloud.google.com/speech-to-text/docs/reference/libraries 。所以我添加了包含以下数据的文件的位置(仅提及 JSON 文件中的键,因为其他信息是机密的)
{
"type": "service_account",
"project_id": "PROJECT_NAME",
"private_key_id": "PROJECT_KEY",
"private_key": "PRIVATE_KEY",
"client_email": "CLIENT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "AUTH_URI",
"token_uri": "TOKEN_URI",
"auth_provider_x509_cert_url": "AUTH_CERT_URL",
"client_x509_cert_url": "CLIENT_CERT_URL"
}
但是当我运行上面的代码时,我得到了以下错误
Traceback (most recent call last):
File "./speech_recognizer.py", line 23, in <module>
text = r.recognize_google_cloud(audio_data)
File "/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/speech_recognition/__init__.py", line 800, in recognize_google_cloud
speech_service = build("speech", "v1beta1", credentials=api_credentials)
File "/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/googleapiclient/discovery.py", line 233, in build
raise UnknownApiNameOrVersion(
googleapiclient.errors.UnknownApiNameOrVersion: name: speech version: v1beta1
当我搜索错误时,我发现了一个名为gapic-google-cloud-speech-v1beta1 的包。因此,也尝试在安装文件后运行该文件。但仍然得到同样的错误。