-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelling_tool.py
127 lines (112 loc) · 3.75 KB
/
labelling_tool.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
import cv2 as cv
import argparse
import os
from pprint import pprint
def reset():
global clean_img, image
image = clean_img.copy()
def save():
global data_gathered
print(data_gathered)
with open('results.txt', 'w') as file:
for line in data_gathered:
file.write(str(line) + '\n')
def quit():
cv.destroyAllWindows()
exit(0)
# initialize the list of reference points and boolean indicating
# whether cropping is being performed or not
stepPt = []
seqPt = []
cropping = False
color = (0,255,0)
data_gathered = []
# available: rectangle, dot
sequence = ["rectangle", "dot"]
sequence_step = 0
current_label = ""
pre_rectangle_img = []
def handle_mouse(event, x, y, flags, param):
global sequence_step, sequence, pre_rectangle_img
global stepPt, seqPt, cropping, color
if event == mousebindings['startstep']:
if sequence[sequence_step] == 'rectangle':
stepPt = [(x, y)]
cropping = True
elif sequence[sequence_step] == 'dot':
cv.circle(image, (x,y), 4, color, 4)
seqPt.append([x,y])
elif event == mousebindings['endstep']:
if sequence[sequence_step] == 'rectangle':
stepPt.append((x, y))
cropping = False
cv.rectangle(image, stepPt[0], stepPt[1], color, 2)
# standardized rectangle coordinates
seqPt.append([
[min(stepPt[0][0], stepPt[1][0]), min(stepPt[0][1], stepPt[1][1])],
[max(stepPt[0][0], stepPt[1][0]), max(stepPt[0][1], stepPt[1][1])],
])
stepPt = []
# cv.imshow("image", image)
sequence_step += 1
if sequence_step == len(sequence):
sequence_step = 0
seqPt.insert(0, current_label)
data_gathered.append(seqPt)
seqPt = []
# to use colors by names run: pip install webcolors
keybindings = {
# 'key': ['label', [RGB color]]
'f': ['quarter','yellow'],
'd': ['half','tomato'],
's': ['full','lightgreen'],
'a': ['eighth','darkturquoise'],
'b': ['back', 'hotpink'],
'R': reset,
'S': save,
'Q': quit,
# '\x1b': quit, # escape key for python ord()
}
mousebindings = {
'dot': cv.EVENT_RBUTTONDOWN,
'startstep': cv.EVENT_LBUTTONDOWN,
'endstep': cv.EVENT_LBUTTONUP,
}
if __name__ == "__main__":
# command-line argument parsing
ap = argparse.ArgumentParser(description='Choose images for labeling')
ap.add_argument('path',
type=str,
help='path to a file or directory')
args = ap.parse_args()
print(os.path.isfile(args.path))
print(os.path.isdir(args.path))
pprint(keybindings)
#FIXME na razie tylko pojedynczy plik
image = cv.imread(args.path)
clean_img = image.copy()
cv.namedWindow("image")
cv.setMouseCallback("image", handle_mouse)
while True:
# handling keyboard input
cv.imshow("image", image)
key = cv.waitKey(1) & 0xFF
if chr(key) != 'ÿ':
print(chr(key))
if chr(key) in keybindings.keys():
# the keybind is for changing label and color
if type(keybindings[chr(key)]) is list:
color = keybindings[chr(key)][1]
if type(color) is str:
import webcolors
color = list(webcolors.name_to_rgb(color))
color.reverse()
current_label = keybindings[chr(key)][0]
seqPt = []
sequence_step = 0
# the keybind is for special function
elif callable(keybindings[chr(key)]):
keybindings[chr(key)]()
else: print("Instruction assigned to this key is incomprehensible.")
# close all open windows
cv.destroyAllWindows()