Skip to content

Commit

Permalink
fps not always retrieved successfully on Linux -> set to 30 if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpagnon committed Aug 31, 2024
1 parent ed6c50b commit 655d952
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Sports2D/Sports2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Sports2D/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 655d952

Please sign in to comment.