Spaces:
Sleeping
Sleeping
Download detector/frame_sampler.py from shin0624/Factory_Layout_Simulator: direct link, hf CLI and curl.
- Browser
- Download file 726 Bytes
-
https://huggingface.co/spaces/shin0624/Factory_Layout_Simulator/resolve/main/detector/frame_sampler.py
- Command line
-
hf download hf://spaces/shin0624/Factory_Layout_Simulator/detector/frame_sampler.py
-
curl -L -o frame_sampler.py https://huggingface.co/spaces/shin0624/Factory_Layout_Simulator/resolve/main/detector/frame_sampler.py
726 Bytes
| import cv2 | |
| import os | |
| def extract_keyframes(video, output_dir, target_frames=50): | |
| cap = cv2.VideoCapture(video) | |
| total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| if total_frames <= 0: | |
| cap.release() | |
| return [] | |
| frame_step = max(1, total_frames // target_frames) | |
| os.makedirs(output_dir, exist_ok=True) | |
| keyframes = [] | |
| for idx in range(0, total_frames, frame_step): | |
| cap.set(cv2.CAP_PROP_POS_FRAMES, idx) | |
| ret, frame = cap.read() | |
| if ret: | |
| output_path = os.path.join(output_dir, f"frame_{idx:05d}.png") | |
| cv2.imwrite(output_path, frame) | |
| keyframes.append(output_path) | |
| cap.release() | |
| return keyframes |