【问题标题】:Google cloud vision api:: AttributeError: 'WebDetection' object has no attribute 'best_guess_labels'谷歌云视觉 api:: AttributeError: 'WebDetection' 对象没有属性 'best_guess_labels'
【发布时间】:2018-12-09 11:54:52
【问题描述】:

我正在尝试使用 python 从 Google Cloud Vision API 调用函数“检测网络”。但是,我无法调用其名为“best_guess_labels”的方法之一。当我尝试调用该方法时,它会抛出一个错误 "AttributeError: 'WebDetection' 对象没有属性 'best_guess_labels':

WebDetection 是一个使用此链接创建并存储到本地文件夹中的 json 文件 ==> https://cloud.google.com/docs/authentication/getting-started

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="WebDetection.json"

“检测网页”功能取自此链接 --> https://cloud.google.com/vision/docs/detecting-web

这是从上述链接复制的功能,供您参考。

def detect_web(path):
    """Detects web annotations given an image."""
    client = vision.ImageAnnotatorClient()

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

image = vision.types.Image(content=content)

response = client.web_detection(image=image)
annotations = response.web_detection

if annotations.best_guess_labels:
    for label in annotations.best_guess_labels:
        print('\nBest guess label: {}'.format(label.label))

if annotations.pages_with_matching_images:
    print('\n{} Pages with matching images found:'.format(
        len(annotations.pages_with_matching_images)))

    for page in annotations.pages_with_matching_images:
        print('\n\tPage url   : {}'.format(page.url))

        if page.full_matching_images:
            print('\t{} Full Matches found: '.format(
                   len(page.full_matching_images)))

            for image in page.full_matching_images:
                print('\t\tImage url  : {}'.format(image.url))

        if page.partial_matching_images:
            print('\t{} Partial Matches found: '.format(
                   len(page.partial_matching_images)))

            for image in page.partial_matching_images:
                print('\t\tImage url  : {}'.format(image.url))

if annotations.web_entities:
    print('\n{} Web entities found: '.format(
        len(annotations.web_entities)))

    for entity in annotations.web_entities:
        print('\n\tScore      : {}'.format(entity.score))
        print(u'\tDescription: {}'.format(entity.description))

if annotations.visually_similar_images:
    print('\n{} visually similar images found:\n'.format(
        len(annotations.visually_similar_images)))

    for image in annotations.visually_similar_images:
        print('\tImage url    : {}'.format(image.url))

但是,当我使用此代码执行上述功能时

detect_web("download.jpg")

我收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-71-6d38dd9b3a76> in <module>()
----> 1 detect_web("download.jpg")

<ipython-input-70-c127dc709a32> in detect_web(path)
     13     annotations = response.web_detection
     14 
---> 15     if annotations.best_guess_labels:
     16         for label in annotations.best_guess_labels:
     17             print('\nBest guess label: {}'.format(label.label))

AttributeError: 'WebDetection' object has no attribute 'best_guess_labels'

我尝试调试,发现“best_guess_labels”不是Json文件的一部分。我不确定 json 文件是否损坏,但我尝试重做练习,但我仍然遇到同样的错误。

可能是什么导致了这个问题?

【问题讨论】:

  • 是你的 google-cloud-vision==0.32.0
  • @TorryYang - 它是 0.29.0
  • 请尝试更新
  • @TorryYang - 我已经更新到 32。但我仍然面临同样的问题。

标签: python-3.x google-cloud-platform google-cloud-vision


【解决方案1】:

我一直在使用google-cloud-vision==0.34.0 和以下代码,我也没有收到任何错误,我在响应中得到了Best guess label: document

import argparse
import io
import re
from google.cloud import vision
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="file.json"

def detect_web(path):
    """Detects web annotations given an image."""
    client = vision.ImageAnnotatorClient()

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

    image = vision.types.Image(content=content)

    response = client.web_detection(image=image)
    annotations = response.web_detection

    if annotations.best_guess_labels:
        for label in annotations.best_guess_labels:
            print('\nBest guess label: {}'.format(label.label))

    if annotations.pages_with_matching_images:
        print('\n{} Pages with matching images found:'.format(
            len(annotations.pages_with_matching_images)))

    for page in annotations.pages_with_matching_images:
        print('\n\tPage url   : {}'.format(page.url))

        if page.full_matching_images:
            print('\t{} Full Matches found: '.format(
                   len(page.full_matching_images)))

            for image in page.full_matching_images:
                print('\t\tImage url  : {}'.format(image.url))

        if page.partial_matching_images:
            print('\t{} Partial Matches found: '.format(
                   len(page.partial_matching_images)))

            for image in page.partial_matching_images:
                print('\t\tImage url  : {}'.format(image.url))
if __name__ == '__main__':
    detect_web("file.jpg")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2019-03-22
    相关资源
    最近更新 更多