Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with pytest.raises(TypeError):
FrameTimecode(timecode=None, fps=FrameTimecode(timecode=0, fps=None))
# Test zero FPS/negative.
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=0)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-1)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-100)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=0.0)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-1.0)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-1000.0)
with pytest.raises(ValueError):
FrameTimecode(timecode=0, fps=MINIMUM_FRAMES_PER_SECOND_FLOAT / 2)
# Test positive framerates.
assert FrameTimecode(timecode=0, fps=1).frame_num == 0
assert FrameTimecode(timecode=0, fps=MINIMUM_FRAMES_PER_SECOND_FLOAT).frame_num == 0
assert FrameTimecode(timecode=0, fps=10).frame_num == 0
assert FrameTimecode(timecode=0, fps=MINIMUM_FRAMES_PER_SECOND_FLOAT * 2).frame_num == 0
assert FrameTimecode(timecode=0, fps=1000).frame_num == 0
assert FrameTimecode(timecode=0, fps=1000.0).frame_num == 0
def test_framerate():
''' Test FrameTimecode constructor argument "fps". '''
# Not passing fps results in TypeError.
with pytest.raises(TypeError): FrameTimecode()
with pytest.raises(TypeError): FrameTimecode(timecode=0, fps=None)
with pytest.raises(TypeError):
FrameTimecode(timecode=None, fps=FrameTimecode(timecode=0, fps=None))
# Test zero FPS/negative.
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=0)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-1)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-100)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=0.0)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-1.0)
with pytest.raises(ValueError): FrameTimecode(timecode=0, fps=-1000.0)
with pytest.raises(ValueError):
FrameTimecode(timecode=0, fps=MINIMUM_FRAMES_PER_SECOND_FLOAT / 2)
# Test positive framerates.
assert FrameTimecode(timecode=0, fps=1).frame_num == 0
assert FrameTimecode(timecode=0, fps=MINIMUM_FRAMES_PER_SECOND_FLOAT).frame_num == 0
assert FrameTimecode(timecode=0, fps=10).frame_num == 0
assert FrameTimecode(timecode=0, fps=MINIMUM_FRAMES_PER_SECOND_FLOAT * 2).frame_num == 0
assert FrameTimecode(timecode=0, fps=1000).frame_num == 0
assert FrameTimecode(timecode=0, fps=1000.0).frame_num == 0
def validate_capture_parameters(video_names, cap_frame_sizes, check_framerate=False,
cap_framerates=None):
# type: (List[Tuple[str, str]], List[Tuple[int, int]], Optional[bool],
# Optional[List[float]]) -> None
""" Validate Capture Parameters: Ensures that all passed capture frame sizes and (optionally)
framerates are equal. Raises VideoParameterMismatch if there is a mismatch.
Raises:
VideoParameterMismatch
"""
bad_params = []
max_framerate_delta = scenedetect.frame_timecode.MINIMUM_FRAMES_PER_SECOND_FLOAT
# Check heights/widths match.
bad_params += [(cv2.CAP_PROP_FRAME_WIDTH, frame_size[0],
cap_frame_sizes[0][0], video_names[i][0], video_names[i][1]) for
i, frame_size in enumerate(cap_frame_sizes)
if abs(frame_size[0] - cap_frame_sizes[0][0]) > 0]
bad_params += [(cv2.CAP_PROP_FRAME_HEIGHT, frame_size[1],
cap_frame_sizes[0][1], video_names[i][0], video_names[i][1]) for
i, frame_size in enumerate(cap_frame_sizes)
if abs(frame_size[1] - cap_frame_sizes[0][1]) > 0]
# Check framerates if required.
if check_framerate:
bad_params += [(cv2.CAP_PROP_FPS, fps, cap_framerates[0], video_names[i][0],
video_names[i][1]) for i, fps in enumerate(cap_framerates)
if math.fabs(fps - cap_framerates[0]) > max_framerate_delta]
if bad_params:
"""
csv_reader = get_csv_reader(csv_file)
num_cols = None
num_metrics = None
num_frames = None
# First row: Framerate, [video_framerate]
try:
row = next(csv_reader)
except StopIteration:
# If the file is blank or we couldn't decode anything, assume the file was empty.
return num_frames
# First Row (FPS = [...]) and ensure framerate equals base_timecode if set.
if not len(row) == 2 or not row[0] == COLUMN_NAME_FPS:
raise StatsFileCorrupt()
stats_file_framerate = float(row[1])
if stats_file_framerate < MINIMUM_FRAMES_PER_SECOND_FLOAT:
raise StatsFileCorrupt("Invalid framerate detected in CSV stats file "
"(decoded FPS: %f)." % stats_file_framerate)
if base_timecode is not None and not base_timecode.equal_framerate(stats_file_framerate):
raise StatsFileFramerateMismatch(base_timecode.get_framerate(), stats_file_framerate)
# Second Row: Frame Num, Timecode, [metrics...]
try:
row = next(csv_reader)
except StopIteration:
raise StatsFileCorrupt("Header row(s) missing.")
if not row or not len(row) >= 2:
raise StatsFileCorrupt()
if row[0] != COLUMN_NAME_FRAME_NUMBER or row[1] != COLUMN_NAME_TIMECODE:
raise StatsFileCorrupt()
num_cols = len(row)
num_metrics = num_cols - 2
if not num_metrics > 0:
and `framerate` was not set.
"""
check_framerate = True
cap_framerate = cap_framerates[0]
if framerate is not None:
if isinstance(framerate, float):
if framerate < scenedetect.frame_timecode.MINIMUM_FRAMES_PER_SECOND_FLOAT:
raise ValueError("Invalid framerate (must be a positive non-zero value).")
cap_framerate = framerate
check_framerate = False
else:
raise TypeError("Expected float for framerate, got %s." % type(framerate).__name__)
else:
unavailable_framerates = [(video_names[i][0], video_names[i][1]) for
i, fps in enumerate(cap_framerates) if fps <
scenedetect.frame_timecode.MINIMUM_FRAMES_PER_SECOND_FLOAT]
if unavailable_framerates:
raise VideoFramerateUnavailable(unavailable_framerates)
return (cap_framerate, check_framerate)
def validate_capture_framerate(video_names, cap_framerates, framerate=None):
# type: (List[Tuple[str, str]], List[float], Optional[float]) -> Tuple[float, bool]
""" Validate Capture Framerate: Ensures that the passed capture framerates are valid and equal.
Raises:
ValueError: Invalid framerate (must be positive non-zero value).
TypeError: Framerate must be of type float.
VideoFramerateUnavailable: Framerate for video could not be obtained,
and `framerate` was not set.
"""
check_framerate = True
cap_framerate = cap_framerates[0]
if framerate is not None:
if isinstance(framerate, float):
if framerate < scenedetect.frame_timecode.MINIMUM_FRAMES_PER_SECOND_FLOAT:
raise ValueError("Invalid framerate (must be a positive non-zero value).")
cap_framerate = framerate
check_framerate = False
else:
raise TypeError("Expected float for framerate, got %s." % type(framerate).__name__)
else:
unavailable_framerates = [(video_names[i][0], video_names[i][1]) for
i, fps in enumerate(cap_framerates) if fps <
scenedetect.frame_timecode.MINIMUM_FRAMES_PER_SECOND_FLOAT]
if unavailable_framerates:
raise VideoFramerateUnavailable(unavailable_framerates)
return (cap_framerate, check_framerate)