-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
100 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import argparse | ||
import numpy as np | ||
import utils as ut | ||
import matplotlib.pyplot as plt | ||
import cv2 | ||
import fast_fourier_transform as fft | ||
|
||
parser = argparse.ArgumentParser(description="Beats per minute monitor using FFT by analyzing a video") | ||
parser.add_argument("video", help="Video to analyze", type=str) | ||
parser.add_argument("--size", "-s", help="Size of observed window. It is a square in the center of the screen.", | ||
type=int, default=30) | ||
parser.add_argument("--npfft",help="Use numpy's fft method if True, use custom method if False",type=bool,default=True) | ||
|
||
args = parser.parse_args() | ||
|
||
cap = cv2.VideoCapture(args.video) | ||
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
length = int(2 ** np.floor(np.log2(length))) | ||
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | ||
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | ||
fps = cap.get(cv2.CAP_PROP_FPS) | ||
|
||
size = args.size | ||
upperLeftCornerX = width // 2 - size // 2 | ||
upperLeftCornerY = height // 2 - size // 2 | ||
lowerRightCornerX = width // 2 + size // 2 | ||
lowerRightCornerY = height// 2 + size // 2 | ||
|
||
r, g, b = ut.load_frames(cap,upperLeftCornerX,upperLeftCornerY,lowerRightCornerX,lowerRightCornerY,length) | ||
cap.release() | ||
cv2.destroyAllWindows() | ||
|
||
n = length | ||
f = np.linspace(-n/2,n/2-1,n)*fps/n | ||
r = r[0,0:n]-np.mean(r[0,0:n]) | ||
g = g[0,0:n]-np.mean(g[0,0:n]) | ||
b = b[0,0:n]-np.mean(b[0,0:n]) | ||
|
||
fft_method = None | ||
if args.npfft: | ||
fft_method = np.fft.fft | ||
else: | ||
fft_method = fft.FFT_R | ||
|
||
R = np.abs(np.fft.fftshift(fft_method(r)))**2 | ||
G = np.abs(np.fft.fftshift(fft_method(g)))**2 | ||
B = np.abs(np.fft.fftshift(fft_method(b)))**2 | ||
|
||
plt.subplot(2,1,1) | ||
plt.plot(60*f,R, 'red') | ||
plt.plot(60*f,G, 'green') | ||
plt.plot(60*f,B, 'blue') | ||
plt.xlim(0,200) | ||
plt.xlabel("frecuencia [1/minuto]") | ||
|
||
title = args.video.split("/")[-1].split(".")[0] | ||
plt.title(title) | ||
|
||
plt.subplot(2,1,2) | ||
plt.plot(np.arange(length),r, 'red') | ||
plt.plot(np.arange(length),g, 'green') | ||
plt.plot(np.arange(length),b, 'blue') | ||
plt.xlabel("valor r g b") | ||
plt.tight_layout() | ||
plt.savefig("./graphs/{}_{}.png".format(title, args.size)) | ||
#plt.show() | ||
print("Frecuencia cardíaca: ", abs(f[np.argmax(R)])*60, " pulsaciones por minuto en R") | ||
print("Frecuencia cardíaca: ", abs(f[np.argmax(G)])*60, " pulsaciones por minuto en G") | ||
print("Frecuencia cardíaca: ", abs(f[np.argmax(B)])*60, " pulsaciones por minuto en B") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import numpy as np | ||
|
||
def load_frames(cap,upperLeftCornerX,upperLeftCornerY,lowerRightCornerX,lowerRightCornerY,length): | ||
r = np.zeros((1,length)) | ||
g = np.zeros((1,length)) | ||
b = np.zeros((1,length)) | ||
|
||
currentFrameNo = 0 | ||
while(cap.isOpened() and currentFrameNo < length): | ||
ret, frame = cap.read() | ||
|
||
if ret == True: | ||
r[0,currentFrameNo] = np.mean(frame[upperLeftCornerX:lowerRightCornerX, upperLeftCornerY:lowerRightCornerY,0]) | ||
g[0,currentFrameNo] = np.mean(frame[upperLeftCornerX:lowerRightCornerX, upperLeftCornerY:lowerRightCornerY,1]) | ||
b[0,currentFrameNo] = np.mean(frame[upperLeftCornerX:lowerRightCornerX, upperLeftCornerY:lowerRightCornerY,2]) | ||
else: | ||
break | ||
currentFrameNo += 1 | ||
print(currentFrameNo) | ||
return [r, g, b] | ||
|