-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabeling_tool.py
105 lines (72 loc) · 2.41 KB
/
labeling_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
# crosswalk labeling tool
# use preprocessed_data images
# input both side of crosswalk --> location / direction(angle)
# reference : https://tykimos.github.io/2018/10/16/Simple_Annotation_Tool_1/
import glob
import cv2
import math
img_files = glob.glob('./preprocessed_data/*.png')
point = [0, (0,0)] # click_cnt, location carrier
#=======================#
# FUNCTIONS #
#=======================#
def draw_point(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
global done
if draw_second_line:
done = True
return
cv2.circle(visual, (x,y), 3, (0, 0, 255), -1)
global point, points
points[point[0]] = (x, y)
point[0] = point[0] + 1
point[1] = (x, y)
print (point[0], points)
def compute_label(img, points):
h, w = img.shape[:2]
x1, y1 = points[0]
x2, y2 = points[1]
x3, y3 = points[2]
x4, y4 = points[3]
x_1 = float(h - y1) * (x1 - x2) / (y1 - y2) + x2
x_2 = float(h - y3) * (x3 - x4) / (y3 - y4) + x3
loc = (w - (x_1 + x_2)) / (x_2 - 1)
x_1 = float(-y1) * (x1 - x2) / (y1 - y2) + x1
x_2 = float(-y3) * (x3 - x4) / (y3 - y4) + x3
print (x_1, x_2)
neg = (-1)**(w < (x_2 + x_1))
print(neg)
ang = math.atan(0.5 * (w - x_2 + x_1) / h) * neg
return loc, ang
#==================#
# MAIN #
#==================#
cv2.namedWindow('tool')
cv2.setMouseCallback('tool', draw_point)
for img_file in img_files:
img = cv2.imread(img_file)
point[0] = 0
points = [(0, 0)]*4
draw_first_line = False
draw_second_line = False
done = False
visual = img.copy()
while True:
if point[0] == 2 and not draw_first_line:
cv2.line(visual, points[0], points[1], (0, 0, 255), 2)
draw_first_line = True
if point[0] == 4 and not draw_second_line:
cv2.line(visual, points[2], points[3], (0, 0, 255), 2)
draw_second_line = True
if done: break
cv2.imshow('tool', visual)
if cv2.waitKey(2) == 32:
break
## TODO: press 'q' --> quit //nested loop break
print(points)
if draw_first_line and draw_second_line:
loc, ang = compute_label(img, points)
print loc, ang
result = open('annotation.txt', 'a')
result.writelines(img_file + ' ' + str(loc) + ' ' + str(ang) + '\n')
result.close()