Download app.py from Fred808/CU2: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/Fred808/CU2/resolve/main/app.py
- Command line
-
hf download hf://spaces/Fred808/CU2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Fred808/CU2/resolve/main/app.py
1.37 kB
| import os | |
| from fastapi import FastAPI | |
| from fastapi.responses import FileResponse, JSONResponse | |
| from scripts.extract_frames import extract_frames | |
| from contextlib import asynccontextmanager | |
| FRAMES_DIR = "frames" | |
| VIDEO_DIR = "videos" | |
| async def lifespan(app: FastAPI): | |
| os.makedirs(FRAMES_DIR, exist_ok=True) | |
| # Step 1: Extract frames from each video | |
| for video_file in os.listdir(VIDEO_DIR): | |
| if video_file.lower().endswith(('.mp4', '.avi', '.mov')): | |
| video_path = os.path.join(VIDEO_DIR, video_file) | |
| extract_frames(video_path, FRAMES_DIR) | |
| print("✅ Frame extraction complete.") | |
| yield | |
| app = FastAPI(lifespan=lifespan) | |
| # Endpoint to list all available frames | |
| def list_frames(): | |
| try: | |
| frames = [f for f in os.listdir(FRAMES_DIR) if f.lower().endswith(('.png', '.jpg', '.jpeg'))] | |
| return JSONResponse(content={"frames": frames}) | |
| except Exception as e: | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |
| # Endpoint to download a specific frame | |
| def get_frame(filename: str): | |
| file_path = os.path.join(FRAMES_DIR, filename) | |
| if os.path.exists(file_path): | |
| return FileResponse(file_path, filename=filename) | |
| return JSONResponse(content={"error": "File not found"}, status_code=404) | |