-
Notifications
You must be signed in to change notification settings - Fork 5
/
xml_to_yolo.py
65 lines (52 loc) · 2.11 KB
/
xml_to_yolo.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import xml.etree.ElementTree as ET
import os
import cv2
nc = open("100/class.names") #class name 가져오기
name_class = dict()
for idx, i in enumerate(nc): #class들 배열에 input
name_class[i.replace('\n','')] = str(idx)
pathes = "100/" #setting 경로
rr = open(pathes + 'train.txt','w')
for path, dir, apple in os.walk(pathes):
if len(dir)>0:
continue
folderList = os.listdir(path)
print(path)
path = path + '/'
r = open(path + 'train.txt','w')
for i in folderList :
print(i)
if i.split('.')[1] == 'jpg':
r.write(path + i)
rr.write(path + i)
r.write('\n')
rr.write('\n')
continue
if i.split('.')[1] == 'xml':
tree = ET.parse(path+i)
root = tree.getroot()
img = cv2.imread(path+i.split('.')[0] + '.jpg')
width = img.shape[1]
height = img.shape[0]
width2 = int(root.find('size').find('width').text)
height2 = int(root.find('size').find('height').text)
if not width == width2:
print(i)
#print(i)
f = open(path+i.split('.')[0] + '.txt','w')
for j in root.iter(tag = 'object'):
label_text = j.find('name').text
f.write(name_class[label_text])
f.write(' ')
xmin = int(j.find('bndbox').find('xmin').text)
xmax = int(j.find('bndbox').find('xmax').text)
ymin = int(j.find('bndbox').find('ymin').text)
ymax = int(j.find('bndbox').find('ymax').text)
f.write(str(round(((xmin + xmax) / 2) / width,5)))
f.write(' ')
f.write(str(round(((ymin + ymax) / 2) / height,5)))
f.write(' ')
f.write(str(round(((xmax - xmin)) / width,5)))
f.write(' ')
f.write(str(round(((ymax - ymin)) / height,5)))
f.write('\n')