首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Cloud Vision OCR错误代码7-权限被拒绝

Google Cloud Vision OCR错误代码7-权限被拒绝
EN

Stack Overflow用户
提问于 2020-03-15 20:26:27
回答 1查看 574关注 0票数 1

我正在构建一个使用Google Cloud Vision的OCR的OCR应用程序。对于大约7-8个请求,OCR工作得很好,之后我得到一个错误,如下所示:

代码语言:javascript
复制
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。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-15 21:07:35

您正在使用来自Google Cloud SDK或Google Cloud Shell的最终用户凭据进行身份验证,而不是使用服务帐户凭据。

1.创建一个新目录

代码语言:javascript
复制
mkdir ocr
cd ocr

2.下载镜像。

代码语言:javascript
复制
curl https://www.python.org/static/apple-touch-icon-144x144-precomposed.png > image.png

3.安装客户端库。

代码语言:javascript
复制
sudo pi3 install --upgrade google-cloud-vision

4.创建服务帐号。

代码语言:javascript
复制
gcloud iam service-accounts create ocr-vision \
      --description "ocr-vision" \
      --display-name "ocr-vision"

gcloud iam service-accounts list

5.创建key.json文件。

代码语言:javascript
复制
gcloud iam service-accounts keys create key.json \
      --iam-account ocr-vision@your-project.iam.gserviceaccount.com 

6.将所有者角色分配给服务帐户。

代码语言:javascript
复制
gcloud projects add-iam-policy-binding your-project \
      --member serviceAccount:ocr-vision@your-project.iam.gserviceaccount.com \
      --role roles/owner

7.导出环境变量

代码语言:javascript
复制
export GOOGLE_APPLICATION_CREDENTIALS=key.json

8.运行脚本

代码语言:javascript
复制
 python script.py
代码语言:javascript
复制
import 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

代码语言:javascript
复制
Labels:
Yellow
Font
Line
Material property
Clip art
Logo
Symbol
Icon
Graphics
Illustration
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60692859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档