-
Notifications
You must be signed in to change notification settings - Fork 171
/
part6 - draw_box.py
65 lines (54 loc) · 1.71 KB
/
part6 - draw_box.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
import os
import matplotlib.pyplot as plt
import cv2
from matplotlib.widgets import RectangleSelector
from generate_xml import write_xml
# global constants
img = None
tl_list = []
br_list = []
object_list = []
# constants
image_folder = 'images'
savedir = 'annotations'
obj = 'fidget_spinner'
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)
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()