Why am I getting a list index out of range error when using Google Vision?

243 Views Asked by At

This is a two-part question. The first is figuring out the out of range error.

Im using code straight from the Google API source.

import argparse
import io
import os
from numpy import random
import pandas as pd
from pillow_utility import draw_borders, Image
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image, ImageDraw

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="<path to json token>"

def get_crop_hint(path):
#Detect crop hints on a single image and return the first result.
client = vision.ImageAnnotatorClient()

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

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

crop_hints_params = vision.types.CropHintsParams(aspect_ratios=[1])
image_context = vision.types.ImageContext(crop_hints_params=crop_hints_params)

response = client.crop_hints(image=image, image_context=image_context)
hints = response.crop_hints_annotation.crop_hints

# Get bounds for the first crop hint using an aspect ratio of 1:1
vertices = hints[0].bounding_poly.vertices

return vertices

When I run this against a Tif image I get the following error:

File "googlecrop.py", line 31, in get_crop_hint
vertices = hints[0].bounding_poly.vertices
IndexError: list index (0) out of range

However, if I run it against the same image saved out as a Jpeg, it works as expected. It's not a file size error as I first thought because that is an entirely different error message.


My end goal is to be able to detect the crop hints, and by using a bounding box of my choice, crop to that box. Does anyone know if this is possible?

0

There are 0 best solutions below