Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
-
import zipfile
|
| 3 |
from fastapi import FastAPI
|
| 4 |
-
from fastapi.responses import FileResponse
|
| 5 |
from scripts.extract_frames import extract_frames
|
| 6 |
-
from scripts.cursor_tracker import track_cursor
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
|
| 9 |
FRAMES_DIR = "frames"
|
| 10 |
-
|
| 11 |
-
FRAMES_ZIP = "frames.zip"
|
| 12 |
-
ANNOTATIONS_ZIP = "annotations.zip"
|
| 13 |
|
| 14 |
@asynccontextmanager
|
| 15 |
async def lifespan(app: FastAPI):
|
| 16 |
-
video_dir = "videos"
|
| 17 |
-
cursor_dir = "cursors"
|
| 18 |
os.makedirs(FRAMES_DIR, exist_ok=True)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
for video_file in os.listdir(video_dir):
|
| 23 |
if video_file.lower().endswith(('.mp4', '.avi', '.mov')):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
with zipfile.ZipFile(FRAMES_ZIP, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 29 |
-
for root, _, files in os.walk(FRAMES_DIR):
|
| 30 |
-
for file in files:
|
| 31 |
-
zipf.write(os.path.join(root, file), arcname=file)
|
| 32 |
-
with zipfile.ZipFile(ANNOTATIONS_ZIP, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 33 |
-
for root, _, files in os.walk(ANNOTATIONS_DIR):
|
| 34 |
-
for file in files:
|
| 35 |
-
zipf.write(os.path.join(root, file), arcname=file)
|
| 36 |
-
print("Pipeline complete and outputs zipped.")
|
| 37 |
yield
|
| 38 |
|
| 39 |
app = FastAPI(lifespan=lifespan)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 4 |
from scripts.extract_frames import extract_frames
|
|
|
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
|
| 7 |
FRAMES_DIR = "frames"
|
| 8 |
+
VIDEO_DIR = "videos"
|
|
|
|
|
|
|
| 9 |
|
| 10 |
@asynccontextmanager
|
| 11 |
async def lifespan(app: FastAPI):
|
|
|
|
|
|
|
| 12 |
os.makedirs(FRAMES_DIR, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
# Step 1: Extract frames from each video
|
| 15 |
+
for video_file in os.listdir(VIDEO_DIR):
|
|
|
|
| 16 |
if video_file.lower().endswith(('.mp4', '.avi', '.mov')):
|
| 17 |
+
video_path = os.path.join(VIDEO_DIR, video_file)
|
| 18 |
+
extract_frames(video_path, FRAMES_DIR)
|
| 19 |
+
|
| 20 |
+
print("✅ Frame extraction complete.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
yield
|
| 22 |
|
| 23 |
app = FastAPI(lifespan=lifespan)
|
| 24 |
|
| 25 |
+
# Endpoint to list all available frames
|
| 26 |
+
@app.get("/frames")
|
| 27 |
+
def list_frames():
|
| 28 |
+
try:
|
| 29 |
+
frames = [f for f in os.listdir(FRAMES_DIR) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 30 |
+
return JSONResponse(content={"frames": frames})
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 33 |
|
| 34 |
+
# Endpoint to download a specific frame
|
| 35 |
+
@app.get("/frames/{filename}")
|
| 36 |
+
def get_frame(filename: str):
|
| 37 |
+
file_path = os.path.join(FRAMES_DIR, filename)
|
| 38 |
+
if os.path.exists(file_path):
|
| 39 |
+
return FileResponse(file_path, filename=filename)
|
| 40 |
+
return JSONResponse(content={"error": "File not found"}, status_code=404)
|