You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from collections import deque
  2. import threading
  3. import time
  4. import cv2
  5. import pyramids
  6. import heartrate
  7. import preprocessing
  8. import eulerian
  9. import numpy as np
  10. class main():
  11. def __init__(self):
  12. # Frequency range for Fast-Fourier Transform
  13. self.freq_min = 1
  14. self.freq_max = 5
  15. self.BUFFER_LEN = 10
  16. self.BUFFER = deque(maxlen=self.BUFFER_LEN)
  17. self.FPS_BUFFER = deque(maxlen=self.BUFFER_LEN)
  18. self.buffer_lock = threading.Lock()
  19. self.FPS = []
  20. def video(self):
  21. cap = cv2.VideoCapture(0)
  22. while len(self.BUFFER) < self.BUFFER_LEN:
  23. start_time = time.time()
  24. ret, frame = cap.read()
  25. frame = cv2.resize(frame, (500, 500))
  26. self.BUFFER.append(frame)
  27. stop_time = time.time()
  28. self.FPS_BUFFER.append(stop_time-start_time)
  29. self.FPS = round(1 / np.mean(np.array(self.FPS_BUFFER)))
  30. print("Buffer ready")
  31. while True:
  32. start_time = time.time()
  33. ret, frame = cap.read()
  34. frame = cv2.resize(frame, (500, 500))
  35. self.BUFFER.append(frame)
  36. stop_time = time.time()
  37. self.FPS_BUFFER.append(stop_time-start_time)
  38. #threading.Event().wait(0.02)
  39. self.FPS = round(1 / np.mean(np.array(self.FPS_BUFFER)))
  40. def processing(self):
  41. # Build Laplacian video pyramid
  42. while True:
  43. with self.buffer_lock:
  44. PROCESS_BUFFER = np.array(self.BUFFER)
  45. lap_video = pyramids.build_video_pyramid(PROCESS_BUFFER)
  46. amplified_video_pyramid = []
  47. for i, video in enumerate(lap_video):
  48. if i == 0 or i == len(lap_video)-1:
  49. continue
  50. # Eulerian magnification with temporal FFT filtering
  51. result, fft, frequencies = eulerian.fft_filter(video, self.freq_min, self.freq_max, self.FPS)
  52. lap_video[i] += result
  53. # Calculate heart rate
  54. heart_rate = heartrate.find_heart_rate(fft, frequencies, self.freq_min, self.freq_max)
  55. # Collapse laplacian pyramid to generate final video
  56. #amplified_frames = pyramids.collapse_laplacian_video_pyramid(lap_video, len(self.BUFFER))
  57. # Output heart rate and final video
  58. print("Heart rate: ", heart_rate, "bpm")
  59. threading.Event().wait(2)
  60. if __name__ == '__main__':
  61. MAIN = main()
  62. video_thread = threading.Thread(target=MAIN.video)
  63. processing_thread = threading.Thread(target=MAIN.processing)
  64. # Starte die Threads
  65. video_thread.start()
  66. time.sleep(2)
  67. print("__SYNCING___")
  68. processing_thread.start()