Repository navigation
Expand file tree
/
Copy pathxml_transform.py
More file actions
81 lines (64 loc) · 2.24 KB
/
Copy pathxml_transform.py
File metadata and controls
81 lines (64 loc) · 2.24 KB
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
import sys
if sys.version_info[0] == 2:
import xml.etree.cElementTree as ET
else:
import xml.etree.ElementTree as ET
import cv2
import ipdb
def xml_transform(target):
"""args:
target: xml file
return:
res: [xmin, ymin, xmax, ymax, label_id]"""
target = ET.parse(target).getroot()
res = []
for obj in target.iter('object'):
difficult = int(obj.find('difficult').text) == 1
if difficult:
continue
name = obj.find('name').text.lower().strip()
bbox = obj.find('bndbox')
pts = ['xmin', 'ymin', 'xmax', 'ymax']
bndbox = []
for i, pt in enumerate(pts):
cur_pt = int(bbox.find(pt).text) - 1
bndbox.append(cur_pt)
label = name
bndbox.append(label)
res += [bndbox]
return res
def plot_rec(img, res, i):
"""args:
img: image
res: [xmin, ymin, xmax, ymax, label]"""
img = cv2.imread(img)
# (0, 255, 0) color, 2 linewidth
for j in range(len(res)):
cv2.rectangle(img, tuple(res[j][:2]), tuple(res[j][2:4]), (0, 255, 0), 2)
# img_ = cv2.imread('ANT/Testimages/{}.jpg'.format(img_id))
# cv2.rectangle(img_, (coords[0], coords[1]),(coords[2], coords[3]), (0, 255, 0), 2)
# cv2.putText(img_, str(label_name)+':'+str(score.item()), (coords[0], coords[1]),\
# cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
# cv2.imwrite(save_folder+'{}.jpg'.format(img_id), img_)
# cv2.rectangle(img, (100, 100), (200, 200), (0, 255, 0), 3)
# cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
# cv2.imshow("Image", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
save_dir = './plot/'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
cv2.imwrite(save_dir + '{}.jpg'.format(i), img)
if __name__ == '__main__':
# target = './蒸呢印/J01_2018.06.13 14_56_32.xml'
# img = './J01_2018.06.27 13_39_14.jpg'
# target = './J01_2018.06.27 13_39_14.xml'
import os
img = os.listdir('./')
xml = list(filter(lambda x: x.endswith('.xml'), img))
jpg = list(filter(lambda x: x.endswith('.jpg'), img))
for i in range(len(xml)):
target = xml[i]
img = jpg[i]
res = xml_transform(target)
plot_rec(img, res, i)