forked from brycedrennan/eulerian-magnification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheulerian_magnify.py
180 lines (140 loc) · 6.28 KB
/
eulerian_magnify.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
import cv2
import os
import sys
import numpy
import pylab
import scipy.signal
import scipy.fftpack
import cv2.cv as cv
def eulerian_magnification(video_filename, image_processing='gaussian', freq_min=0.833, freq_max=1, amplification=50, pyramid_levels=4):
"""Amplify subtle variation in a video and save it to disk"""
path_to_video = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), video_filename)
orig_vid, fps = load_video(path_to_video)
if image_processing == 'gaussian':
vid_data = gaussian_video(orig_vid, pyramid_levels)
elif image_processing == 'laplacian':
vid_data = laplacian_video(orig_vid, pyramid_levels)
vid_data = temporal_bandpass_filter(vid_data, fps, freq_min=freq_min, freq_max=freq_max)
print "Amplifying signal by factor of " + str(amplification)
vid_data *= amplification
file_name = os.path.splitext(path_to_video)[0]
file_name = file_name + "_min"+str(freq_min)+"_max"+str(freq_max)+"_amp"+str(amplification)
combine_pyramid_and_save(vid_data, orig_vid, pyramid_levels, fps, save_filename=file_name + '_magnified.avi')
def show_frequencies(video_filename, bounds=None):
"""Graph the average value of the video as well as the frequency strength"""
original_video, fps = load_video(video_filename)
print fps
averages = []
if bounds:
for x in range(1, original_video.shape[0] - 1):
averages.append(original_video[x, bounds[2]:bounds[3], bounds[0]:bounds[1], :].sum())
else:
for x in range(1, original_video.shape[0] - 1):
averages.append(original_video[x, :, :, :].sum())
charts_x = 1
charts_y = 2
pylab.figure(figsize=(charts_y, charts_x))
pylab.subplots_adjust(hspace=.7)
pylab.subplot(charts_y, charts_x, 1)
pylab.title("Pixel Average")
pylab.plot(averages)
frequencies = scipy.fftpack.fftfreq(len(averages), d=1.0 / fps)
pylab.subplot(charts_y, charts_x, 2)
pylab.title("FFT")
pylab.axis([0, 15, -2000000, 5000000])
pylab.plot(frequencies, scipy.fftpack.fft(averages))
pylab.show()
def temporal_bandpass_filter(data, fps, freq_min=0.833, freq_max=1, axis=0):
print "Applying bandpass between " + str(freq_min) + " and " + str(freq_max) + " Hz"
fft = scipy.fftpack.fft(data, axis=axis)
frequencies = scipy.fftpack.fftfreq(data.shape[0], d=1.0 / fps)
bound_low = (numpy.abs(frequencies - freq_min)).argmin()
bound_high = (numpy.abs(frequencies - freq_max)).argmin()
fft[:bound_low] = 0
fft[bound_high:-bound_high] = 0
fft[-bound_low:] = 0
return scipy.fftpack.ifft(fft, axis=0)
def load_video(video_filename):
"""Load a video into a numpy array"""
print "Loading " + video_filename
# noinspection PyArgumentList
capture = cv2.VideoCapture(video_filename)
frame_count = int(capture.get(cv.CV_CAP_PROP_FRAME_COUNT))
width, height = get_capture_dimensions(capture)
fps = int(capture.get(cv.CV_CAP_PROP_FPS))
x = 0
orig_vid = numpy.zeros((frame_count, height, width, 3), dtype='uint8')
while True:
_, frame = capture.read()
if frame == None or x >= frame_count:
break
orig_vid[x] = frame
x += 1
capture.release()
return orig_vid, fps
def save_video(video, fps, save_filename='media/output.avi'):
"""Save a video to disk"""
fourcc = cv.CV_FOURCC('M', 'J', 'P', 'G')
writer = cv2.VideoWriter(save_filename, fourcc, fps, (video.shape[2], video.shape[1]), 1)
for x in range(0, video.shape[0]):
res = cv2.convertScaleAbs(video[x])
writer.write(res)
def gaussian_video(video, shrink_multiple):
"""Create a gaussian representation of a video"""
vid_data = None
for x in range(0, video.shape[0]):
frame = video[x]
gauss_copy = numpy.ndarray(shape=frame.shape, dtype="float")
gauss_copy[:] = frame
for i in range(shrink_multiple):
gauss_copy = cv2.pyrDown(gauss_copy)
if x == 0:
vid_data = numpy.zeros((video.shape[0], gauss_copy.shape[0], gauss_copy.shape[1], 3))
vid_data[x] = gauss_copy
return vid_data
def laplacian_video(video, shrink_multiple):
vid_data = None
for x in range(0, video.shape[0]):
frame = video[x]
gauss_copy = numpy.ndarray(shape=frame.shape, dtype="float")
gauss_copy[:] = frame
for i in range(shrink_multiple):
prev_copy = gauss_copy[:]
gauss_copy = cv2.pyrDown(gauss_copy)
laplacian = prev_copy - cv2.pyrUp(gauss_copy)
if x == 0:
vid_data = numpy.zeros((video.shape[0], laplacian.shape[0], laplacian.shape[1], 3))
vid_data[x] = laplacian
return vid_data
def combine_pyramid_and_save(g_video, orig_video, enlarge_multiple, fps, save_filename='media/output.avi'):
"""Combine a gaussian video representation with the original and save to file"""
width, height = get_frame_dimensions(orig_video[0])
fourcc = cv.CV_FOURCC('M', 'J', 'P', 'G')
writer = cv2.VideoWriter(save_filename, fourcc, fps, (width, height), 1)
for x in range(0, g_video.shape[0]):
img = numpy.ndarray(shape=g_video[x].shape, dtype='float')
img[:] = g_video[x]
for i in range(enlarge_multiple):
img = cv2.pyrUp(img)
img[:height, :width] = img[:height, :width] + orig_video[x]
res = cv2.convertScaleAbs(img[:height, :width])
writer.write(res)
def get_capture_dimensions(capture):
"""Get the dimensions of a capture"""
width = int(capture.get(cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
return width, height
def get_frame_dimensions(frame):
"""Get the dimensions of a single frame"""
height, width = frame.shape[:2]
return width, height
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = scipy.signal.butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = scipy.signal.lfilter(b, a, data, axis=0)
return y