Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# File 3: Wrong Header Names [StatsFileCorrupt]
stats_writers[3].writerow([COLUMN_NAME_FPS, '%.10f' % base_timecode.get_framerate()])
stats_writers[3].writerow(
[COLUMN_NAME_TIMECODE, COLUMN_NAME_FRAME_NUMBER, some_metric_key])
stats_writers[3].writerow(
[some_frame_key, some_frame_timecode.get_timecode(), some_metric_value])
for stats_file in stats_files: stats_file.close()
stats_files = [open(stats_file, 'rt') for stats_file in TEST_STATS_FILES]
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[0], base_timecode)
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[1], base_timecode)
with pytest.raises(StatsFileFramerateMismatch):
stats_manager.load_from_csv(stats_files[2], base_timecode)
with pytest.raises(StatsFileCorrupt):
stats_manager.load_from_csv(stats_files[3], base_timecode)
finally:
for stats_file in stats_files: stats_file.close()
for stats_file in TEST_STATS_FILES: os.remove(stats_file)
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:
raise StatsFileCorrupt('No metrics defined in CSV file.')
metric_keys = row[2:]
num_frames = 0
for row in csv_reader:
os.path.basename(self.stats_file_path))
try:
with open(self.stats_file_path, 'rt') as stats_file:
self.stats_manager.load_from_csv(stats_file, self.base_timecode)
except StatsFileCorrupt:
error_strs = [
'Could not load stats file.', 'Failed to parse stats file:',
'Could not load frame metrics from stats file - file is corrupt or not a'
' valid PySceneDetect stats file. If the file exists, ensure that it is'
' a valid stats file CSV, otherwise delete it and run PySceneDetect again'
' to re-generate the stats file.']
logging.error('\n'.join(error_strs))
raise click.BadParameter(
'\n Could not load given stats file, see above output for details.',
param_hint='input stats file')
except StatsFileFramerateMismatch as ex:
error_strs = [
'could not load stats file.', 'Failed to parse stats file:',
'Framerate differs between stats file (%.2f FPS) and input'
' video%s (%.2f FPS)' % (
ex.stats_file_fps,
's' if self.video_manager.get_num_videos() > 1 else '',
ex.base_timecode_fps),
'Ensure the correct stats file path was given, or delete and re-generate'
' the stats file.']
logging.error('\n'.join(error_strs))
raise click.BadParameter(
'framerate differs between given stats file and input video(s).',
param_hint='input stats file')
def __init__(self, base_timecode_fps, stats_file_fps, message=
"Framerate differs between stats file and base timecode."):
# type: (str, str)
# Pass message string to base Exception class.
super(StatsFileFramerateMismatch, self).__init__(message)
self.base_timecode_fps = base_timecode_fps
self.stats_file_fps = stats_file_fps
os.path.basename(self.stats_file_path))
try:
with open(self.stats_file_path, 'rt') as stats_file:
self.stats_manager.load_from_csv(stats_file, self.base_timecode)
except StatsFileCorrupt:
error_strs = [
'Could not load stats file.', 'Failed to parse stats file:',
'Could not load frame metrics from stats file - file is corrupt or not a'
' valid PySceneDetect stats file. If the file exists, ensure that it is'
' a valid stats file CSV, otherwise delete it and run PySceneDetect again'
' to re-generate the stats file.']
logging.error('\n'.join(error_strs))
raise click.BadParameter(
'\n Could not load given stats file, see above output for details.',
param_hint='input stats file')
except StatsFileFramerateMismatch as ex:
error_strs = [
'could not load stats file.', 'Failed to parse stats file:',
'Framerate differs between stats file (%.2f FPS) and input'
' video%s (%.2f FPS)' % (
ex.stats_file_fps,
's' if self.video_manager.get_num_videos() > 1 else '',
ex.base_timecode_fps),
'Ensure the correct stats file path was given, or delete and re-generate'
' the stats file.']
logging.error('\n'.join(error_strs))
raise click.BadParameter(
'framerate differs between given stats file and input video(s).',
param_hint='input stats file')