-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
350 lines (295 loc) · 10.5 KB
/
run.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from bottle import route, run, template,static_file, view, request
import numpy as np
from PIL import Image
import mysql.connector
from os import walk
dataset = 'Berkeley'
sql_port = 3306 #8889
# Getting the image names
images = []
for (dirpath, dirnames, filenames) in walk("datasets/"+dataset+"/images/"):
images = filenames
boxes = {}
for img_name in images:
mask = np.array(Image.open('datasets/'+dataset+'/masks/' +img_name.split('.')[0]+'.png'))
mask = (mask > 0).astype(np.uint8)
points =np.nonzero(mask)
y_0 = min(points[0])
y_1 = max(points[0])
x_0 = min(points[1])
x_1 = max(points[1])
boxes[img_name] = (x_0, y_0, x_1, y_1)
@route('/')
@view('templates/index.tpl')
def index():
context = {}
return (context)
@route('/login')
@view('templates/login.tpl')
def login():
context = {}
return (context)
@route('/bounding_boxes')
@view('templates/boxes.tpl')
def play():
context = {}
return (context)
@route('/ctr_points')
@view('templates/ctr_points.tpl')
def play():
context = {}
return (context)
@route('/extreme_points')
@view('templates/extreme_points.tpl')
def extreme_points():
context = {}
return (context)
@route('/img/<filename>')
def server_static(filename):
return static_file(filename, root='img/')
@route('/style/<filename>')
def server_static(filename):
return static_file(filename, root='style/')
@route('/scripts/<filename>')
def server_static(filename):
return static_file(filename, root='scripts/')
@route('/datasets/<filename>')
def server_static(filename):
return static_file(filename, root='datasets/')
@route('/datasets/'+dataset+'/fusions/<filename>')
def server_static(filename):
return static_file(filename, root='datasets/'+dataset+'/fusions/')
@route('/datasets/'+dataset+'/images/<filename>')
def server_static(filename):
return static_file(filename, root='datasets/'+dataset+'/images/')
@route('/datasets/GrabCut/fusions/<filename>')
def server_static(filename):
return static_file(filename, root='datasets/'+dataset+'/fusions/')
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
@route('/get_first_image_points', method='POST')
def get_first_image_points():
username = request.forms.get('username')
new_image = get_new_image_points(username)
if new_image is not None:
return '/datasets/'+dataset+'/images/'+new_image
return 'end'
@route('/get_first_image_ctr_points', method='POST')
def get_first_image_ctr_points():
username = request.forms.get('username')
new_image = get_new_image_ctr_points(username)
if new_image is not None:
return '/datasets/'+dataset+'/images/'+new_image
return 'end'
@route('/get_first_image', method='POST')
def get_first_image():
username = request.forms.get('username')
new_image = get_new_image(username)
if new_image is not None:
return '/datasets/'+dataset+'/images/'+get_new_image(username)
return 'end'
@route('/save_time', method='POST')
def save_time_to_db():
image_path = request.forms.get('image_path')
username = request.forms.get('username')
print('username', username)
box = eval(request.forms.get('bounding_box'))
box_width = box[1]['x'] - box[0]['x']
box_height = box[1]['y'] - box[0]['y']
time = int(float(request.forms.get('time')))
boxA = (box[0]['x'], box[0]['y'], box[1]['x'], box[1]['y'])
iou = bb_intersection_over_union(boxA, boxes[image_path.split('/')[-1]])
print('iou',iou)
try:
mydb = mysql.connector.connect(
host="localhost",
port=sql_port,
user="root",
password="root",
database="box_time"
)
cursor = mydb.cursor()
sql = ('INSERT INTO drawn_boxes(ID, username, img_path, x, y, width, height, iou, time) VALUES ')
print(sql+str((0, username, image_path, int(box[0]['x']), int(box[0]['y']), int(box_width), int(box_height), iou, time)))
cursor.execute(sql+str((0, username, image_path, box[0]['x'], box[0]['y'], box_width, box_height, iou, time)))
print("affected rows = {}".format(cursor.rowcount))
cursor.close()
mydb.commit()
mydb.close()
new_image = get_new_image(username)
print("new_image", new_image)
if new_image is not None:
return '/datasets/'+dataset+'/images/'+new_image
else:
return 'end'
except:
print('error')
return None
@route('/save_ctr_points_time_to_db', method='POST')
def save_ctr_points_time_to_db():
"""Save contour points
"""
image_path = request.forms.get('image_path')
username = request.forms.get('username')
print('username', username)
points = np.array(eval(request.forms.get('points')))
time = int(float(request.forms.get('time')))
x_0 = min(points[:,0])
x_1 = max(points[:,0])
y_0 = min(points[:,1])
y_1 = max(points[:,1])
boxA = (x_0, y_0, x_1, y_1)
iou = bb_intersection_over_union(boxA, boxes[image_path.split('/')[-1]])
try:
mydb = mysql.connector.connect(
host="localhost",
port=sql_port,
user="root",
password="root",
database="box_time"
)
cursor = mydb.cursor()
sql = ('INSERT INTO drawn_ctr_points(ID, username, img_path, point_1_x, point_2_x, point_3_x, point_1_y, point_2_y, point_3_y, iou, time) VALUES ')
cursor.execute(sql+str((0, username, image_path, points[0][0], points[1][0], points[2][0], points[0][1], points[1][1], points[2][1] , iou, time)))
print("affected rows = {}".format(cursor.rowcount))
cursor.close()
mydb.commit()
mydb.close()
new_image = get_new_image_ctr_points(username)
if new_image is not None:
return '/datasets/'+dataset+'/images/'+new_image
else:
return 'end'
return get_new_image_ctr_points(username)
except:
print("error")
return None
@route('/save_points', method='POST')
def save_points_time_to_db():
"""Save extreme points
"""
image_path = request.forms.get('image_path')
username = request.forms.get('username')
print('username', username)
points = np.array(eval(request.forms.get('points')))
time = int(float(request.forms.get('time')))
x_0 = min(points[:,0])
x_1 = max(points[:,0])
y_0 = min(points[:,1])
y_1 = max(points[:,1])
boxA = (x_0, y_0, x_1, y_1)
iou = bb_intersection_over_union(boxA, boxes[image_path.split('/')[-1]])
print('iou',iou)
try:
mydb = mysql.connector.connect(
host="localhost",
port=sql_port,
user="root",
password="root",
database="box_time"
)
cursor = mydb.cursor()
sql = ('INSERT INTO drawn_points(ID, username, img_path, point_1_x, point_2_x, point_3_x, point_4_x, point_1_y, point_2_y, point_3_y, point_4_y, iou, time) VALUES ')
cursor.execute(sql+str((0, username, image_path, points[0][0], points[1][0], points[2][0], points[3][0], points[0][1], points[1][1], points[2][1], points[3][1] , iou, time)))
print("affected rows = {}".format(cursor.rowcount))
cursor.close()
mydb.commit()
mydb.close()
new_image = get_new_image_points(username)
if new_image is not None:
return '/datasets/'+dataset+'/images/'+new_image
else:
return 'end'
return get_new_image_points(username)
except:
print("error")
return None
def get_new_image(username):
print("get new image username")
try:
mydb = mysql.connector.connect(
host="localhost",
port=sql_port,
user="root",
password="root",
database="box_time"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT img_path FROM drawn_boxes WHERE username = %s", (username,))
list_tuples = list(mycursor)
list_paths = [i[0] for i in list_tuples]
mydb.close()
for img in images:
img_path = '/datasets/'+dataset+'/images/' + img
if img_path not in list_paths:
return img
except Exception as e:
print(e)
return None
return
def get_new_image_points(username):
print("get new image")
try:
mydb = mysql.connector.connect(
host="localhost",
port=sql_port,
user="root",
password="root",
database="box_time"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT img_path FROM drawn_points WHERE username = %s", (username,))
list_tuples = list(mycursor)
list_paths = [i[0] for i in list_tuples]
mydb.close()
for img in images:
img_path = '/datasets/'+dataset+'/images/' + img
if img_path not in list_paths:
return img
mydb.close()
except Exception as e:
print(e)
return None
return
def get_new_image_ctr_points(username):
print("get new image")
try:
mydb = mysql.connector.connect(
host="localhost",
port=sql_port,
user="root",
password="root",
database="box_time"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT img_path FROM drawn_ctr_points WHERE username = %s", (username,))
list_tuples = list(mycursor)
list_paths = [i[0] for i in list_tuples]
mydb.close()
for img in images:
img_path = '/datasets/'+dataset+'/images/' + img
if img_path not in list_paths:
return img
mydb.close()
except Exception as e:
print(e)
return None
return
#run(host='george.intra.cea.fr', port=8084)
run(host='george.intra.cea.fr', port=8084, debug=True, reloader=True, server='cherrypy')