简体   繁体   English

IndexError:带有 Google Cloud Vision API 的字节数组

[英]IndexError: bytearray with Google Cloud Vision API

I am trying to use the Google Cloud Vision API to detect text in an image.我正在尝试使用 Google Cloud Vision API 检测图像中的文本。 I followed the code in the following tutorial: https://cloud.google.com/vision/docs/fulltext-annotations我按照以下教程中的代码进行操作: https : //cloud.google.com/vision/docs/fulltext-annotations

The full code is below:完整代码如下:

import argparse
from enum import Enum
import io

from google.cloud import vision
from PIL import Image, ImageDraw

class FeatureType(Enum):
    PAGE = 1
    BLOCK = 2
    PARA = 3
    WORD = 4
    SYMBOL = 5


def draw_boxes(image, bounds, color):
    """Draw a border around the image using the hints in the vector list."""
    draw = ImageDraw.Draw(image)

    for bound in bounds:
        draw.polygon([
            bound.vertices[0].x, bound.vertices[0].y,
            bound.vertices[1].x, bound.vertices[1].y,
            bound.vertices[2].x, bound.vertices[2].y,
            bound.vertices[3].x, bound.vertices[3].y], None, color)
    return image


def get_document_bounds(image_file, feature):
    """Returns document bounds given an image."""
    client = vision.ImageAnnotatorClient()

    bounds = []

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

    image = vision.Image(content=content)

    response = client.document_text_detection(image=image)
    document = response.full_text_annotation

    # Collect specified feature bounds by enumerating all document features
    for page in document.pages:
        for block in page.blocks:
            for paragraph in block.paragraphs:
                for word in paragraph.words:
                    for symbol in word.symbols:
                        if (feature == FeatureType.SYMBOL):
                            bounds.append(symbol.bounding_box)

                    if (feature == FeatureType.WORD):
                        bounds.append(word.bounding_box)

                if (feature == FeatureType.PARA):
                    bounds.append(paragraph.bounding_box)

            if (feature == FeatureType.BLOCK):
                bounds.append(block.bounding_box)

    # The list `bounds` contains the coordinates of the bounding boxes.
    return bounds


def render_doc_text(filein, fileout):
    image = Image.open(filein)
    bounds = get_document_bounds(filein, FeatureType.BLOCK)
    draw_boxes(image, bounds, 'red')
    bounds = get_document_bounds(filein, FeatureType.PARA)
    draw_boxes(image, bounds, 'red')
    bounds = get_document_bounds(filein, FeatureType.WORD)
    draw_boxes(image, bounds, 'red')

    if fileout != 0:
        image.save(fileout)
    else:
        image.show()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('detect_file', help='The image for text detection.')
    parser.add_argument('-out_file', help='Optional output file', default=0)
    args = parser.parse_args()

    render_doc_text(args.detect_file, args.out_file)

I am using Windows 10 with Python 3.7 and use the following code in the command prompt:我将 Windows 10 与 Python 3.7 一起使用,并在命令提示符中使用以下代码:

C:\Users\ariel\Dropbox\Research\Mestizo\Code>python doctext.py censo_19940_tab_corta-30.png -out_file out.jpg

And I get the following error and traceback:我收到以下错误和回溯:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImagePalette.py", line 99, in getcolor
    return self.colors[color]
KeyError: (255, 0, 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "doctext.py", line 96, in <module>
    render_doc_text(args.detect_file, args.out_file)
  File "doctext.py", line 78, in render_doc_text
    draw_boxes(image, bounds, 'red')
  File "doctext.py", line 35, in draw_boxes
    bound.vertices[3].x, bound.vertices[3].y], None, color)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageDraw.py", line 239, in polygon
    ink, fill = self._getink(outline, fill)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageDraw.py", line 113, in _getink
    ink = self.palette.getcolor(ink)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImagePalette.py", line 109, in getcolor
    self.palette[index + 256] = color[1]
IndexError: bytearray index out of range

I have looked through previous posts about this error but I can't figure out where this is coming from.我已经查看了以前关于此错误的帖子,但我无法弄清楚这是从哪里来的。

The issue was that I was using a png file when I was supposed to use a jpg.问题是我应该使用 jpg 时使用的是 png 文件。 The documentation of the code/tutorial uses a jpg file as an input:代码/教程的文档使用 jpg 文件作为输入:

https://cloud.google.com/vision/docs/fulltext-annotations https://cloud.google.com/vision/docs/fulltext-annotations

After converting to a jpg, the code ran without problems and produced the expected output.转换为 jpg 后,代码运行没有问题,并产生了预期的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM