-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_label_me.py
306 lines (281 loc) · 14.9 KB
/
read_label_me.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import cv2
import numpy as np
import xml.dom.minidom as DOM
import xml.etree.cElementTree as etree
import xml.etree.ElementTree as ET
def rotate(image,angle,center = None,scale=1.0):
(h,w) = image.shape[:2]
if center is None:
center = (w/2, h/2)
M = cv2.getRotationMatrix2D((int(w/2),int(h/2)), angle, scale)
#print("s1:", s, "t1:", t)
#print("w:", w, "h:", h)
cos = np.abs(M[0,0])
sin = np.abs(M[0,1])
# print(M[0,0],M[0,1])
# print(np.sin(-angle),np.cos(-angle))
nw = int((h*sin)+(w*cos))
nh = int((h*cos)+(w*sin))
M[0,2] += (nw/2) - w/2
M[1,2] += (nh/2) - h/2
rotated = cv2.warpAffine(image, M, (nw, nh))
#rotated = cv2.warpAffine(image, M, (w, h))
# print("s1:", rotated.shape[1], "t1:", rotated.shape[0])
# a = cv2.resize(rotated, (int(nw/4), int(nh/4)))
#cv2.imshow("",a)
#cv2.waitKey(2000)
return rotated
def crop_image_and_rotate_fc(img, xmin,ymin,xmax,ymax,thero_ori):
flag = True
(h, w) = img.shape[:2]
# if flag:
# cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
# cv2.imshow("", img)
# cv2.waitKey(200)
# draw_img = draw_line(lines, crop_image, True, (0, 255, 0))
# draw_img = draw_line(_, draw_img, flage, (0, 0, 255))
# if thero != 1.57:
# thero_ori = (thero - 1.57) * 180 / np.pi
crop_img = img[ymin:ymax,xmin:xmax]
rotated_ori_img = rotate(crop_img, thero_ori)
# if flag:
# cv2.imshow("rotation1:", rotated_ori_img)
# cv2.waitKey(500)
return rotated_ori_img # crop_image
def arrange_hc_label_me():
jpg_dir = '/home/gytang/project/traintickets/image/'
xml_dir = '/home/gytang/project/traintickets/label/'
zaiqing_xml_dir = '/home/gytang/project/zq_traintickets/'
txt_label = '/home/gytang/project/traintickets/traintickets_info.txt'
ft = open(txt_label, 'w')
for info in os.listdir(xml_dir):
with open(os.path.join(xml_dir, info), 'r') as f:
# class_rst_dict = defaultdict(list)
# # we fill the data path not label path in the class_rst_dict. It is used for separation
# filled_value = filled_file_name if filled_file_name is not None else file_path
# with open(file_path, 'r') as f:
fpimage = cv2.imread(os.path.join(jpg_dir, os.path.splitext(info)[0] + '.jpg'))
xml_tree = ET.parse(f)
root = xml_tree.getroot()
file_name = root.find('filename').text
class_rst_lst = {}
for_direction = 0
obj_lst = root.findall("object")
angel = None
for obj_id, obj in enumerate(obj_lst):
cls = obj.find('name').text
spls = cls.split('-')
if len(spls) < 3:
if cls == 'hcp-text':
continue
else:
print("exception label name: ", cls,info)
elif len(spls) == 3:
label = spls[-1]
class_rst_lst['name'] = label
bndbox = obj.find("bndbox")
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
if label in ['up', 'down', 'left', 'right']:
for_direction += 1
try:
with open(os.path.join(zaiqing_xml_dir, info), 'r') as directf:
xml_tree_directf = ET.parse(directf)
directroot = xml_tree_directf.getroot()
direct_obj_lst = directroot.findall('object')
direct_for_direction = 0
for direct_obj_id, direct_obj in enumerate(direct_obj_lst):
if direct_obj.find('name').text.split('-')[-1] in ['up', 'down', 'left', 'right']:
direct_for_direction += 1
if for_direction == direct_for_direction:
direct_bndbox = direct_obj.find('bndbox')
# if str(xmin) == direct_bndbox.find('xmin').text \
# and str(ymin) == direct_bndbox.find('ymin').text \
# and str(xmax) == direct_bndbox.find('xmax').text \
# and str(ymax) == direct_bndbox.find('ymax').text:
direct = direct_obj.find('name').text.split('-')[-1]
if direct == 'right':
angel = 90
elif direct == 'left':
angel = -90
elif direct == 'down':
angel = 180
else:
angel = 0
co_img = crop_image_and_rotate_fc(fpimage, xmin, ymin, xmax, ymax, angel)
# for next_obj_lst in enumerate(obj_lst[obj_id:]):
# next_spls = next_obj_lst.find('name').text.split('-')
# if len(next_spls) == 3 and next_spls[-1] not in ['up', 'down', 'left', 'right']:
# next_bndbox = next_obj_lst.find("bndbox").text
# xmin_next = int(next_bndbox.find('xmin').text)
# ymin_next = int(next_bndbox.find('ymin').text)
# xmax_next = int(next_bndbox.find('xmax').text)
# ymax_next = int(next_bndbox.find('ymax').text)
# co_img = crop_image_and_rotate_fc(fpimage, xmin_next, ymin_next, xmax_next, ymax_next, rotate)
# else:
# print(xmin,ymin,xmax,ymax," | ",direct_bndbox.find('xmin').text,
# direct_bndbox.find('ymin').text,
# direct_bndbox.find('xmax').text,
# direct_bndbox.find('ymax').text)
except:
print("zq has no this xml file.", info)
else:
if angel is not None:
co_img = crop_image_and_rotate_fc(fpimage, xmin, ymin, xmax, ymax, angel)
cv2.imwrite("/home/gytang/project/traintickets/crop_image/"+os.path.splitext(info)[0]+'_'+str(obj_id)+'.jpg', co_img)
ft.write(os.path.splitext(info)[0] + '_'+ str(obj_id)+'.jpg' + ':' + label + '\n')
else:
print("exception label, the angel is None,",info)
else:
print("exception label name: ", cls, info)
ft.close()
def arrange_jp_label_me():
jpg_dir = '/home/gytang/project/dataset/yg_railway_airplane/air/jpg/'
xml_dir = '/home/gytang/project/dataset/yg_railway_airplane/air/label/'
#zaiqing_xml_dir = '/home/gytang/project/zq_traintickets/'
txt_label = '/home/gytang/project/planetickets/planetickets_info.txt'
ft = open(txt_label, 'w')
for info in os.listdir(xml_dir):
with open(os.path.join(xml_dir, info), 'r') as f:
# class_rst_dict = defaultdict(list)
# # we fill the data path not label path in the class_rst_dict. It is used for separation
# filled_value = filled_file_name if filled_file_name is not None else file_path
# with open(file_path, 'r') as f:
fpimage = cv2.imread(os.path.join(jpg_dir, os.path.splitext(info)[0] + '.jpg'))
xml_tree = ET.parse(f)
root = xml_tree.getroot()
class_rst_lst = {}
for_direction = 0
obj_lst = root.findall("object")
angel = None
for obj_id, obj in enumerate(obj_lst):
cls = obj.find('name').text
spls = cls.split('-')
if len(spls) < 3:
if cls == 'jp-text':
continue
else:
label = spls[-1]
class_rst_lst['name'] = label
bndbox = obj.find("bndbox")
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
if label in ['up', 'down', 'left', 'right']:
for_direction += 1
if label == 'right':
angel = -90
elif label == 'left':
angel = 90
elif label == 'down':
angel = 180
else:
angel = 0
co_img = crop_image_and_rotate_fc(fpimage, xmin, ymin, xmax, ymax,
angel)
elif len(spls) >= 3:
label = ""
for lab_i in spls[2:]:
label += '-'+lab_i
if label[0]=='-':
label=label[1:]
class_rst_lst['name'] = label
bndbox = obj.find("bndbox")
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
if angel is not None:
if xmin>=xmax or ymin>=ymax:
print("crop content failed ",cls,info)
else:
co_img = crop_image_and_rotate_fc(fpimage, xmin, ymin, xmax, ymax, angel)
cv2.imwrite("/home/gytang/project/dataset/yg_railway_airplane/air/crop_image/" + os.path.splitext(info)[
0] + '_' + str(obj_id) + '.jpg', co_img)
ft.write(os.path.splitext(info)[0] + '_' + str(obj_id) + '.jpg' + ':' + label + '\n')
else:
print("exception label, the angel is None,", info)
else:
print("exception label name: ", cls, info)
ft.close()
def arrange_jp_train_label_me():
jpg_dir = '/home/gytang/project/dataset/yg_railway_airplane/air/jpg/'
xml_dir = '/home/gytang/project/dataset/yg_railway_airplane/air/label/'
#zaiqing_xml_dir = '/home/gytang/project/zq_traintickets/'
fr = open('/home/gytang/project/dataset/yg_railway_airplane/air/train.txt', 'r')
lines= fr.readlines()
txt_label = '/home/gytang/project/dataset/yg_railway_airplane/air/planetickets_info.txt'
ft = open(txt_label, 'w')
for info in lines:
#for info in os.listdir(xml_dir):
info = info[:-3]
with open(os.path.join(xml_dir, info+'.xml'), 'r') as f:
# class_rst_dict = defaultdict(list)
# # we fill the data path not label path in the class_rst_dict. It is used for separation
# filled_value = filled_file_name if filled_file_name is not None else file_path
# with open(file_path, 'r') as f:
fpimage = cv2.imread(os.path.join(jpg_dir, info + '.jpg'))
xml_tree = ET.parse(f)
root = xml_tree.getroot()
class_rst_lst = {}
for_direction = 0
obj_lst = root.findall("object")
angel = None
for obj_id, obj in enumerate(obj_lst):
cls = obj.find('name').text
spls = cls.split('-')
if len(spls) < 3:
if 'jp-text' in cls:
continue
else:
label = spls[-1]
class_rst_lst['name'] = label
bndbox = obj.find("bndbox")
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
if label in ['up', 'down', 'left', 'right']:
for_direction += 1
if label == 'right':
angel = 90
elif label == 'left':
angel = -90
elif label == 'down':
angel = 180
else:
angel = 0
co_img = crop_image_and_rotate_fc(fpimage, xmin, ymin, xmax, ymax,
angel)
for obj_id, obj in enumerate(obj_lst):
cls = obj.find('name').text
spls = cls.split('-')
if len(spls) >= 3:
label = ""
for lab_i in spls[2:]:
label += '-'+lab_i
if label[0]=='-':
label=label[1:]
class_rst_lst['name'] = label
bndbox = obj.find("bndbox")
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
if angel is not None:
if xmin>=xmax or ymin>=ymax:
print("crop content failed ", cls, info)
else:
co_img = crop_image_and_rotate_fc(fpimage, xmin, ymin, xmax, ymax, angel)
cv2.imwrite("/home/gytang/project/dataset/yg_railway_airplane/air/crop_image/" + info + '_' + str(obj_id) + '.jpg', co_img)
ft.write(info + '_' + str(obj_id) + '.jpg' + ':' + label + '\n')
else:
print("exception label, the angel is None,", info)
ft.close()
if __name__ == "__main__":
#arrange_hc_label_me()
arrange_jp_train_label_me()