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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. print("VideoCapture")
  23. while len(self.BUFFER) < self.BUFFER_LEN:
  24. start_time = time.time()
  25. ret, frame = cap.read()
  26. frame = cv2.resize(frame, (500, 500))
  27. # why resize if we later call a pyramid?
  28. self.BUFFER.append(frame)
  29. stop_time = time.time()
  30. self.FPS_BUFFER.append(stop_time-start_time)
  31. self.FPS = round(1 / np.mean(np.array(self.FPS_BUFFER)))
  32. print("Buffer ready")
  33. while True:
  34. start_time = time.time()
  35. ret, frame = cap.read()
  36. frame = cv2.resize(frame, (500, 500))
  37. self.BUFFER.append(frame)
  38. stop_time = time.time()
  39. self.FPS_BUFFER.append(stop_time-start_time)
  40. #threading.Event().wait(0.02)
  41. self.FPS = round(1 / np.mean(np.array(self.FPS_BUFFER)))
  42. def processing(self):
  43. # Build Laplacian video pyramid
  44. while True:
  45. with self.buffer_lock:
  46. PROCESS_BUFFER = np.array(self.BUFFER)
  47. lap_video = pyramids.build_video_pyramid(PROCESS_BUFFER)
  48. amplified_video_pyramid = []
  49. for i, video in enumerate(lap_video):
  50. if i == 0 or i == len(lap_video)-1:
  51. continue
  52. # Eulerian magnification with temporal FFT filtering
  53. result, fft, frequencies = eulerian.fft_filter(video, self.freq_min, self.freq_max, self.FPS)
  54. lap_video[i] += result
  55. # Calculate heart rate
  56. heart_rate = heartrate.find_heart_rate(fft, frequencies, self.freq_min, self.freq_max)
  57. # Collapse laplacian pyramid to generate final video
  58. #amplified_frames = pyramids.collapse_laplacian_video_pyramid(lap_video, len(self.BUFFER))
  59. # Output heart rate and final video
  60. print("Heart rate: ", heart_rate, "bpm")
  61. threading.Event().wait(2)
  62. if __name__ == '__main__':
  63. MAIN = main()
  64. video_thread = threading.Thread(target=MAIN.video)
  65. processing_thread = threading.Thread(target=MAIN.processing)
  66. # Starte die Threads
  67. video_thread.start()
  68. time.sleep(2)
  69. print("__SYNCING___")
  70. processing_thread.start()