0

我正在使用 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 文件从表中提取合同类型?谢谢...

4

1 回答 1

0

为了让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')
于 2021-05-14T14:27:38.080 回答