-
Notifications
You must be signed in to change notification settings - Fork 0
/
XML_to_YOLO_test.py
109 lines (88 loc) · 4.4 KB
/
XML_to_YOLO_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import xml.etree.ElementTree as ET
import os
import glob
Class_index = {'bicycle' : 0
,'bus':1
,'car':2
,'carrier':3
,'cat':4
,'dog':5
,'motorcycle':6
,'movable_signage':7
,'person':8
,'scooter':9
,'stroller':10
,'truck':11
,'wheelchair':12
,'barricade':13
,'bench':14
,'bollard':15
,'chair':16
,'fire_hydrant':17
,'kiosk':18
,'parking_meter':19
,'pole':20
,'potted_plant':21
,'power_controller':22
,'stop':23
,'table':24
,'traffic_light':25
,'traffic_light_controller':26
,'traffic_sign':27
,'tree_trunk':28} #Class Annotation을 위한 {클래스 이름 : 라벨 넘버} 1:1 대응 Dictionary
def normalization(center_x, center_y,width,height,img_width,img_height):
center_x = round(center_x / img_width, 6)
center_y = round(center_y / img_height, 6)
width = round(width / img_width, 6)
height = round(height / img_height, 6)
return [center_x,center_y,width,height]
import glob
import time
empty_list = []
exist_list = []
folder_number = 2480 #폴더 갯수, 2480개지만 안전을 위해 저장하고 보관할떈 0으로 적음
for i in range(folder_number):
# xml_name = "C:/Users/dldjw/OneDrive/바탕 화면/Test/images/" # -> 여기 뒤에 Bbox_n 폴더와 xml 파일 찾아서 붙여넣어 파일 이름 완성하면 됨
# xml_name += "Bbox_"+str(i+1).zfill(4)
# print(xml_name)
target = "H:\내 드라이브\Capstone1/train\images\Bbox_{}\*.xml".format(str(i + 1).zfill(4))
xml_list = glob.glob(target)
print("폴더 : Bbox_{}".format(str(i + 1).zfill(4)), xml_list)
# if len(xml_list) == 0 : empty_list.append(str(i+1).zfill(4))
if len(xml_list) != 0:
tree = ET.parse(xml_list[0]) #xml_list[0] 에는 xml 주소가 적혀있음.
root = tree.getroot()
# folder = "H:/내 드라이브/Capstone1/labels/Bbox_{}".format(str(i+1).zfill(4))
# os.mkdir(folder) 무조건 주석 처리 할 것!!!!!!!!!!!!!!! 폴더 생성하는 코드
# time.sleep(1)
exist_list.append("train/images/Bbox_{}".format(str(i + 1).zfill(4)))
for image in root.findall('image'):
#Image 이름 불러오기
image_fullname = image.attrib['name']
image_name, ext = os.path.splitext(image_fullname)
print("파일 이름 : ", image.attrib['name'])
# Image size 정보(기본 1920*1080) 이지만 Just in case
image_width = int(image.attrib['width']) # 1920(픽셀)
image_height = int(image.attrib['height']) # 1080(픽셀)
#label 위치할 주소 받아오기
label_context=""
for box in image.findall('box'): # 라벨 내용 한줄 한줄 작성하는 코드
# print("label name : ", box.attrib['label']) #-> box.attrib는 dictionary 타입이므로 이런 식으로 접근 가능!
# text = str(Class_index[box.attrib['label']]) +" " + box.attrib['xtl']+" " + box.attrib['ytl']+" " + box.attrib['xbr']+" " + box.attrib['ybr']
# print("XML Format : ",text)
''' center_x = round((float(box.attrib['xtl']) + float(box.attrib['xbr'])) / 2 , 6)
center_y = round((float(box.attrib['ytl']) + float(box.attrib['ybr'])) / 2 , 6)
width = round((float(box.attrib['xbr']) - float(box.attrib['xtl'])) , 6)
height = round((float(box.attrib['ybr']) - float(box.attrib['ytl'])) , 6)
center_x, center_y, width, height = normalization(center_x,center_y,width,height,image_width,image_height)
# print("{} {} {} {} {}".format(str(Class_index[box.attrib['label']]), center_x,center_y,width,height)) # print("YOLO Format : {} {} {} {} {}".format(str(Class_index[box.attrib['label']]), center_x,center_y,width,height))
label_context += "{} {} {} {} {}\n".format(str(Class_index[box.attrib['label']]), center_x,center_y,width,height)
print("label : {}\n{}".format(image_name.rstrip('jpg')+'.txt',label_context))
label_name = "{}.txt".format(image_name)
label_location = "H:/내 드라이브/Capstone1/labels/Bbox_{}/{}".format(str(i + 1).zfill(4),label_name)
file = open(label_location,'w')
file.write(label_context)
file.close()'''
else:
empty_list.append("Bbox_{}".format(str(i + 1).zfill(4)))
print(*exist_list, sep=',')