Spaces:
Sleeping
Sleeping
키 프레임 추출
Browse files- detector/frame_sampler.py +21 -0
detector/frame_sampler.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def extract_keyframes(video, output_dir, target_frames=50):
|
| 5 |
+
cap = cv2.VideoCapture(video)
|
| 6 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 7 |
+
frame_step = max(1, total_frames // target_frames)
|
| 8 |
+
|
| 9 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 10 |
+
keyframes = []
|
| 11 |
+
|
| 12 |
+
for idx in range(0, total_frames, frame_step):
|
| 13 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
|
| 14 |
+
ret, frame = cap.read()
|
| 15 |
+
if ret:
|
| 16 |
+
output_path = os.path.join(output_dir, f"frame_{idx:05d}.png")
|
| 17 |
+
cv2.imwrite(output_path, frame)
|
| 18 |
+
keyframes.append(output_path)
|
| 19 |
+
|
| 20 |
+
cap.release()
|
| 21 |
+
return keyframes
|