From 655d9529267fdbc004ae142be9389b58b4520133 Mon Sep 17 00:00:00 2001 From: davidpagnon Date: Sat, 31 Aug 2024 23:31:28 +0200 Subject: [PATCH] fps not always retrieved successfully on Linux -> set to 30 if needed --- Sports2D/Sports2D.py | 13 ++++++------- Sports2D/process.py | 4 ++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Sports2D/Sports2D.py b/Sports2D/Sports2D.py index 2309229..aebc98f 100644 --- a/Sports2D/Sports2D.py +++ b/Sports2D/Sports2D.py @@ -270,15 +270,14 @@ def base_params(config_dict): # frame_rates frame_rates = [] for video_file in video_files: - print(str(video_dir / video_file)) video = cv2.VideoCapture(str(video_dir / video_file)) + if not video.isOpened(): + raise FileNotFoundError(f'Error: Could not open {video_dir/video_file}. Check that the file exists.') frame_rate = video.get(cv2.CAP_PROP_FPS) - try: - 1/frame_rate - frame_rates.append(frame_rate) - except ZeroDivisionError: - print('Frame rate could not be retrieved: check that your video exists at the correct path') - raise + if frame_rate == 0: + frame_rate = 30 + logging.warning(f'Error: Could not retrieve frame rate from {video_dir/video_file}. Defaulting to 30fps.') + frame_rates.append(frame_rate) video.release() # time_ranges diff --git a/Sports2D/process.py b/Sports2D/process.py index 8a391d7..f3db0da 100644 --- a/Sports2D/process.py +++ b/Sports2D/process.py @@ -160,6 +160,8 @@ def setup_webcam(webcam_id, save_vid, vid_output_path, input_size): out_vid = None if save_vid: fps = cap.get(cv2.CAP_PROP_FPS) + if frame_rate == 0: + frame_rate = 30 # fourcc MJPG produces very large files but is faster. If it is too slow, consider using it and then converting the video to h264 # try: # fourcc = cv2.VideoWriter_fourcc(*'avc1') # =h264. better compression and quality but may fail on some systems @@ -206,6 +208,8 @@ def setup_video(video_file_path, save_vid, vid_output_path): out_vid = None if save_vid: fps = cap.get(cv2.CAP_PROP_FPS) + if frame_rate == 0: + frame_rate = 30 # try: # fourcc = cv2.VideoWriter_fourcc(*'avc1') # =h264. better compression and quality but may fail on some systems # out_vid = cv2.VideoWriter(vid_output_path, fourcc, fps, (cam_width, cam_height))