-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfExercises02.py
439 lines (325 loc) · 13.7 KB
/
ProfExercises02.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# MIT LICENSE
#
# Copyright 2024 Michael J. Reale
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###############################################################################
# IMPORTS
###############################################################################
import sys
import numpy as np
import torch
import cv2
import pandas
import sklearn
def filterNeighborhood2D(image, kernel, crow, ccol):
halfH = kernel.shape[0]//2
halfW = kernel.shape[1]//2
startOffH = (1 - kernel.shape[0]%2)
startOffW = (1 - kernel.shape[1]%2)
endRow = crow + halfH
endCol = ccol + halfW
startRow = crow - halfH + startOffH
startCol = ccol - halfW + startOffW
clamp_startRow = max(0, startRow)
clamp_startCol = max(0, startCol)
neighborhood = image[clamp_startRow:(endRow+1), clamp_startCol:(endCol+1)]
if startRow < 0:
kernel = kernel[-startRow:]
elif endRow > (image.shape[0]-1):
off = image.shape[0] - 1 - endRow
kernel = kernel[0:(kernel.shape[0]+off)]
if startCol < 0:
kernel = kernel[:, -startCol:]
elif endCol > (image.shape[1]-1):
off = image.shape[1] - 1 - endCol
kernel = kernel[:, 0:(kernel.shape[1]+off)]
#print("NEIGHBORHOOD:", neighborhood.shape)
#print("KERNEL:", kernel.shape)
value = kernel * neighborhood
value = np.sum(value)
return value
def filterNeighborhood3D(video, kernel, ctime, crow, ccol):
halfT = kernel.shape[0]//2
halfH = kernel.shape[1]//2
halfW = kernel.shape[2]//2
startOffT = (1 - kernel.shape[0]%2)
startOffH = (1 - kernel.shape[1]%2)
startOffW = (1 - kernel.shape[2]%2)
endTime = ctime + halfT
endRow = crow + halfH
endCol = ccol + halfW
startTime = ctime - halfT + startOffT
startRow = crow - halfH + startOffH
startCol = ccol - halfW + startOffW
clamp_startTime = max(0, startTime)
clamp_startRow = max(0, startRow)
clamp_startCol = max(0, startCol)
neighborhood = video[ clamp_startTime:(endTime+1),
clamp_startRow:(endRow+1),
clamp_startCol:(endCol+1)]
def get_bounds(start, end, max_image_size, max_kernel_size):
if start < 0:
s = -start
e = max_kernel_size
elif end > (max_image_size-1):
off = max_image_size - 1 - end
s = 0
e = max_kernel_size + off
else:
s = 0
e = max_kernel_size
return s,e
st, et = get_bounds(startTime, endTime, video.shape[0], kernel.shape[0])
sr, er = get_bounds(startRow, endRow, video.shape[1], kernel.shape[1])
sc, ec = get_bounds(startCol, endCol, video.shape[2], kernel.shape[2])
kernel = kernel[st:et, sr:er, sc:ec]
print("NEIGHBORHOOD:", neighborhood.shape)
print("KERNEL:", kernel.shape)
value = kernel * neighborhood
value = np.sum(value)
return value
def filter2D(image, kernel):
output = np.copy(image)
for row in range(image.shape[0]):
for col in range(image.shape[1]):
output[row,col] = filterNeighborhood2D(image, kernel, row, col)
return output
def filter3D(video, kernel):
output = np.copy(video)
for t in range(video.shape[0]):
for row in range(video.shape[1]):
for col in range(video.shape[2]):
output[t,row,col] = filterNeighborhood3D(video, kernel, t, row, col)
return output
def compute_one_optical_flow_horn_shunck(prev_frame, cur_frame,
kfx, kfy, kft1, kft2,
max_iter=20):
fx = (cv2.filter2D(prev_frame, cv2.CV_64F, kfx)
+ cv2.filter2D(cur_frame, cv2.CV_64F, kfx))
fy = (cv2.filter2D(prev_frame, cv2.CV_64F, kfy)
+ cv2.filter2D(cur_frame, cv2.CV_64F, kfy))
ft = (cv2.filter2D(prev_frame, cv2.CV_64F, kft1)
+ cv2.filter2D(cur_frame, cv2.CV_64F, kft2))
fx /= 4.0
fy /= 4.0
ft /= 4.0
u = np.zeros(fx.shape, dtype="float64")
v = np.zeros(fx.shape, dtype="float64")
lap_filter = np.array([[0, 0.25, 0],
[0.25,0,0.25],
[0,0.25,0]], dtype="float64")
converged = False
iter_cnt = 0
lamb = 0.1
print_inc = 5
while not converged:
# MAGIC
uav = cv2.filter2D(u, cv2.CV_64F, lap_filter)
vav = cv2.filter2D(v, cv2.CV_64F, lap_filter)
P = fx*uav + fy*vav + ft
D = lamb + fx*fx + fy*fy
PD = P/D
u = uav - fx*PD
v = vav - fy*PD
iter_cnt += 1
if iter_cnt % print_inc == 0:
print("ITERATION", iter_cnt, "DONE...")
if iter_cnt >= max_iter:
converged = True
extra = np.zeros_like(u)
combo = np.stack([u,v,extra], axis=-1)
return combo
def convert_to_hsv_flow(flow):
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv = np.zeros((flow.shape[0], flow.shape[1], 3), dtype="uint8")
#print("MIN MAX:", np.amin(ang), np.amax(ang))
hsv[...,1] = 255
hsv[...,0] = cv2.normalize(ang, None, 0, 255, cv2.NORM_MINMAX) # 255*ang/(2.0*np.pi) # ang*180.0/np.pi
hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return flow
def compute_optical_flow_horn_shunck(video_frames, kfx, kfy, kft1, kft2,
max_iter=20):
all_flow = []
prev_frame = None
#index = 0
for index, frame in enumerate(video_frames):
print("** FRAME", index, "************************")
if prev_frame is None:
prev_frame = frame
flow = compute_one_optical_flow_horn_shunck(prev_frame, frame,
kfx, kfy, kft1, kft2,
max_iter=max_iter)
flow = convert_to_hsv_flow(flow)
all_flow.append(flow)
prev_frame = frame
#index += 1
return all_flow
def compute_optical_flow_farneback(video_frames):
all_flow = []
prev_frame = None
#index = 0
for index, frame in enumerate(video_frames):
print("** FRAME", index, "************************")
if prev_frame is None:
prev_frame = frame
flow = cv2.calcOpticalFlowFarneback(prev_frame, frame,
None, 0.5, 3,
winsize=31, #15,
iterations=3,
poly_n=5,
poly_sigma=1.2,
flags=cv2.OPTFLOW_FARNEBACK_GAUSSIAN)
flow = convert_to_hsv_flow(flow)
all_flow.append(flow)
prev_frame = frame
#index += 1
return all_flow
def make_test_video(image_size=(480,640), frame_cnt=30, inc_x=5, inc_y=0):
video_frames = []
start_pos = [100,100]
end_pos = [200,200]
for index in range(frame_cnt):
frame = np.zeros(image_size, dtype="float64")
cv2.rectangle(frame, start_pos, end_pos, (1.0,), -1)
video_frames.append(frame)
start_pos[0] += inc_x
end_pos[0] += inc_x
start_pos[1] += inc_y
end_pos[1] += inc_y
return video_frames
###############################################################################
# MAIN
###############################################################################
def main():
dummy_video = np.zeros((4,4,4), dtype="float64")
dummy_filter = np.zeros((2,2,2), dtype="float64")
dummy_output = filter3D(dummy_video, dummy_filter)
###############################################################################
# PYTORCH
###############################################################################
b = torch.rand(5,3)
print(b)
print("Torch CUDA?:", torch.cuda.is_available())
###############################################################################
# PRINT OUT VERSIONS
###############################################################################
print("Torch:", torch.__version__)
print("Numpy:", np.__version__)
print("OpenCV:", cv2.__version__)
print("Pandas:", pandas.__version__)
print("Scikit-Learn:", sklearn.__version__)
###############################################################################
# OPENCV
###############################################################################
if len(sys.argv) <= 1:
# Webcam
print("Opening webcam...")
# Linux/Mac (or native Windows) with direct webcam connection
capture = cv2.VideoCapture(1) #, cv2.CAP_DSHOW) # CAP_DSHOW recommended on Windows
# WSL: Use Yawcam to stream webcam on webserver
# https://www.yawcam.com/download.php
# Get local IP address and replace
#IP_ADDRESS = "192.168.0.7"
#capture = cv2.VideoCapture("http://" + IP_ADDRESS + ":8081/video.mjpg")
# Did we get it?
if not capture.isOpened():
print("ERROR: Cannot open capture!")
exit(1)
# Set window name
windowName = "Webcam"
else:
# Trying to load video from argument
# Get filename
filename = sys.argv[1]
# Load video
capture = cv2.VideoCapture(filename)
# Check if data is invalid
if not capture.isOpened():
print("ERROR: Could not open or find the video!")
exit(1)
# Set window name
windowName = "Video"
# Create window ahead of time
cv2.namedWindow(windowName)
# While not closed...
key = -1
prev_frame = None
kfx = np.array([[-1, 1],
[-1, 1]], dtype="float64")
kfy = np.array([[-1,-1],
[1,1]], dtype="float64")
kft1 = np.array([[-1,-1],
[-1,-1]], dtype="float64")
kft2 = np.array([[1,1],
[1,1]], dtype="float64")
video_frames = []
while key == -1:
# Get next frame from capture
ret, frame = capture.read()
if ret == True:
# Show the image
cv2.imshow(windowName, frame)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).astype("float64")
gray_image /= 255.0
kernel_size = 17
#gray_image = cv2.GaussianBlur(gray_image,
# ksize=(kernel_size, kernel_size),
# sigmaX=0)
cv2.imshow("GRAY", gray_image)
'''
#fx = cv2.filter2D(gray_image, cv2.CV_64F, kfx)
fx = filter2D(gray_image, kfx)
cv2.imshow("FX", np.absolute(fx)*4.0)
if prev_frame is None:
prev_frame = np.copy(gray_image)
diff_image = gray_image - prev_frame
diff_image = np.absolute(diff_image)
cv2.imshow("DIFF", diff_image)
'''
video_frames.append(gray_image)
prev_frame = np.copy(gray_image)
else:
break
# Wait 30 milliseconds, and grab any key presses
key = cv2.waitKey(30)
# Release the capture and destroy the window
capture.release()
cv2.destroyAllWindows()
key = -1
ESC_KEY = 27
index = 0
# OVERRIDE
video_frames = make_test_video(inc_x=0, inc_y=5)
#flow_frames = compute_optical_flow_horn_shunck(video_frames,
# kfx, kfy,
# kft1, kft2)
flow_frames = compute_optical_flow_farneback(video_frames)
while key != ESC_KEY:
cur_frame = video_frames[index]
flow_frame = np.absolute(flow_frames[index])
cv2.imshow("ORIGINAL", cur_frame)
cv2.imshow("FLOW", flow_frame)
key = cv2.waitKey(33)
index += 1
if index >= len(video_frames):
index = 0
cv2.destroyAllWindows()
# Close down...
print("Closing application...")
if __name__ == "__main__":
main()
# The end