我正在构建一个使用Google Cloud Vision的OCR的OCR应用程序。对于大约7-8个请求,OCR工作得很好,之后我得到一个错误,如下所示:
Error: 7 PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the vision.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.问题是,我已经设置了一个计费帐户和一个服务帐户。
我已经尝试使用多个GCloud命令来修复这个问题,当我运行gcloud auth list时,我可以看到我的服务帐户是活动帐户。我还尝试生成一个JSON密钥,并在我的环境变量中设置该密钥的路径-如下所示:https://cloud.google.com/docs/authentication/getting-started
以前有没有人遇到过这个问题?作为参考,我运行的是Windows10,webapp使用的是Node.js。谢谢!
发布于 2020-03-15 21:07:35
您正在使用来自Google Cloud SDK或Google Cloud Shell的最终用户凭据进行身份验证,而不是使用服务帐户凭据。
1.创建一个新目录
mkdir ocr
cd ocr2.下载镜像。
curl https://www.python.org/static/apple-touch-icon-144x144-precomposed.png > image.png3.安装客户端库。
sudo pi3 install --upgrade google-cloud-vision4.创建服务帐号。
gcloud iam service-accounts create ocr-vision \
--description "ocr-vision" \
--display-name "ocr-vision"
gcloud iam service-accounts list5.创建key.json文件。
gcloud iam service-accounts keys create key.json \
--iam-account ocr-vision@your-project.iam.gserviceaccount.com 6.将所有者角色分配给服务帐户。
gcloud projects add-iam-policy-binding your-project \
--member serviceAccount:ocr-vision@your-project.iam.gserviceaccount.com \
--role roles/owner7.导出环境变量
export GOOGLE_APPLICATION_CREDENTIALS=key.json8.运行脚本
python script.pyimport io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('image.png')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)9.Output
Labels:
Yellow
Font
Line
Material property
Clip art
Logo
Symbol
Icon
Graphics
Illustrationhttps://stackoverflow.com/questions/60692859
复制相似问题