-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_scale_BL.py
374 lines (315 loc) · 14.9 KB
/
video_scale_BL.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
Program to measure scale / body length from video files manually
videoScaleBL.py
N. Mizumoto
"""
# ---------------------------------------------------------------------------------------------
# packages
# ---------------------------------------------------------------------------------------------
import PySimpleGUI as sg
import cv2
import sys
import numpy as np
import glob
import tqdm
from keyboard import press
import os
import math
import pandas as pd
import pickle
from numpy.linalg import norm
# ---------------------------------------------------------------------------------------------
# Main part of video analysis
# ---------------------------------------------------------------------------------------------
def ImageAnalysis(idir, odir, img_scale, measure_scale, shape, num_ind, frame_interval, skip_analyzed):
# Loading data
path = glob.glob(idir + os.sep + "*.mp4")
file_nums = list(range((len(path))))
# Dataframe
if os.path.exists(odir + "/res.pickle"):
print("existing analysis loaded")
with open(odir+ os.sep + 'res.pickle', mode='rb') as f:
df = pickle.load(f)
else:
df_column = ["name", "width", "height", "length", "fps", "frame", "scale"]
for i in range(num_ind):
df_column.append("bodyLength" + str(i))
df = pd.DataFrame(np.arange(len(path)*(7+num_ind)). reshape(len(path), 7+num_ind),
columns=df_column)
# ------------------ main ------------------
def frame_check(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
press('l')
if event == cv2.EVENT_RBUTTONDOWN:
press('r')
for i in file_nums:
cv2.namedWindow(winname='window')
# ----- file info -----
v = path[i]
file_name = os.path.basename(v)
name = file_name.split('.')[0]
if df.iloc[i, 0] == name:
if skip_analyzed == "true":
continue
img_read = cv2.imread(odir + name + ".jpg")
cv2.putText(img_read, 'Skip? R:yes, L:No',
(10, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow('window', img_read)
cv2.setMouseCallback('window', frame_check)
k = cv2.waitKey(0)
if k == 27:
break
if k == ord("r"):
continue
video = cv2.VideoCapture(v)
width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
count = video.get(cv2.CAP_PROP_FRAME_COUNT)
fps = video.get(cv2.CAP_PROP_FPS)
print("name:{}, width:{}, height:{}, count:{}, fps:{}".format(name, width, height, count, fps))
img_shape = np.array([int(width), int(height)])
# ----------
# region ----- 1. Extract frame -----
img = None
frame_id = 0
while True:
video.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
ret, frame = video.read()
frame = cv2.resize(frame, dsize=None, fx=img_scale, fy=img_scale)
frame_copy = frame.copy()
cv2.putText(frame_copy, 'file: ' + name + ' frame: ' + str(frame_id),
(10, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(frame_copy, 'Use this frame? R:yes, L:No',
(10, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow('window', frame_copy)
cv2.setMouseCallback('window', frame_check)
k = cv2.waitKey(0)
if k == ord("r"):
img = frame.copy()
break
elif k == ord("l"):
frame_id = frame_id + frame_interval
if frame_id > count:
print("End of frames. Maybe reduce frame_interval to sample more frames.")
frame_id = 0
elif k == 27:
break
if k == 27:
break
# endregion ----------
img_output = img.copy()
# region ----- 2. Scale -----
if measure_scale == "True":
sx0, sy0 = 0, 0
sx1, sy1 = 0, 0
drawing = False
if shape == "circle":
def scale_draw(event, x, y, flags, param):
nonlocal sx0, sy0, scale, drawing, end, img_output, img_copy
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
sx0, sy0 = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.circle(img_copy, center=(int((x - sx0) / 2) + sx0, int((y - sy0) / 2) + sy0),
radius=int(math.sqrt((sx0 - x) ** 2 + (sy0 - y) ** 2) / 2),
color=(0, 0, 255), thickness=1)
elif event == cv2.EVENT_LBUTTONUP:
scale = math.sqrt((sx0 - x) ** 2 + (sy0 - y) ** 2)
cv2.circle(img_copy, center=(int((x - sx0) / 2) + sx0, int((y - sy0) / 2) + sy0),
radius=int(scale / 2),
color=(0, 0, 255), thickness=1)
cv2.circle(img_output, center=(int((x - sx0) / 2) + sx0, int((y - sy0) / 2) + sy0),
radius=int(scale / 2),
color=(0, 0, 255), thickness=1)
drawing = False
if event == cv2.EVENT_RBUTTONDOWN:
end = 1
press('enter')
elif shape == "line":
def scale_draw(event, x, y, flags, param):
nonlocal sx0, sy0, scale, drawing, end, img_output
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
sx0, sy0 = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.line(img_copy, (sx0, sy0), (x, y), (0, 0, 255), 1)
elif event == cv2.EVENT_LBUTTONUP:
cv2.line(img_copy, (sx0, sy0), (x, y), (0, 0, 255), 1)
cv2.line(img_output, (sx0, sy0), (x, y), (0, 0, 255), 1)
drawing = False
scale = math.sqrt((sx0 - x) ** 2 + (sy0 - y) ** 2)
if event == cv2.EVENT_RBUTTONDOWN:
end = 1
img_copy = img.copy()
end = 0
drawing = False
cv2.putText(img_copy, 'Scaling', (10, 50), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 1, cv2.LINE_AA)
while True:
cv2.imshow('window', img_copy)
if drawing:
img_copy = img.copy()
cv2.setMouseCallback('window', scale_draw)
k = cv2.waitKey(0)
if end == 1:
break
if k == 27:
break
if k == 27:
break
else:
scale = 0
# endregion
drawing = False
# region --- 3. Measure body length --- #
img_copy = img.copy()
cv2.putText(img_copy, 'Body Length', (10, 50), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 1, cv2.LINE_AA)
body_length = [0] * num_ind
bl_data = []
# todo: code for zooming is not great. but I have no idea how to improve yet.
zoom, zoom_xy = [1, 1, 1], [np.array([0, 0]), np.array([0, 0]), np.array([0, 0])] # for x2, x4, x8
mouse_xy = np.array([0, 0])
for ii in range(num_ind):
current_bl = [[0, 0], [0, 0]]
bl = 0
while True:
cv2.imshow('window', img_copy)
def bl_draw(event, x, y, flags, param):
nonlocal bl_data, ii, img, img_copy, current_bl, mouse_xy, zoom, zoom_xy, bl
if event == cv2.EVENT_MOUSEMOVE:
mouse_xy = np.array([x, y])
if event == cv2.EVENT_LBUTTONDOWN:
current_bl[0] = (((np.array([x, y])+zoom_xy[2])/zoom[2]+zoom_xy[1])/zoom[1]+zoom_xy[0])/zoom[0]
current_bl[0] = current_bl[0].astype(int)
if event == cv2.EVENT_LBUTTONUP:
current_bl[1] = (((np.array([x, y])+zoom_xy[2])/zoom[2]+zoom_xy[1])/zoom[1]+zoom_xy[0])/zoom[0]
current_bl[1] = current_bl[1].astype(int)
cv2.line(img_copy,
((current_bl[0]*zoom[0]-zoom_xy[0])*zoom[1]-zoom_xy[1])*zoom[2]-zoom_xy[2],
((current_bl[1]*zoom[0]-zoom_xy[0])*zoom[1]-zoom_xy[1])*zoom[2]-zoom_xy[2], (0, 255, 255), 2)
bl = norm(current_bl[1] - current_bl[0])
if event == cv2.EVENT_RBUTTONDOWN:
cv2.line(img_copy,
((current_bl[0]*zoom[0]-zoom_xy[0])*zoom[1]-zoom_xy[1])*zoom[2]-zoom_xy[2],
((current_bl[1]*zoom[0]-zoom_xy[0])*zoom[1]-zoom_xy[1])*zoom[2]-zoom_xy[2], (0, 0, 255), 2)
cv2.line(img_output, current_bl[0], current_bl[1], (0, 0, 255), 2)
press("r")
cv2.imshow('window', img_copy)
cv2.setMouseCallback('window', bl_draw)
k = cv2.waitKey(0)
def zoom_func(img_z, mouse_xy, img_shape, zoom):
mouse_xy[0] = max(mouse_xy[0], img_shape[0] / 4)
mouse_xy[1] = max(mouse_xy[1], img_shape[1] / 4)
mouse_xy[0] = min(mouse_xy[0], img_shape[0] * 3 / 4)
mouse_xy[1] = min(mouse_xy[1], img_shape[1] * 3 / 4)
img_zoom = cv2.resize(img_z, dsize=(img_shape * 2))
img_zoom = img_zoom[int(mouse_xy[1] * 2 - img_shape[1] / 2):int(mouse_xy[1] * 2 + img_shape[1] / 2),
int(mouse_xy[0] * 2 - img_shape[0] / 2):int(mouse_xy[0] * 2 + img_shape[0] / 2)]
zoom_xy = mouse_xy * 2 - img_shape / 2
zoom_xy = zoom_xy.astype(int)
return img_zoom, zoom_xy, zoom*2
if k == ord("z"):
if zoom[1] == 2 and zoom[2] == 1:
img_copy, zoom_xy[2], zoom[2] = zoom_func(img_copy, mouse_xy, img_shape, zoom[2])
elif zoom[0] == 2 and zoom[1] == 1:
img_copy, zoom_xy[1], zoom[1] = zoom_func(img_copy, mouse_xy, img_shape, zoom[1])
elif zoom[0] == 1:
img_copy, zoom_xy[0], zoom[0] = zoom_func(img_copy, mouse_xy, img_shape, zoom[0])
if k == ord("x"):
# cancel zoom when redo
img_copy = img.copy()
if ii > 0:
for i in range(ii-1):
cv2.line(img_copy,
((bl_data[ii][0]*zoom[0]-zoom_xy[0])*zoom[1]-zoom_xy[1])*zoom[2]-zoom_xy[2],
((bl_data[ii][1]*zoom[0]-zoom_xy[0])*zoom[1]-zoom_xy[1])*zoom[2]-zoom_xy[2], (0, 0, 255), 2)
zoom, zoom_xy = [1, 1, 1], [np.array([0, 0]), np.array([0, 0]), np.array([0, 0])]
if k == ord("r"):
bl_data.append(current_bl)
body_length[ii] = bl
break
if k == 27:
break
if k == 27:
break
if k == 27:
break
# endregion
# region ----- 4. Output -----#
df.iloc[i:(i+1), 0:7] = [name, width, height, count, fps, frame_id, scale/img_scale,]
for ii in range(num_ind):
df.iloc[i:(i+1), 7+ii] = body_length[ii]/img_scale
cv2.putText(img_output, name, (10, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imwrite(odir + os.sep + name + ".jpg", img_output)
cv2.destroyAllWindows()
print(df)
with open(odir + os.sep + 'res.pickle', mode='wb') as f:
pickle.dump(df, f)
df.to_csv(odir + os.sep + '/res.csv')
# ---------------------------------------------------------------------------------------------
# GUI
# ---------------------------------------------------------------------------------------------
sg.theme('Dark')
frame1 = sg.Frame('', [
[sg.Text("In"),
sg.InputText('Input folder', key='-INPUTTEXT-', enable_events=True, ),
sg.FolderBrowse(button_text='select', size=(8, 1), key="-INFOLDERNAME-")
],
[sg.Text("Out"),
sg.InputText('Output folder', key='-INPUTTEXT-', enable_events=True, ),
sg.FolderBrowse(button_text='select', size=(8, 1), key="-OUTFOLDERNAME-"),
sg.Text("(* will be created if not specified)")
],
[sg.Text("Display img relative size (default = 0.5)"),
sg.In(key='-scale-')],
[sg.Text("Measure scale"),
sg.Combo(['True', 'False'], default_value="True", size=(4, 1), key="-measure_scale")
],
[sg.Text("Methods"),
sg.Combo(['circle', 'line'], default_value="circle", size=(4, 1), key="-shape")
],
[sg.Text("Num of individual (default = 2)"),
sg.In(key='-NUMIND-')],
[sg.Text("Sample frame interval (default = 3000)"),
sg.In(key='-FRAMEINTERVAL-')],
[sg.Text("skip analyzed files", size=(15,1)),
sg.Combo(['true', 'false'], default_value="true", size=(6, 1), key="-SKIP_ANALYZED-")],
], size=(800, 300))
frame2 = sg.Frame('', [
[sg.Submit(button_text='Start', size=(10, 3), key='button_start')]], size=(100, 140))
layout = [[frame1, frame2]]
window = sg.Window('files', layout, resizable=True)
while True:
event, values = window.read()
if event is None:
print('exit')
break
if len(values["-INFOLDERNAME-"]) == 0:
print("no input!")
else:
if event == 'button_start':
idir = values["-INFOLDERNAME-"]
if len(values["-OUTFOLDERNAME-"]) == 0:
odir = idir + "/ouput/"
if not os.path.exists(odir):
os.makedirs(odir)
else:
odir = values["-OUTFOLDERNAME-"]
if len(values["-scale-"]) == 0:
img_scale = 0.5
else:
img_scale = float(values["-scale-"])
if len(values["-NUMIND-"]) == 0:
num_ind = 2
else:
num_ind = int(values["-NUMIND-"])
if len(values["-FRAMEINTERVAL-"]) == 0:
frame_interval = 3000
else:
frame_interval = int(values["-FRAMEINTERVAL-"])
skip_analyzed = values["-SKIP_ANALYZED-"]
shape = values["-shape"]
measure_scale = values["-measure_scale"]
ImageAnalysis(idir, odir, img_scale, measure_scale, shape, num_ind, frame_interval, skip_analyzed)
window.close()