-
Notifications
You must be signed in to change notification settings - Fork 12
/
annotation.py
176 lines (131 loc) · 4.42 KB
/
annotation.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
"""
Walk-Assistant : Recognizing sidewalk for the visually impaired
Copyright (C) 2018 Yoongi Kim (devlifecode@outlook.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
print("""
Walk-Assistant Copyright (C) 2018 Yoongi Kim
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
""")
import cv2
from data_loader import DataLoader
import glob
import numpy as np
import random
import os
from model import MyModel
PATH = "data/frames"
ANNOTATION_FILE = "data/label.txt"
KERNEL = 80
SHAPE = (9, 16)
HEIGHT = 720
WIDTH = 1280
labels = []
grid = np.zeros(SHAPE, dtype=np.int)
l_down = False
r_down = False
def click(event, x, y, flags, param):
global clicks, l_down, r_down
if event == cv2.EVENT_LBUTTONDOWN or (event == cv2.EVENT_MOUSEMOVE and l_down):
l_down = True
x = max(0, min(x, WIDTH-1))
y = max(0, min(y, HEIGHT-1))
r = y // KERNEL
c = x // KERNEL
grid[r][c] = 1
elif event == cv2.EVENT_LBUTTONUP:
l_down = False
if event == cv2.EVENT_RBUTTONDOWN or (event == cv2.EVENT_MOUSEMOVE and r_down):
r_down = True
r = y // KERNEL
c = x // KERNEL
grid[r][c] = 0
elif event == cv2.EVENT_RBUTTONUP:
r_down = False
def visualize(img):
global grid
grid = np.round(grid)
h, w, c = img.shape
box = np.zeros((h, w), dtype=np.uint8)
for i in range(len(grid)):
for j in range(len(grid[i])):
box[i*KERNEL : (i+1)*KERNEL, j*KERNEL : (j+1)*KERNEL] = grid[i][j] * 255
box = cv2.cvtColor(box, cv2.COLOR_GRAY2BGR)
box[:,:,0]=0
box[:,:,2]=0
visual = cv2.add(img, box)
for i in range(0, HEIGHT, KERNEL): # 가로선
cv2.line(visual, (0, i), (WIDTH, i), color=(255,255,255))
for i in range(0, WIDTH, KERNEL): # 세로선
cv2.line(visual, (i, 0), (i, HEIGHT), color=(255,255,255))
return visual
def read_label():
files = []
if not os.path.exists(ANNOTATION_FILE):
return [], []
with open(ANNOTATION_FILE, 'r') as f:
lines = f.readlines()
for line in lines:
line = str(line).replace('\n', '')
file, encode = line.split(',')
files.append(file)
return files, lines
def write_label(file_name, grid):
grid = np.round(grid).astype(np.uint8)
flat = np.array(grid).flatten()
encode = ''
label = ''
for b in flat:
encode += str(b)
label = '%s,%s\n' % (file_name, encode)
print(label)
labels.append(label)
with open(ANNOTATION_FILE, 'w') as f:
f.writelines(labels)
print('Saved label file')
def predict(img, my_model):
img = np.array([img])/255.0
res = my_model.model.predict(img)[0]
res = np.squeeze(res[:,:,1]) # (9, 16)
return res
if __name__ == '__main__':
model = MyModel(True, HEIGHT, WIDTH, KERNEL, KERNEL, 1e-6, 'main')
files = glob.glob('{}/*.jpg'.format(PATH))
prev_files, prev_lines = read_label()
labels = prev_lines
print('Previous annotations: {}'.format(len(prev_files)))
print(prev_files)
random.shuffle(files)
for index, file in enumerate(files):
file_name = str(file).replace('\\', '/').split('/')[-1]
if file_name in prev_files:
continue
img = cv2.imread(file)
grid = predict(img, model)
cv2.namedWindow('img')
cv2.setMouseCallback('img', click)
while True:
cv2.imshow('img', visualize(img))
k = cv2.waitKey(2)
if k == -1: # No Key
pass
elif k == 27: # ESC
print('Exit')
exit(0)
elif k == 32 or k == 13: # ESC, Space, Enter
write_label(file_name, grid)
break
else:
print('Key pressed: ', k)
print('Done')