如何解决如何使用 opencv 和 pytesseract python 提取文本?
我正在使用 labelImg 在图像行上绘制一个矩形。这给了我 xml 文件。借助此 xml 如何从图像表中提取该文本。为了提取文本,我使用了水平和垂直线检测,但没有得到好的结果。现在我正在使用 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>
我的输入图像:
解决方法
要获得 xmin
,您可以将 xpath()
与 '//annotation/object/bndbox/xmin'
或更短的 '//xmin'
结合使用
它总是给出列表(即使只有一个元素或没有元素)所以它需要 [0]
来获取第一个元素或 for
循环来处理所有元素。>
使用 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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。