Skip to main content
 首页 » 编程设计

python之对从 Google Vision API 中提取为完整单词的文本进行分组

2025年02月15日8oomusou

我正在尝试通过 Google Vision API 重现“文档文本检测”示例 UI uploader 的输出。但是,我从 sample code 得到的输出当我需要将单词组合在一起时,仅提供单个字符作为输出。

库中是否有允许按“单词”而不是 DOCUMENT_TEXT_DETECT 端点或 Python 中的 image.detect_full_text() 函数分组的功能?

我不是在寻找全文提取,因为我的 .jpg 文件的视觉结构不是 image.detect_text() 函数所满足的。

Google 的示例代码:

def detect_document(path): 
    """Detects document features in an image.""" 
    vision_client = vision.Client() 
 
    with io.open(path, 'rb') as image_file: 
        content = image_file.read() 
 
    image = vision_client.image(content=content) 
 
    document = image.detect_full_text() 
 
    for page in document.pages: 
        for block in page.blocks: 
            block_words = [] 
            for paragraph in block.paragraphs: 
                block_words.extend(paragraph.words) 
 
            block_symbols = [] 
            for word in block_words: 
                block_symbols.extend(word.symbols) 
 
            block_text = '' 
            for symbol in block_symbols: 
                block_text = block_text + symbol.text 
 
            print('Block Content: {}'.format(block_text)) 
            print('Block Bounds:\n {}'.format(block.bounding_box)) 

Google 提供的现成示例的示例输出:

property { 
  detected_languages { 
    language_code: "mt" 
  } 
} 
bounding_box { 
  vertices { 
    x: 1193 
    y: 1664 
  } 
  vertices { 
    x: 1206 
    y: 1664 
  } 
  vertices { 
    x: 1206 
    y: 1673 
  } 
  vertices { 
    x: 1193 
    y: 1673 
  } 
} 
symbols { 
  property { 
    detected_languages { 
      language_code: "en" 
    } 
  } 
  bounding_box { 
    vertices { 
      x: 1193 
      y: 1664 
    } 
    vertices { 
      x: 1198 
      y: 1664 
    } 
    vertices { 
      x: 1198 
      y: 1673 
    } 
    vertices { 
      x: 1193 
      y: 1673 
    } 
  } 
  text: "P" 
} 
symbols { 
  property { 
    detected_languages { 
      language_code: "en" 
    } 
    detected_break { 
      type: LINE_BREAK 
    } 
  } 
  bounding_box { 
    vertices { 
      x: 1200 
      y: 1664 
    } 
    vertices { 
      x: 1206 
      y: 1664 
    } 
    vertices { 
      x: 1206 
      y: 1673 
    } 
    vertices { 
      x: 1200 
      y: 1673 
    } 
  } 
  text: "M" 
} 
 
 
block_words 
Out[47]:  
[property { 
   detected_languages { 
     language_code: "en" 
   } 
 } 
 bounding_box { 
   vertices { 
     x: 1166 
     y: 1664 
   } 
   vertices { 
     x: 1168 
     y: 1664 
   } 
   vertices { 
     x: 1168 
     y: 1673 
   } 
   vertices { 
     x: 1166 
     y: 1673 
   } 
 } 
 symbols { 
   property { 
     detected_languages { 
       language_code: "en" 
     } 
   } 
   bounding_box { 
     vertices { 
       x: 1166 
       y: 1664 
     } 
     vertices { 
       x: 1168 
       y: 1664 
     } 
     vertices { 
       x: 1168 
       y: 1673 
     } 
     vertices { 
       x: 1166 
       y: 1673 
     } 
   } 
   text: "2" 
 } 

请您参考如下方法:

此回复来晚了。我猜您正在寻找类似下面的内容。

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 
 
    return text[0].description 

该函数返回图像中的完整文本。