Format OCR text annotation from Cloud Vision API in Python

前端 未结 1 578
我在风中等你
我在风中等你 2021-02-09 23:32

I am using the Google Cloud Vision API for Python on a small program I\'m using. The function is working and I get the OCR results, but I need to format these before being able

1条回答
  •  -上瘾入骨i
    2021-02-10 00:01

    You are almost right there. As you want to slice the text line by line, instead of looping the text annotations, try to get the direct 'description' from google vision's response as shown below.

    def parse_image(image_path=None):
        """
        Parse the image using Google Cloud Vision API, Detects "document" features in an image
        :param image_path: path of the image
        :return: text content
        :rtype: str
        """
    
        client = vision.ImageAnnotatorClient()
        response = client.text_detection(image=open(image_path, 'rb'))
        text = response.text_annotations
        del response     # to clean-up the system memory
    
        return text[0].description
    

    The above function returns a string with the content in the image, with the lines separated by "\n"

    Now, you can add prefix & suffix as you need to each line.

    image_content = parse_image(image_path="path\to\image")
    
    my_formatted_text = ""
    for line in image_content.split("\n"):
        my_formatted_text += "    " + line + "\n"
    

    my_formatted_text is the text you need.

    0 讨论(0)
提交回复
热议问题