Download backend/video_processor.py from SuriRaja/IDSL_All: direct link, hf CLI and curl.
- Browser
- Download file 869 Bytes
-
https://huggingface.co/spaces/SuriRaja/IDSL_All/resolve/main/backend/video_processor.py
- Command line
-
hf download hf://spaces/SuriRaja/IDSL_All/backend/video_processor.py
-
curl -L -o video_processor.py https://huggingface.co/spaces/SuriRaja/IDSL_All/resolve/main/backend/video_processor.py
869 Bytes
| import cv2 | |
| import os | |
| def extract_frames(video_path, output_dir='frames', every_n_sec=1): | |
| os.makedirs(output_dir, exist_ok=True) | |
| video_name = os.path.splitext(os.path.basename(video_path))[0] | |
| save_path = os.path.join(output_dir, video_name) | |
| os.makedirs(save_path, exist_ok=True) | |
| cap = cv2.VideoCapture(video_path) | |
| if not cap.isOpened(): | |
| raise ValueError("Failed to open video.") | |
| fps = cap.get(cv2.CAP_PROP_FPS) | |
| interval = max(1, int(fps * every_n_sec)) | |
| frame_idx = 0 | |
| saved = 0 | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| if frame_idx % interval == 0: | |
| filename = os.path.join(save_path, f"frame_{saved:04d}.jpg") | |
| cv2.imwrite(filename, frame) | |
| saved += 1 | |
| frame_idx += 1 | |
| cap.release() | |
| return saved, save_path | |