-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmltotxt.py
More file actions
42 lines (30 loc) · 1.05 KB
/
xmltotxt.py
File metadata and controls
42 lines (30 loc) · 1.05 KB
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
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 22 15:00:58 2019
@author: Dufert
"""
import os
import xml.etree.ElementTree as ET
import glob
def xml_to_txt(indir,outdir):
os.chdir(indir)#进入当前目录
annotations = os.listdir('.')
annotations = glob.glob(str(annotations)+'*.xml')
for i, file in enumerate(annotations):
file_save = file.split('.')[0]+'.txt'
file_txt=os.path.join(outdir,file_save)
f_w = open(file_txt,'w')
# actual parsing
in_file = open(file)
tree=ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('object'):
xmlbox = obj.find('bndbox')
xn = xmlbox.find('xmin').text
xx = xmlbox.find('xmax').text
yn = xmlbox.find('ymin').text
yx = xmlbox.find('ymax').text
f_w.write(xn+' '+yn+' '+xx+' '+yx+' ')
indir='g:/CV_Library/Winding_data/winding3/train/xml/' #xml目录
outdir='g:/CV_Library/Winding_data/winding3/train/txt/' #txt目录
xml_to_txt(indir,outdir)