Google Api Vision,“请求前”

2024-04-20 10:21:24 发布

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

所以我尝试使用Google的visionapi,它可以识别标签、面部表情和文本检测……等等

但不幸的是,我无法修复导致我们落后的错误。在

源代码

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
from google.cloud import storage

def getCredentials():
    storage_client = storage.Client.from_service_account_json(
            'eyeHear-cad56858f9be.json')
    return storage_client
    # buckets = list(storage_client.list_buckets())
    # print(buckets)

def instantiateClient():
    # Instantiates a client
    client = vision.ImageAnnotatorClient(
        credentials=getCredentials())

    # The name of the image file to annotate
    file_name = os.path.join(
        os.path.dirname(__file__),
        'images/demo-image.jpg')

    # 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)

它说“Client”对象没有属性“before-request”

这就是我被困的地方,我不知道该怎么办。在

错误

^{pr2}$

Tags: thenamefromimageimportclientcloudos
1条回答
网友
1楼 · 发布于 2024-04-20 10:21:24

奇怪,我让它跑起来了。我必须在调用中直接声明凭据的路径操作系统环境. 也许可以尝试一下,而不是使用from storage方法?在

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/jeff/Downloads/test_client.json"

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.join(
    os.path.dirname(__file__),
    'collie.jpg')

# 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)

输出为:

^{pr2}$

输入是

wget https://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Border_Collie_liver_portrait.jpg/220px-Border_Collie_liver_portrait.jpg

然后重命名为牧羊犬.jpg在

相关问题 更多 >