| import os, cv2 |
| import torch |
| from pathlib import Path |
| from multiprocessing import freeze_support |
|
|
| def extract_frames(input_video_path, output_imgs_path): |
| |
| vidcap = cv2.VideoCapture(input_video_path) |
| |
| |
| frame_count = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| |
| |
| os.makedirs(output_imgs_path, exist_ok=True) |
| |
| |
| for i in range(frame_count): |
| success, image = vidcap.read() |
| if success: |
| cv2.imwrite(os.path.join(output_imgs_path, f"frame{i}.png"), image) |
| print(f"{frame_count} frames extracted and saved to {output_imgs_path}") |
| |
| def video2humanmasks(input_frames_path, output_folder_path, output_type, fps): |
| |
| freeze_support() |
| |
| |
| if not os.path.exists(input_frames_path) or not os.path.isdir(input_frames_path): |
| raise ValueError("Invalid input path: {}".format(input_frames_path)) |
| |
| |
| if not os.path.exists(output_folder_path) or not os.path.isdir(output_folder_path): |
| raise ValueError("Invalid output path: {}".format(output_folder_path)) |
| |
| |
| valid_output_types = ["video", "pngs", "both"] |
| if output_type.lower() not in valid_output_types: |
| raise ValueError("Invalid output type: {}. Must be one of {}".format(output_type, valid_output_types)) |
| |
| |
| predicted_torch_model_cache_path = os.path.join(Path.home(), ".cache", "torch", "hub", "hithereai_RobustVideoMatting_master") |
| predicted_rvm_cache_testilfe = os.path.join(predicted_torch_model_cache_path, "hubconf.py") |
|
|
| |
| try: |
| |
| convert_video = torch.hub.load(predicted_torch_model_cache_path, "converter", source='local') |
| model = torch.hub.load(predicted_torch_model_cache_path, "resnet50", source='local').cuda() |
| except: |
| |
| convert_video = torch.hub.load("hithereai/RobustVideoMatting", "converter") |
| model = torch.hub.load("hithereai/RobustVideoMatting", "resnet50").cuda() |
| |
| output_alpha_vid_path = os.path.join(output_folder_path, "human_masked_video.mp4") |
| |
| |
| convert_video( |
| model, |
| input_source=input_frames_path, |
| output_type='video' if output_type.upper() in ("VIDEO", "BOTH") else 'png_sequence', |
| output_alpha=output_alpha_vid_path if output_type.upper() in ("VIDEO", "BOTH") else output_folder_path, |
| output_video_mbps=4, |
| output_video_fps=fps, |
| downsample_ratio=None, |
| seq_chunk=12, |
| progress=True |
| ) |
|
|
| if output_type.lower() == "both": |
| extract_frames(output_alpha_vid_path, output_folder_path) |
|
|