forked from xortical/evm_experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyevm.py
166 lines (149 loc) · 5.49 KB
/
pyevm.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
import cv2
import numpy as np
import scipy.signal as signal
import scipy.fftpack as fftpack
#convert RBG to YIQ
def rgb2ntsc(src):
[rows,cols]=src.shape[:2]
dst=np.zeros((rows,cols,3),dtype=np.float64)
T = np.array([[0.114, 0.587, 0.298], [-0.321, -0.275, 0.596], [0.311, -0.528, 0.212]])
for i in range(rows):
for j in range(cols):
dst[i, j]=np.dot(T,src[i,j])
return dst
#convert YIQ to RBG
def ntsc2rbg(src):
[rows, cols] = src.shape[:2]
dst=np.zeros((rows,cols,3),dtype=np.float64)
T = np.array([[1, -1.108, 1.705], [1, -0.272, -0.647], [1, 0.956, 0.620]])
for i in range(rows):
for j in range(cols):
dst[i, j]=np.dot(T,src[i,j])
return dst
#Build Gaussian Pyramid
def build_gaussian_pyramid(src,level=3):
s=src.copy()
pyramid=[s]
for i in range(level):
s=cv2.pyrDown(s)
pyramid.append(s)
return pyramid
#Build Laplacian Pyramid
def build_laplacian_pyramid(src,levels=3):
gaussianPyramid = build_gaussian_pyramid(src, levels)
pyramid=[]
for i in range(levels,0,-1):
GE=cv2.pyrUp(gaussianPyramid[i])
L=cv2.subtract(gaussianPyramid[i-1],GE)
pyramid.append(L)
return pyramid
#load video from file
def load_video(video_filename):
cap=cv2.VideoCapture(video_filename)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width, height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
video_tensor=np.zeros((frame_count,height,width,3),dtype='float')
x=0
while cap.isOpened():
ret,frame=cap.read()
if ret is True:
video_tensor[x]=frame
x+=1
else:
break
return video_tensor,fps
# apply temporal ideal bandpass filter to gaussian video
def temporal_ideal_filter(tensor,low,high,fps,axis=0):
fft=fftpack.fft(tensor,axis=axis)
frequencies = fftpack.fftfreq(tensor.shape[0], d=1.0 / fps)
bound_low = (np.abs(frequencies - low)).argmin()
bound_high = (np.abs(frequencies - high)).argmin()
fft[:bound_low] = 0
fft[bound_high:-bound_high] = 0
fft[-bound_low:] = 0
iff=fftpack.ifft(fft, axis=axis)
return np.abs(iff)
# build gaussian pyramid for video
def gaussian_video(video_tensor,levels=3):
for i in range(0,video_tensor.shape[0]):
frame=video_tensor[i]
pyr=build_gaussian_pyramid(frame,level=levels)
gaussian_frame=pyr[-1]
if i==0:
vid_data=np.zeros((video_tensor.shape[0],gaussian_frame.shape[0],gaussian_frame.shape[1],3))
vid_data[i]=gaussian_frame
return vid_data
#amplify the video
def amplify_video(gaussian_vid,amplification=50):
return gaussian_vid*amplification
#reconstract video from original video and gaussian video
def reconstract_video(amp_video,origin_video,levels=3):
final_video=np.zeros(origin_video.shape)
for i in range(0,amp_video.shape[0]):
img = amp_video[i]
for x in range(levels):
img=cv2.pyrUp(img)
img=img+origin_video[i]
final_video[i]=img
return final_video
#save video to files
def save_video(video_tensor):
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
[height,width]=video_tensor[0].shape[0:2]
writer = cv2.VideoWriter("out.avi", fourcc, 30, (width, height), 1)
for i in range(0,video_tensor.shape[0]):
writer.write(cv2.convertScaleAbs(video_tensor[i]))
writer.release()
#magnify color
def magnify_color(video_name,low,high,levels=3,amplification=20):
t,f=load_video(video_name)
gau_video=gaussian_video(t,levels=levels)
filtered_tensor=temporal_ideal_filter(gau_video,low,high,f)
amplified_video=amplify_video(filtered_tensor,amplification=amplification)
final=reconstract_video(amplified_video,t,levels=3)
save_video(final)
#build laplacian pyramid for video
def laplacian_video(video_tensor,levels=3):
tensor_list=[]
for i in range(0,video_tensor.shape[0]):
frame=video_tensor[i]
pyr=build_laplacian_pyramid(frame,levels=levels)
if i==0:
for k in range(levels):
tensor_list.append(np.zeros((video_tensor.shape[0],pyr[k].shape[0],pyr[k].shape[1],3)))
for n in range(levels):
tensor_list[n][i] = pyr[n]
return tensor_list
#butterworth bandpass filter
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
omega = 0.5 * fs
low = lowcut / omega
high = highcut / omega
b, a = signal.butter(order, [low, high], btype='band')
y = signal.lfilter(b, a, data, axis=0)
return y
#reconstract video from laplacian pyramid
def reconstract_from_tensorlist(filter_tensor_list,levels=3):
final=np.zeros(filter_tensor_list[-1].shape)
for i in range(filter_tensor_list[0].shape[0]):
up = filter_tensor_list[0][i]
for n in range(levels-1):
up=cv2.pyrUp(up)+filter_tensor_list[n + 1][i]#up=cv2.pyrUp(up)
final[i]=up
return final
#manify motion
def magnify_motion(video_name,low,high,levels=3,amplification=20):
t,f=load_video(video_name)
lap_video_list=laplacian_video(t,levels=levels)
filter_tensor_list=[]
for i in range(levels):
filter_tensor=butter_bandpass_filter(lap_video_list[i],low,high,f)
filter_tensor*=amplification
filter_tensor_list.append(filter_tensor)
recon=reconstract_from_tensorlist(filter_tensor_list)
final=t+recon
save_video(final)
if __name__=="__main__":
# magnify_color("baby.mp4",0.4,3)
magnify_color("111.mp4",0.4,2)