Advertisement
Skylighty

pulse-working

Nov 20th, 2020
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import cv2
  4.  
  5. # Declare means array
  6. means = []
  7.  
  8. # Declare VideoCapture feature
  9. cap = cv2.VideoCapture("sample8.mp4")
  10.  
  11. # Loop untill the end of the video - frame by frame operations
  12. while (cap.isOpened()):
  13.     ret, frame = cap.read()
  14.     if not ret:
  15.         break
  16.  
  17.     # Extract shape from frame
  18.     (x, y, colors) = frame.shape
  19.  
  20.     # Slicing 350x350 square from the center
  21.     x1 = int((x / 2) - 175)
  22.     x2 = int((x / 2) + 175)
  23.     y1 = int((y / 2) - 175)
  24.     y2 = int((y / 2) + 175)
  25.     square = frame[y1:y2, x1:x2, 0]
  26.  
  27.     # Compute the mean of values in our square and add it to list
  28.     mean = np.mean(square, dtype=np.float64)
  29.     means.append(mean)
  30.  
  31. cap.release()
  32. cv2.destroyAllWindows()
  33.  
  34. # Filtering
  35. filter = [-0.02286961, -0.06362756,  0.57310236,  0.57310236, -0.06362756, -0.02286961]
  36. filtered = np.convolve(means, filter, 'same')
  37.  
  38. # FFT calculation
  39. fourier = abs(np.fft.fft(filtered))
  40. fourier[0] = 0
  41.  
  42. # Plot the means
  43. fig = plt.figure()
  44. plt.plot(filtered)
  45. plt.show()
  46.  
  47. # Raw data is ready, let's plot it:
  48. fig2 = plt.figure()
  49. plt.plot(fourier)
  50. plt.show()
  51.  
  52. # Variables necessary for calculations
  53. fps = 30.0
  54. framecount = len(filtered)
  55. fftmean = np.mean(fourier[:100])
  56. fftpos = []
  57. fftvalues = []
  58.  
  59. # Create a list of indexes which contain high-value frequencies
  60. for i in range(100):
  61.     if fourier[i] > fftmean:
  62.         fftpos.append(i)
  63.         fftvalues.append(round(fourier[i], 5))
  64.  
  65. # Create a list of frequencies under previously specified indexes
  66. freqs = []
  67. for i in range(len(fftpos)):
  68.     freqs.append(round((fftpos[i]*fps)/framecount, 5))
  69.  
  70. # Calculate BPM value for previosuly calculated frequencies
  71. bpms = []
  72. for i in range(len(freqs)):
  73.     bpms.append(round(freqs[i]*60, 5))
  74.  
  75. # Finding the most proper value basic on coefficients
  76. # (fftamp*bpm)/freq
  77. coefficients = {}
  78. for i in range(len(bpms)):
  79.     if freqs[i] >= 1:
  80.         coefficients[((fftvalues[i]*bpms[i])/freqs[i])] = bpms[i]
  81. maxc = max(coefficients.keys())
  82. print('Pulse that programme found matching is : ' + str(coefficients[maxc]) +'bpm')
  83.  
  84. # Output - write results to text file
  85. file = open('bpms.txt', 'w')
  86. file.write('FFT pos         Freq[Hz]            FFT Amp                   BPM\n')
  87. for i in range(len(freqs)-1):
  88.     file.write(str(fftpos[i]) + '               ' + str(freqs[i]) + '             '
  89.                + str(fftvalues[i])+'               ' + str(bpms[i])+'\n')
  90. try:
  91.     file.close()
  92.     print('File cotaining the results saved successfully')
  93. except:
  94.     print('An error has occured during saving a file')
  95.  
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement