-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_labels_to_yolo_format.py
134 lines (107 loc) · 4.76 KB
/
1_labels_to_yolo_format.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import glob, os
import os.path
import time
from shutil import copyfile
import cv2
from xml.dom import minidom
from os.path import basename
#from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
#--------------------------------------------------------------------
folderCharacter = "/" # \\ is for windows
#xmlFolder = "/DATA1/Datasets_mine/labeled/crowd_human_dataset/labels"
#imgFolder = "/DATA1/Datasets_mine/labeled/crowd_human_dataset/images"
#negFolder = "/home/chtseng/datasets/12_hand_gestures/negatives"
#saveYoloPath = "/DATA1/Datasets_mine/labeled/crowd_human_dataset/yolo"
#classList = { "person_head":0, "person_vbox":1, "person_fbox":2 }
xmlFolder = "/home/xuan/pos_bread/xmlfile/"
imgFolder = "/home/xuan/pos_bread/breads_modle/"
negFolder = ""
saveYoloPath = "/home/xuan/pos_bread/yolo"
classList = { "b01":1, "b02":2, "b03":3,"b04":4, "b05":5, "b06":6, "b07":7, "b08":8 }
print("x: load")
#---------------------------------------------------------------------
if not os.path.exists(saveYoloPath):
os.makedirs(saveYoloPath)
print("x: no saveyoli")
def transferYolo( xmlFilepath, imgFilepath):
global imgFolder
img_file, img_file_extension = os.path.splitext(imgFilepath)
img_filename = basename(img_file)
print(imgFilepath)
if(xmlFilepath is not None):
img = cv2.imread(imgFilepath)
imgShape = img.shape
print (img.shape)
img_h = imgShape[0]
img_w = imgShape[1]
labelXML = minidom.parse(xmlFilepath)
labelName = []
labelXmin = []
labelYmin = []
labelXmax = []
labelYmax = []
totalW = 0
totalH = 0
countLabels = 0
tmpArrays = labelXML.getElementsByTagName("filename")
for elem in tmpArrays:
filenameImage = elem.firstChild.data
tmpArrays = labelXML.getElementsByTagName("name")
for elem in tmpArrays:
labelName.append(str(elem.firstChild.data))
tmpArrays = labelXML.getElementsByTagName("xmin")
for elem in tmpArrays:
labelXmin.append(int(elem.firstChild.data))
tmpArrays = labelXML.getElementsByTagName("ymin")
for elem in tmpArrays:
labelYmin.append(int(elem.firstChild.data))
tmpArrays = labelXML.getElementsByTagName("xmax")
for elem in tmpArrays:
labelXmax.append(int(elem.firstChild.data))
tmpArrays = labelXML.getElementsByTagName("ymax")
for elem in tmpArrays:
labelYmax.append(int(elem.firstChild.data))
yoloFilename = os.path.join(saveYoloPath, img_filename + ".txt")
print("writeing to {}".format(yoloFilename))
with open(yoloFilename, 'a') as the_file:
i = 0
for className in labelName:
if(className in classList):
classID = classList[className]
x = (labelXmin[i] + (labelXmax[i]-labelXmin[i])/2) * 1.0 / img_w
y = (labelYmin[i] + (labelYmax[i]-labelYmin[i])/2) * 1.0 / img_h
w = (labelXmax[i]-labelXmin[i]) * 1.0 / img_w
h = (labelYmax[i]-labelYmin[i]) * 1.0 / img_h
the_file.write(str(classID) + ' ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
i += 1
else:
yoloFilename = os.path.join(saveYoloPath ,img_filename + ".txt")
print("writeing negative file to {}".format(yoloFilename))
with open(yoloFilename, 'a') as the_file:
the_file.write('')
the_file.close()
#---------------------------------------------------------------
fileCount = 0
print("x: fileCount")
for file in os.listdir(imgFolder):
filename, file_extension = os.path.splitext(file)
file_extension = file_extension.lower()
if(file_extension == ".jpg" or file_extension==".png" or file_extension==".jpeg" or file_extension==".bmp"):
imgfile = os.path.join(imgFolder, file)
xmlfile = os.path.join(xmlFolder ,filename + ".xml")
if(os.path.isfile(xmlfile)):
print("id:{}".format(fileCount))
print("processing {}".format(imgfile))
print("processing {}".format(xmlfile))
fileCount += 1
transferYolo( xmlfile, imgfile)
copyfile(imgfile, os.path.join(saveYoloPath ,file))
print("x: jpg?")
if(os.path.exists(negFolder)):
for file in os.listdir(negFolder):
filename, file_extension = os.path.splitext(file)
file_extension = file_extension.lower()
imgfile = os.path.join(negFolder ,file)
if(file_extension == ".jpg" or file_extension==".png" or file_extension==".jpeg" or file_extension==".bmp"):
transferYolo( None, imgfile, "")
copyfile(imgfile, os.path.join(saveYoloPath ,file))