【问题标题】:how to extract text using opencv and pytesseract python?如何使用opencv和pytesseract python提取文本?
【发布时间】:2021-05-13 17:00:59
【问题描述】:

我正在使用 labelImg 在图像行上绘制一个矩形。这给了我 xml 文件。借助此 xml 如何从图像表中提取该文本。为了提取文本,我使用了水平和垂直 ine 检测,但没有得到好的结果。现在我正在使用 labelImg 它给了我想要提取的文本的点,但我不知道如何应用这个方法。请告诉我该怎么做?

我的 xml 文件:

    <annotation>
      <folder>Test Images</folder>
      <filename>FreKa.jpg</filename>
      <path>/home/sumit/Desktop/office_works/Fusion_Code/BIS_Final/Test Images/FreKa.jpg</path>
      <source>
         <database>Unknown</database>
        </source>
      <size>
         <width>679</width>
         <height>341</height>
         <depth>3</depth>
         </size>
         <segmented>0</segmented>
       <object>
         <name>Contact Type</name>
         <pose>Unspecified</pose>
         <truncated>1</truncated>
         <difficult>0</difficult>
         <bndbox>
           <xmin>1</xmin>
           <ymin>100</ymin>
           <xmax>678</xmax>
           <ymax>157</ymax>
        </bndbox>
       </object>
       </annotation>

我的输入图像:

如何借助 xml 文件从表格中提取合同类型? 谢谢...

【问题讨论】:

  • 你想获取 xml 中的哪个值?您可以为此在模块lxml 中使用xpath() - 即。 '//annotation/object/bndbox/xmin'
  • 或者你可以使用正则表达式 - re.findall('&lt;xmin&gt;(\d+)&lt;/xmin&gt;', text)
  • 借助 xml 我想提取图像的第 3 行。
  • 你能告诉我如何使用xml获取对象名称(例如合同,合同描述等)吗?
  • 如果你的意思是&lt;object&gt;,那么//annotation/object应该给你所有&lt;object&gt;的列表,然后你可以使用len(list_with_objects)。或者您可以对 //annotation/object/name 执行相同操作 - 它应该为您提供所有名称的列表,您可以使用 len(list_with_names)

标签: python opencv python-tesseract image-preprocessing labelimg


【解决方案1】:

要获得xmin,您可以使用xpath()'//annotation/object/bndbox/xmin' 或更短的'//xmin'

它总是给出列表(即使只有一个元素或没有元素),所以它需要[0] 来获取第一个元素或for-loop 来处理所有元素。

使用if list_of_elelemts: ...,只有当列表有一些元素时才能运行代码。

您也可以使用len() 来检查您获得了多少元素。

text = '''
<annotation>
  <folder>Test Images</folder>
  <filename>FreKa.jpg</filename>
  <path>/home/sumit/Desktop/office_works/Fusion_Code/BIS_Final/Test Images/FreKa.jpg</path>
  <source>
     <database>Unknown</database>
  </source>
  <size>
     <width>679</width>
     <height>341</height>
     <depth>3</depth>
  </size>
  <segmented>0</segmented>
  <object>
     <name>Contact Type</name>
     <pose>Unspecified</pose>
     <truncated>1</truncated>
     <difficult>0</difficult>
     <bndbox>
       <xmin>1</xmin>
       <ymin>100</ymin>
       <xmax>678</xmax>
       <ymax>157</ymax>
     </bndbox>
  </object>
</annotation>
'''

import lxml.etree

tree = lxml.etree.fromstring(text)

print('xmin:', tree.xpath("//annotation/object/bndbox/xmin")[0].text)
print('xmin:', tree.xpath("//bndbox/xmin")[0].text)
print('xmin:', tree.xpath("//object//xmin")[0].text)
print('xmin:', tree.xpath("//xmin")[0].text)

print('xmin:', tree.xpath("//xmin/text()")[0])  # with `text()` instead of `.text`

for item in tree.xpath("//xmin/text()"):
    print('xmin:', item)  # with `text()` instead of `.text`

objects = tree.xpath("//object")
print('len(objects):', len(objects))

other = tree.xpath("//bndbox/other")
if other:
    print('found', len(other), 'elements')
else:
    print('there is no "other" elements')

【讨论】:

  • 嗨@furas,你能告诉我如何计算边界框内的行数吗?
  • 我不知道我是否理解问题:首先您必须将框转换为字符串,然后您必须检查是否有一些印地语字符。或者检查是否有与英文字符不同的字符 - 它可能更简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 2020-06-15
相关资源
最近更新 更多