Skip to content

Commit

Permalink
Added graphic saving and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonrod committed Oct 23, 2018
1 parent ac0f11e commit d13fb6e
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 67 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Como leer el nombre de los archivos con graficos:
`{Nombre del video del que salieron}_{Tamaño de la ventana}.png`
# ITBA - Métodos Numéricos Avanzados - Grupo 10
Second Advanced Numerical Methods Project: Hear Beat Rate


## Getting Started
These instructions will install the development environment into your local machine.

Expand Down
Binary file added data/Axel_70bpm.MOV
Binary file not shown.
Binary file added data/FernanSinEstabilizacion.mp4
Binary file not shown.
2 changes: 1 addition & 1 deletion fast_fourier_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def FFT_R(x):
splitN = int(N/2)

if np.log2(N) % 1 > 0:
raise ValueError('values count must be a power of 2, "{}" given.'.format(values_count))
raise ValueError('values count must be a power of 2, "{}" given.'.format(N))
elif N < 2:
return x
else:
Expand Down
9 changes: 5 additions & 4 deletions given-data/heartrate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
b# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 16 19:23:10 2017
Expand Down Expand Up @@ -52,15 +52,16 @@
G = np.abs(np.fft.fftshift(np.fft.fft(g)))**2
B = np.abs(np.fft.fftshift(np.fft.fft(b)))**2

plt.plot(60*f,R)
plt.plot(60*f,R,'red')
plt.xlim(0,200)


plt.plot(60*f,G)
plt.plot(60*f,G,'green')
plt.xlim(0,200)
plt.xlabel("frecuencia [1/minuto]")

plt.plot(60*f,B)
plt.plot(60*f,B,'blue')
plt.xlim(0,200)
plt.show()

print("Frecuencia cardíaca: ", abs(f[np.argmax(G)])*60, " pulsaciones por minuto")
Binary file added graphs/2017-09-14 21_300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphs/Axel_70bpm_300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphs/FernanSinEstabilizacion_300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 0 additions & 62 deletions heartrate.py

This file was deleted.

70 changes: 70 additions & 0 deletions main.py
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")

21 changes: 21 additions & 0 deletions utils.py
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]

0 comments on commit d13fb6e

Please sign in to comment.