我刚刚在Google Cloud Platform上注册了免费套餐帐户,并获得了用于复制此代码的api密钥:
import os
import speech_recognition as sr
from tqdm import tqdm
with open("api-key.json") as f:
GOOGLE_CLOUD_SPEECH_CREDENTIALS = f.read()
r = sr.Recognizer()
files = sorted(os.listdir('parts/'))
all_text = []
for f in tqdm(files):
name = "parts/" + f
# Load audio file
with sr.AudioFile(name) as source:
audio = r.record(source)
# Transcribe audio file
text = r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)
all_text.append(text)
transcript = ""
for i, t in enumerate(all_text):
total_seconds = i * 30
# Cool shortcut from:
# https://stackoverflow.com/questions/775049/python-time-seconds-to-hms
# to get hours, minutes and seconds
m, s = divmod(total_seconds, 60)
h, m = divmod(m, 60)
# Format time as h:m:s - 30 seconds of text
transcript = transcript + "{:0>2d}:{:0>2d}:{:0>2d} {}\n".format(h, m, s, t)
print(transcript)
with open("transcript.txt", "w") as f:
f.write(transcript)
不幸的是,Pycharm向我返回了我在任何地方都找不到的错误:
line 20, in <module>
text = r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 920, in recognize_google_cloud
raise RequestError("missing google-api-python-client module: ensure that google-api-python-client is set up correctly.")
speech_recognition.RequestError: missing google-api-python-client module: ensure that google-api-python-client is set up correctly.
所以我尝试安装和升级google-api-python-client但没有任何变化。
答案 0 :(得分:0)
我发现没有为该项目启用Cloud Speech API。这可以在https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=PASTE_YOUR_PROJECT_ID_HERE
上完成答案 1 :(得分:0)
我也遇到了同样的错误。这是因为使用了“ google-api-python-client”版本。将google-api-python-client安装为:
pip install google-api-python-client==1.6.4
答案 2 :(得分:0)
对于我来说,安装oauth2client
可以解决问题。
对于python:
sudo pip install oauth2client
对于python3:
sudo pip3 install oauth2client