-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathvoc_to_YOLOv3_NO_CLASSES.py
71 lines (59 loc) · 1.92 KB
/
voc_to_YOLOv3_NO_CLASSES.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
66
67
68
69
70
71
import xml.etree.ElementTree as ET
from os import getcwd
import os
save_folder = 'model_data\\'
dataset_train = 'CSGO_images\\'
dataset_file = save_folder+'NO_CLASSES.txt'
classes_file = dataset_file[:-4]+'_classes.txt'
CLS = os.listdir(dataset_train)
classes =[dataset_train+CLASS for CLASS in CLS]
wd = getcwd()
CLASSES = []
def GetClassesNames(fullname):
global CLASSES
in_file = open(fullname)
tree=ET.parse(in_file)
root = tree.getroot()
for i, obj in enumerate(root.iter('object')):
name = obj.find('name').text
if name not in CLASSES:
CLASSES.append(name)
def test(fullname):
bb = ""
in_file = open(fullname)
tree=ET.parse(in_file)
root = tree.getroot()
filename = root.find('filename').text
for i, obj in enumerate(root.iter('object')):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in CLS or int(difficult)==1:
continue
cls_id = CLS.index(cls)
xmlbox = obj.find('bndbox')
b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
bb += (" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
if bb != "":
list_file = open(dataset_file, 'a')
file_string = str(fullname)[:-4]+filename[-4:]+bb+'\n'
list_file.write(file_string)
list_file.close()
for CLASS in classes:
if not CLASS.endswith('.xml'):
continue
fullname = os.getcwd()+'\\'+CLASS#+'\\'+filename
print(fullname)
GetClassesNames(fullname)
CLASSES.sort()
CLS = CLASSES.copy()
print(CLS)
for CLASS in classes:
if not CLASS.endswith('.xml'):
continue
fullname = os.getcwd()+'\\'+CLASS#+'\\'+filename
test(fullname)
for CLASS in CLS:
list_file = open(classes_file, 'a')
file_string = str(CLASS)+"\n"
list_file.write(file_string)
list_file.close()