Spaces:
Sleeping
Sleeping
Download app.py from 7pixels/VideoToImage: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/7pixels/VideoToImage/resolve/main/app.py
- Command line
-
hf download hf://spaces/7pixels/VideoToImage/app.py
-
curl -L -o app.py https://huggingface.co/spaces/7pixels/VideoToImage/resolve/main/app.py
1.04 kB
| import gradio as gr | |
| import cv2 | |
| import os | |
| def video_to_images(video_file, fps=1): | |
| video_path = video_file.name | |
| output_folder = "frames" | |
| os.makedirs(output_folder, exist_ok=True) | |
| cap = cv2.VideoCapture(video_path) | |
| total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| video_fps = cap.get(cv2.CAP_PROP_FPS) | |
| step = int(video_fps / fps) if fps > 0 else 1 | |
| frame_list = [] | |
| count = 0 | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| if count % step == 0: | |
| frame_filename = os.path.join(output_folder, f"frame_{count:04d}.png") | |
| cv2.imwrite(frame_filename, frame) | |
| frame_list.append(frame_filename) | |
| count += 1 | |
| cap.release() | |
| return frame_list | |
| iface = gr.Interface( | |
| fn=video_to_images, | |
| inputs=[gr.File(label="Upload Video"), gr.Slider(0.1, 5, value=1, step=0.1, label="Frames per second")], | |
| outputs=gr.Gallery(label="Extracted Frames", scale=4), | |
| title="Video to Images Converter" | |
| ) | |
| iface.launch() | |