Files
audio2motion/scripts/GRID/run-20-extract-frames.py
2022-01-21 17:42:41 +08:00

68 lines
2.0 KiB
Python

import cv2
import os, sys
import glob
import numpy as np
import time
import pdb
import argparse
from pathlib2 import Path
total_pid = ["s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10" ]
if __name__ == "__main__":
path_data = Path("/yrfs/lsen/DATA/GRID/processing")
v_ids = sorted(path_data.glob("*"), reverse=False)
print(f"Have {len(v_ids)} dir files, \n {v_ids[:10]}")
# for each speaker
for i, ipath_id in enumerate(v_ids):
id_name = ipath_id.stem
if id_name not in total_pid: continue
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):
# 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"))) > 10:
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