forked from LZQ-RSer/RS_Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_to_txt.py
35 lines (35 loc) · 1.22 KB
/
xml_to_txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
# @Time : 2021/2/4 23:03
# @Author : lzq
# @Site :
# @File : xml_to_txt.py
# @Software: PyCharm Community Edition
import sys
import os
import glob
import xml.etree.ElementTree as ET
def xml_to_txt(xml,outfile):
with open(outfile, "w") as new_f:
root = ET.parse(xml).getroot()
filename = root.find('filename').text
size = root.find('size')
width = size.find('width').text
height = size.find('height').text
new_f.write("%s %s,%s "%(filename,width,height))#注意空格
for obj in root.findall('object'):
if obj.find('difficult')!=None:
difficult = obj.find('difficult').text
if int(difficult)==1:
continue
obj_name = obj.find('name').text
bndbox = obj.find('bndbox')
left = bndbox.find('xmin').text
top = bndbox.find('ymin').text
right = bndbox.find('xmax').text
bottom = bndbox.find('ymax').text
new_f.write("%s,%s,%s,%s,%s " % (left, top, right, bottom,obj_name))
new_f.write('\n')
if __name__ == '__main__':
xml = "./test/aircraft_79.xml"
outfile = './test/aircraft_79.txt'
xml_to_txt(xml,outfile)