将Python转换为PHP GCP授权凭证

2024-05-18 23:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用gcpcloudvisionapi和Python从图像中检索一些信息。具体地说,我向这个API发送一个产品的照片,然后检索与之相关的web实体;其中一个几乎总是品牌。在

执行此工作的基本Python脚本如下:

import  io
from google.cloud import vision
from google.cloud.vision import types
import os
import cv2
import numpy as np

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_name.json"

def detect_text(file):

    client = vision.ImageAnnotatorClient()

    with io.open(file, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)
    web_detection = client.web_detection(image=image).web_detection
    print(web_detection)

file_name = "/Users/User/Desktop/Image.jpg"
img = cv2.imread(file_name)
detect_text(file_name)

Project_name.json文件包含一些授权凭证,以便我可以访问googlecloudapi客户端。在

我想用PHP做同样的事情。因此,到目前为止,我编写了以下PHP脚本:

^{pr2}$

但是当我运行这个程序时,我得到了以下错误:

Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\V1\ImageAnnotatorClient' not found in /opt/lampp/htdocs/index.php:12 Stack trace: #0 /opt/lampp/htdocs/index.php(26): Google\Cloud\Samples\Vision\detect_web('/Users/User...') #1 {main} thrown in /opt/lampp/htdocs/index.php on line 12

显然,出现此错误是因为(首先)我没有像在上面的Python中一样提供Google授权凭证(os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/User/PycharmProjects/Project_name.json")。在

我应该通过我的PHP脚本和我的脚本在Google中得到同样的结果?在


Tags: nameimageimportproject脚本webjsonos
1条回答
网友
1楼 · 发布于 2024-05-18 23:32:17

您可以create a service account并选择JSON作为验证yocloudvisionapi的密钥类型。提供了设置身份验证的步骤here。一旦有了JSON密钥文件,就可以使用指向JSON密钥文件的“export GOOGLE_APPLICATION_CREDENTIALS”命令设置环境变量。您可以将客户端库的PHP version用于云视觉API。这里还有一个PHP API Reference documentation以获得其他帮助。在

相关问题 更多 >

    热门问题