diff --git a/docs/_static/img/video_to_bw.webm b/docs/_static/img/video_to_bw.webm new file mode 100644 index 000000000..12c188aad Binary files /dev/null and b/docs/_static/img/video_to_bw.webm differ diff --git a/simba/ui/pop_ups/video_processing_pop_up.py b/simba/ui/pop_ups/video_processing_pop_up.py index 76d3a01a1..8fca41880 100644 --- a/simba/ui/pop_ups/video_processing_pop_up.py +++ b/simba/ui/pop_ups/video_processing_pop_up.py @@ -2081,7 +2081,7 @@ def __init__(self): def run_video(self): video_path = self.selected_video.file_path check_file_exist_and_readable(file_path=video_path) - self.brightness, self.contrast = brightness_contrast_ui(video_path=video_path) + self.brightness, self.contrast = brightness_contrast_ui(data=video_path) self.video_paths = [video_path] self.apply() @@ -2089,7 +2089,7 @@ def run_directory(self): video_dir = self.selected_dir.folder_path check_if_dir_exists(in_dir=video_dir, source=self.__class__.__name__) self.video_paths = find_files_of_filetypes_in_directory(directory=video_dir, extensions=Options.ALL_VIDEO_FORMAT_OPTIONS.value, raise_error=True) - self.brightness, self.contrast = brightness_contrast_ui(video_path=self.video_paths[0]) + self.brightness, self.contrast = brightness_contrast_ui(data=self.video_paths[0]) self.apply() def apply(self): @@ -2106,6 +2106,7 @@ def apply(self): timer.stop_timer() stdout_success(f'{len(self.video_paths)} video(s) converted.', elapsed_time=timer.elapsed_time_str) +#BrightnessContrastPopUp() class InteractiveClahePopUp(PopUpMixin): """ diff --git a/simba/video_processors/video_processing.py b/simba/video_processors/video_processing.py index c3b29eb35..047b2e288 100644 --- a/simba/video_processors/video_processing.py +++ b/simba/video_processors/video_processing.py @@ -3445,6 +3445,35 @@ def superimpose_elapsed_time(video_path: Union[str, os.PathLike], timer.stop_timer() stdout_success(msg=f'Super-imposed time on {len(video_paths)} video(s), saved in {save_dir}', elapsed_time=timer.elapsed_time_str) + +def video_to_bw(video_path: Union[str, os.PathLike], + threshold: Optional[float] = 0.5) -> None: + """ + Convert video to black and white using passed threshold. + + .. video:: _static/img/video_to_bw.webm + :loop: + + :param Union[str, os.PathLike] video_path: Path to the video + :param Optional[float] threshold: Value between 0 and 1. Lower values gives more white and vice versa. + :return: None. + + :example: + >>> video_to_bw(video_path='/Users/simon/Downloads/1_LH_clipped_cropped_eq_20240515135926.mp4', threshold=0.02) + """ + + check_float(name=f'{video_to_bw.__name__} threshold', value=threshold, min_value=0, max_value=1.0) + threshold = int(255 * threshold) + check_ffmpeg_available(raise_error=True) + timer = SimbaTimer(start=True) + _ = get_video_meta_data(video_path=video_path) + dir, video_name, ext = get_fn_ext(video_path) + save_path = os.path.join(dir, f'{video_name}_bw{ext}') + cmd = f"ffmpeg -i '{video_path}' -vf \"format=gray,geq=lum_expr='if(lt(lum(X,Y),{threshold}),0,255)'\" -pix_fmt yuv420p '{save_path}' -y" + subprocess.call(cmd, shell=True, stdout=subprocess.PIPE) + timer.stop_timer() + stdout_success(msg=f'Video {video_name} converted.', elapsed_time=timer.elapsed_time_str) + # video_paths = ['/Users/simon/Desktop/envs/simba/troubleshooting/beepboop174/project_folder/merge/Trial 10_clipped_gantt.mp4', # '/Users/simon/Desktop/envs/simba/troubleshooting/beepboop174/project_folder/merge/Trial 10_clipped.mp4', # '/Users/simon/Desktop/envs/simba/troubleshooting/beepboop174/project_folder/merge/Trial 10_clipped_line.mp4',