-
Notifications
You must be signed in to change notification settings - Fork 5
/
godec_opencv.py
executable file
·79 lines (61 loc) · 1.61 KB
/
godec_opencv.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
#%% Import libraries
import time
from numpy import array, column_stack
try:
import cv2 as cv
except ImportError:
raise ImportError('OpenCV is requires to read video files')
from godec import godec
from utils import play_2d_results
#%% Open video file
cap = cv.VideoCapture('dataset/demo.avi')
# Check if video was opened successfully
if not cap.isOpened():
raise("Error opening video stream or file")
# Read until video is completed
M = None
height = None
width = None
i = 0
debug = True
print("Press 'q' to stop...")
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
height, width = frame.shape
# Stack frames as column vectors
F = frame.T.reshape(-1)
if i == 0:
M = array([F]).T
else:
M = column_stack((M, F))
if debug:
# Display the resulting frame
cv.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv.waitKey(25) & 0xFF == ord('q'):
break
i = i + 1
# print(i)
# Break the loop
else:
break
# Release the video capture object
cap.release()
# Closes all the frames
if debug:
cv.destroyAllWindows()
assert M is not None
assert width is not None
assert height is not None
print("Number of frames: ", i, " with size ", (width, height))
print("Processing M with shape ", M.shape)
#%% Decompose
t = time.time()
L, S, LS, _ = godec(M)
elapsed = time.time() - t
print(elapsed, "sec elapsed")
#%% Play results
play_2d_results(M, LS, L, S, width, height)