-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboxIt.py
203 lines (173 loc) · 6.23 KB
/
boxIt.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
import os
import matplotlib.pyplot as plt
import cv2
from lxml import etree
import xml.etree.cElementTree as ET
from matplotlib.widgets import RectangleSelector
def write_xml(folder, img, objects, tl, br, savedir):
if not os.path.isdir(savedir):
os.mkdir(savedir)
image = cv2.imread(img.path)
height, width, depth = image.shape
annotation = ET.Element('annotation')
ET.SubElement(annotation, 'folder').text = folder
ET.SubElement(annotation, 'filename').text = img.name
ET.SubElement(annotation, 'segmented').text = '0'
size = ET.SubElement(annotation, 'size')
ET.SubElement(size, 'width').text = str(width)
ET.SubElement(size, 'height').text = str(height)
ET.SubElement(size, 'depth').text = str(depth)
for obj, topl, botr in zip(objects, tl, br):
ob = ET.SubElement(annotation, 'object')
ET.SubElement(ob, 'name').text = obj
ET.SubElement(ob, 'pose').text = 'Unspecified'
ET.SubElement(ob, 'truncated').text = '0'
ET.SubElement(ob, 'difficult').text = '0'
bbox = ET.SubElement(ob, 'bndbox')
ET.SubElement(bbox, 'xmin').text = str(topl[0])
ET.SubElement(bbox, 'ymin').text = str(topl[1])
ET.SubElement(bbox, 'xmax').text = str(botr[0])
ET.SubElement(bbox, 'ymax').text = str(botr[1])
xml_str = ET.tostring(annotation)
root = etree.fromstring(xml_str)
xml_str = etree.tostring(root, pretty_print=True)
save_path = os.path.join(savedir, img.name.replace('jpg', 'xml'))
with open(save_path, 'wb') as temp_xml:
temp_xml.write(xml_str)
# global const
img = None
tl_list = []
br_list = []
object_list = []
# const
image_folder = 'images'
savedir = 'annotations'
obj = 'elephant'
def line_select_callback(clk, rls):
global tl_list
global br_list
global object_list
tl_list.append((int(clk.xdata), int(clk.ydata)))
br_list.append((int(rls.xdata), int(rls.ydata)))
object_list.append(obj)
def onkeypress(event):
global object_list
global tl_list
global br_list
global img
if event.key == 'q':
print(object_list)
write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None
plt.close()
def toggle_selector(event):
toggle_selector.RS.set_active(True)
if __name__ == '__main__':
for n, image_file in enumerate(os.scandir(image_folder)):
img = image_file
fig, ax = plt.subplots(1)
mngr = plt.get_current_fig_manager()
# mngr.window.setGeometry(250, 120, 1280, 1024)
# NOTE : uncomment the above for windows 10..
image = cv2.imread(image_file.path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
ax.imshow(image)
toggle_selector.RS = RectangleSelector(
ax, line_select_callback,
drawtype='box', useblit=True,
button=[1], minspanx=5, minspany=5,
spancoords='pixels', interactive=True
)
bbox = plt.connect('key_press_event', toggle_selector)
key = plt.connect('key_press_event', onkeypress)
plt.show()
=======
import os
import matplotlib.pyplot as plt
import cv2
from lxml import etree
import xml.etree.cElementTree as ET
from matplotlib.widgets import RectangleSelector
def write_xml(folder, img, objects, tl, br, savedir):
if not os.path.isdir(savedir):
os.mkdir(savedir)
image = cv2.imread(img.path)
height, width, depth = image.shape
annotation = ET.Element('annotation')
ET.SubElement(annotation, 'folder').text = folder
ET.SubElement(annotation, 'filename').text = img.name
ET.SubElement(annotation, 'segmented').text = '0'
size = ET.SubElement(annotation, 'size')
ET.SubElement(size, 'width').text = str(width)
ET.SubElement(size, 'height').text = str(height)
ET.SubElement(size, 'depth').text = str(depth)
for obj, topl, botr in zip(objects, tl, br):
ob = ET.SubElement(annotation, 'object')
ET.SubElement(ob, 'name').text = obj
ET.SubElement(ob, 'pose').text = 'Unspecified'
ET.SubElement(ob, 'truncated').text = '0'
ET.SubElement(ob, 'difficult').text = '0'
bbox = ET.SubElement(ob, 'bndbox')
ET.SubElement(bbox, 'xmin').text = str(topl[0])
ET.SubElement(bbox, 'ymin').text = str(topl[1])
ET.SubElement(bbox, 'xmax').text = str(botr[0])
ET.SubElement(bbox, 'ymax').text = str(botr[1])
xml_str = ET.tostring(annotation)
root = etree.fromstring(xml_str)
xml_str = etree.tostring(root, pretty_print=True)
save_path = os.path.join(savedir, img.name.replace('jpg', 'xml'))
with open(save_path, 'wb') as temp_xml:
temp_xml.write(xml_str)
# global const
img = None
tl_list = []
br_list = []
object_list = []
# const
image_folder = 'images'
savedir = 'annotations'
obj = 'elephant'
def line_select_callback(clk, rls):
global tl_list
global br_list
global object_list
tl_list.append((int(clk.xdata), int(clk.ydata)))
br_list.append((int(rls.xdata), int(rls.ydata)))
object_list.append(obj)
def onkeypress(event):
global object_list
global tl_list
global br_list
global img
if event.key == 'q':
print(object_list)
write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None
plt.close()
def toggle_selector(event):
toggle_selector.RS.set_active(True)
if __name__ == '__main__':
for n, image_file in enumerate(os.scandir(image_folder)):
img = image_file
fig, ax = plt.subplots(1)
mngr = plt.get_current_fig_manager()
# mngr.window.setGeometry(250, 120, 1280, 1024)
# NOTE : uncomment the above for windows 10..
image = cv2.imread(image_file.path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
ax.imshow(image)
toggle_selector.RS = RectangleSelector(
ax, line_select_callback,
drawtype='box', useblit=True,
button=[1], minspanx=5, minspany=5,
spancoords='pixels', interactive=True
)
bbox = plt.connect('key_press_event', toggle_selector)
key = plt.connect('key_press_event', onkeypress)
plt.show()