Doubt Dhanabalu
Doubt Dhanabalu

Reputation: 457

Google cloud vision api:: AttributeError: 'WebDetection' object has no attribute 'best_guess_labels'

I am trying to call the function "detect web" from Google Cloud Vision API using python. However I am not able to call one of its method named "best_guess_labels". When I tried to call the method, it throws out an error as "AttributeError: 'WebDetection' object has no attribute 'best_guess_labels':

WebDetection is a json file that was created using this link and stored into a local folder ==> https://cloud.google.com/docs/authentication/getting-started

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

The function of "detect web" is taken from this link --> https://cloud.google.com/vision/docs/detecting-web

Here is the function copied from the above link for your ready reference.

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

However, When i execute the above function using this code

detect_web("download.jpg")

I am getting the below error:

---------------------------------------------------------------------------
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'

I tried to debugging and found that the "best_guess_labels" is not part of the Json file. I am not sure whether the json file got corrupted, but i tried to redo the exercise, but i still getting the same error.

What might have caused the issue?

Upvotes: 0

Views: 692

Answers (1)

F10
F10

Reputation: 2893

I've been using google-cloud-vision==0.34.0 with the following code and I got no errors as well I'm getting a Best guess label: document within the response:

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

Upvotes: 1

Related Questions