为什么在使用Google Vision时出现列表索引超出范围错误?

问题描述

这是一个分为两部分的问题。首先是找出超出范围的错误

我使用的是直接来自Google Api代码

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

针对Tif图像运行此命令时,出现以下错误

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

但是,如果我对保存为Jpeg的同一张图片运行它,则它将按预期工作。这不是我最初想到的文件大小错误,因为那是完全不同的错误消息。


我的最终目标是能够检测裁剪提示,并使用我选择的边界框裁剪到该框。有人知道这是否可能吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)