2
0

test_video.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import unittest
  2. import time
  3. import os
  4. import shutil
  5. from glob import glob
  6. import cv2
  7. import numpy as np
  8. from device.video import VideoCaptureThread
  9. from device.trigger_box import TriggerNeuracle
  10. from device.data_client import NeuracleDataClient
  11. def get_video_length(file_path):
  12. cap = cv2.VideoCapture(file_path)
  13. if not cap.isOpened():
  14. return None
  15. # Get the frames per second (fps) and total number of frames
  16. fps = cap.get(cv2.CAP_PROP_FPS)
  17. total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
  18. # Calculate the duration of the video in seconds
  19. duration = total_frames / fps
  20. # Release the video capture object
  21. cap.release()
  22. return duration
  23. class TestVideo(unittest.TestCase):
  24. def test_video_recording(self):
  25. output_dir = './tests/data/video'
  26. video_cam = VideoCaptureThread(output_dir=output_dir, video_source=1)
  27. time.sleep(10)
  28. video_cam.close()
  29. # read video files
  30. file = glob(os.path.join(output_dir, '*.mp4'))[0]
  31. duration = get_video_length(file)
  32. self.assertTrue(isinstance(duration, float))
  33. self.assertTrue(duration > 0)
  34. shutil.rmtree(output_dir)
  35. def test_video_sync(self):
  36. output_dir = './tests/data/video'
  37. trigger = TriggerNeuracle()
  38. data_client = NeuracleDataClient(buffer_len=10.)
  39. video_cam = VideoCaptureThread(output_dir=output_dir, video_source=1, sync_device=trigger)
  40. time.sleep(5)
  41. video_cam.close()
  42. events = data_client.get_trial_data(clear=True)[1]
  43. self.assertEqual(len(events), 1)
  44. self.assertAlmostEqual(events[0, 2], 255)
  45. data_client.close()
  46. shutil.rmtree(output_dir)