Skip to content

Commit

Permalink
seekability
Browse files Browse the repository at this point in the history
  • Loading branch information
sronilsson committed Dec 29, 2024
1 parent 1a3744f commit 49affe4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build/
*.whl

# dev
simba/sandbox/
simba/sandbox

#local docs build
_build/html/
Expand Down
16 changes: 9 additions & 7 deletions simba/ui/pop_ups/check_videos_seekable_pop_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from simba.utils.checks import check_if_dir_exists, is_valid_video_file
from simba.utils.enums import Formats, Options
from simba.utils.read_write import (find_files_of_filetypes_in_directory,
get_desktop_path)
get_desktop_path, get_downloads_path)
from simba.video_processors.video_processing import is_video_seekable


Expand All @@ -26,8 +26,8 @@ def __init__(self):
batch_size_options = list(range(100, 5100, 100))
batch_size_options.insert(0, 'NONE')
self.use_gpu_cb, self.use_gpu_var = SimbaCheckbox(parent=settings_frm, txt="Use GPU (reduced runtime)", txt_img='gpu_2')
self.batch_size_dropdown = DropDownMenu(settings_frm, "FRAME BATCH SIZE:", batch_size_options, "15")
self.batch_size_dropdown.setChoices('NONE')
self.batch_size_dropdown = DropDownMenu(settings_frm, "FRAME BATCH SIZE:", batch_size_options, "25")
self.batch_size_dropdown.setChoices(400)
single_video_frm = LabelFrame(self.main_frm, text="SINGLE VIDEO", font=Formats.FONT_HEADER.value)
self.single_video_path = FileSelect(single_video_frm, "VIDEO PATH:", title="Select a video file", lblwidth=25, file_types=[("VIDEO", Options.ALL_VIDEO_FORMAT_STR_OPTIONS.value)])
single_run_btn = SimbaButton(parent=single_video_frm, txt="RUN", img='rocket', font=Formats.FONT_REGULAR.value, cmd=self.run, cmd_kwargs={'directory': lambda: False})
Expand All @@ -49,7 +49,7 @@ def __init__(self):
multiple_video_frm.grid(row=2, column=0, sticky=NW)
self.directory_path.grid(row=0, column=0, sticky=NW)
dir_run_btn.grid(row=1, column=0, sticky=NW)
#self.main_frm.mainloop()
self.main_frm.mainloop()

def run(self, directory: bool):
if directory:
Expand All @@ -67,13 +67,15 @@ def run(self, directory: bool):
batch_size = None
else:
batch_size = int(batch_size)
desktop_path = get_desktop_path()
save_path = os.path.join(desktop_path, f'seekability_test_{datetime.now().strftime("%Y%m%d%H%M%S")}.csv')
save_dir = get_desktop_path()
if save_dir is None:
save_dir = get_downloads_path(raise_error=True)
save_path = os.path.join(save_dir, f'seekability_test_{datetime.now().strftime("%Y%m%d%H%M%S")}.csv')
_ = is_video_seekable(data_path=data_path,
gpu=gpu,
batch_size=batch_size,
verbose=False,
save_path=save_path)


#CheckVideoSeekablePopUp()
CheckVideoSeekablePopUp()
24 changes: 20 additions & 4 deletions simba/utils/read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -2617,12 +2617,28 @@ def create_empty_xlsx_file(xlsx_path: Union[str, os.PathLike]):

def get_desktop_path(raise_error: bool = False):
""" Get the path to the user desktop directory """
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
if not os.path.isdir(desktop_path):
desktop_path_option_1 = os.path.join(os.path.expanduser("~"), "Desktop")
if not os.path.isdir(desktop_path_option_1):
desktop_path_option_2 = os.path.join(os.path.expanduser("~"), "OneDrive", "Desktop")
if os.path.isdir(desktop_path_option_2):
return desktop_path_option_2
else:
if raise_error:
raise InvalidFilepathError(msg=f'{desktop_path_option_1} OR {desktop_path_option_2} are not valid directories')
else:
return None
else:
return desktop_path_option_1

def get_downloads_path(raise_error: bool = False):
""" Get the path to the user downloads directory """
downloads_path = os.path.join(os.path.expanduser("~"), "Downloads")
if not os.path.isdir(downloads_path):
if raise_error:
raise InvalidFilepathError(msg=f'{desktop_path} is not a valid directory')
raise InvalidFilepathError(msg=f'{downloads_path} is not a valid directory')
else:
return None
else:
return desktop_path
return downloads_path


14 changes: 8 additions & 6 deletions simba/video_processors/video_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4601,6 +4601,7 @@ def is_video_seekable(data_path: Union[str, os.PathLike],
>>> _ = is_video_seekable(data_path='/Users/simon/Desktop/unseekable/20200730_AB_7dpf_850nm_0003_fps_5.mp4', batch_size=400)
"""

timer = SimbaTimer(start=True)
if batch_size is not None:
check_int(name=f'{is_video_seekable.__name__}', value=batch_size, min_value=1)
if save_path is not None:
Expand All @@ -4623,30 +4624,31 @@ def is_video_seekable(data_path: Union[str, os.PathLike],
_, video_name, _ = get_fn_ext(filepath=file_path)
print(f'Checking seekability video {video_name}...')
video_meta_data = get_video_meta_data(video_path=file_path)
video_frm_ranges = np.arange(0, video_meta_data['frame_count']+1)
video_frm_ranges = np.arange(0, video_meta_data['frame_count'])
if batch_size is not None:
video_frm_ranges = np.array_split(video_frm_ranges, max(1, int(video_frm_ranges.shape[0]/batch_size)))
else:
video_frm_ranges = [video_frm_ranges]
video_error_frms = []
for video_frm_range in video_frm_ranges:
print(f'Processing frame {video_frm_range[0]} to {video_frm_range[-1]} (video: {video_name})...')
if not gpu:
imgs = ImageMixin.read_img_batch_from_video(video_path=file_path, start_frm=video_frm_range[0], end_frm=video_frm_range[-1], verbose=verbose)
imgs = ImageMixin.read_img_batch_from_video(video_path=file_path, start_frm=video_frm_range[0], end_frm=video_frm_range[-1], verbose=False)
else:
imgs = read_img_batch_from_video_gpu(video_path=file_path, start_frm=video_frm_range[0], end_frm=video_frm_range[-1], verbose=verbose)
imgs = read_img_batch_from_video_gpu(video_path=file_path, start_frm=video_frm_range[0], end_frm=video_frm_range[-1], verbose=False)
invalid_frms = [k for k, v in imgs.items() if v is None]
video_error_frms.extend(invalid_frms)
results[video_name] = video_error_frms

timer.stop_timer()
if all(len(v) == 0 for v in results.values()):
if verbose:
stdout_success(msg=f'The {len(data_paths)} videos are valid.', source=is_video_seekable.__name__)
stdout_success(msg=f'The {len(data_paths)} videos are valid.', source=is_video_seekable.__name__, elapsed_time=timer.elapsed_time_str)
return True
else:
if save_path is not None:
out_df = pd.DataFrame.from_dict(data=results).T
out_df.to_csv(save_path)
FrameRangeWarning(msg=f'Some videos have unseekable frames. See {save_path} for results', source=is_video_seekable.__name__)
FrameRangeWarning(msg=f'Some videos have unseekable frames. See {save_path} for results. Elapsed time: {timer.elapsed_time_str}s', source=is_video_seekable.__name__)
if raise_error:
raise FrameRangeError(msg=f'{results} The frames in the videos listed are unreadable. Consider re-encoding these videos.', source=is_video_seekable.__name__)
else:
Expand Down

0 comments on commit 49affe4

Please sign in to comment.