import cv2 import os, sys import glob import numpy as np import time import pdb import argparse from pathlib2 import Path if __name__ == "__main__": path_data = Path("/mnt/data2/DATA/processing") v_ids = sorted(path_data.glob("*"), reverse=True) print(f"Have {len(v_ids)} dir files, \n {v_ids[:10]}") # for each word for i, ipath_id in enumerate(v_ids): id_name = ipath_id.stem dir_video = path_data.joinpath(id_name) v_dir_videos = sorted(dir_video.glob("**/video/video_orignal.mp4")) print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:10]}") # for each video for iv, ipath_video in enumerate(v_dir_videos): ivideo_name = ipath_video.stem itrain_test = ipath_video.parent.name # create data path path_save_frames = ipath_video.parent.parent.joinpath("frames") # if already exists frames if path_save_frames.exists(): if len(list(path_save_frames.glob("*.jpg"))) > 20: continue path_save_frames.mkdir(parents=True, exist_ok=True) #print("save frames dir = ", {path_save_frames}) # STEP1: extract frames count = 0 cap = cv2.VideoCapture(str(ipath_video)) length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) while True: success, image = cap.read() if not success: break ipath_frame = path_save_frames.joinpath(f"frame-{count:04d}.jpg") if not ipath_frame.exists(): cv2.imwrite(str(ipath_frame), image) count += 1 if iv % 50 == 0: print(f"--> Extract {iv}-th video, have {length} frames, {ipath_video}") #print(f"extract and save the {count}/{length}-th frame --> {ipath_frame}") #break #break