使用googlecloudvision库在Python中获取空对象

2024-09-30 12:35:24 发布

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

我目前正在开发一个discord bot,它允许一些命令对google云库进行API调用,比如language和vision。然而,我没有错误,但有时我的图像没有得到标记。我正在使用Python3.7的googlecloudvision库

此命令只返回空列表。 analyseimg("https://cdn.discordapp.com/attachments/294469159004143617/645054741042167838/1.png")

同样的图像现在上传到imgur,并突然返回标签的完整列表。 analyseimg("https://i.imgur.com/crluCYG.png")

from google.cloud import vision


def analyseimg(url):
    client = vision.ImageAnnotatorClient.from_service_account_json('cred.json')
    image = vision.types.Image()
    image.source.image_uri = url
    response = client.label_detection(image=image)
    labels = response.label_annotations
    print('Labels:')
    for label in labels:
        print(label.description, label.score)
    return labels

我有什么特别的东西要看吗

编辑:我编辑了我的代码,现在它正在工作,但我仍然不知道为什么

from google.cloud import vision
from google.cloud.vision import types
import requests
import io


def analyseimg(url):
    client = vision.ImageAnnotatorClient.from_service_account_json('cred.json')
    img = requests.get(url).content
    image = types.Image(content=img)
    response = client.label_detection(image=image)
    labels = response.label_annotations
    print('Labels:')
    for label in labels:
        print(label.description, label.score)
    return labels


Tags: fromimageimportclientjsoncloudurllabels

热门问题