Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 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 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
video_dir = "videos"
|
| 18 |
-
cursor_dir = "cursors"
|
| 19 |
-
os.makedirs(FRAMES_DIR, exist_ok=True)
|
| 20 |
-
os.makedirs(cursor_dir, exist_ok=True)
|
| 21 |
-
os.makedirs(ANNOTATIONS_DIR, exist_ok=True)
|
| 22 |
-
# Step 1: Extract frames
|
| 23 |
-
for video_file in os.listdir(video_dir):
|
| 24 |
-
if video_file.lower().endswith(('.mp4', '.avi', '.mov')):
|
| 25 |
-
extract_frames(os.path.join(video_dir, video_file), FRAMES_DIR)
|
| 26 |
-
# Step 2: Track cursor
|
| 27 |
-
track_cursor(FRAMES_DIR, cursor_dir, os.path.join(ANNOTATIONS_DIR, "cursor_positions.json"))
|
| 28 |
-
# Step 3: Zip frames and annotations
|
| 29 |
-
with zipfile.ZipFile(FRAMES_ZIP, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 30 |
-
for root, _, files in os.walk(FRAMES_DIR):
|
| 31 |
-
for file in files:
|
| 32 |
-
zipf.write(os.path.join(root, file), arcname=file)
|
| 33 |
-
with zipfile.ZipFile(ANNOTATIONS_ZIP, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 34 |
-
for root, _, files in os.walk(ANNOTATIONS_DIR):
|
| 35 |
-
for file in files:
|
| 36 |
-
zipf.write(os.path.join(root, file), arcname=file)
|
| 37 |
-
print("Pipeline complete and outputs zipped.")
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
ANNOTATIONS_DIR = "annotations"
|
| 11 |
+
FRAMES_ZIP = "frames.zip"
|
| 12 |
+
ANNOTATIONS_ZIP = "annotations.zip"
|
| 13 |
+
|
| 14 |
+
@asynccontextmanager
|
| 15 |
+
async def lifespan(app: FastAPI):
|
| 16 |
+
# --- This replaces the old @app.on_event("startup") ---
|
| 17 |
+
video_dir = "videos"
|
| 18 |
+
cursor_dir = "cursors"
|
| 19 |
+
os.makedirs(FRAMES_DIR, exist_ok=True)
|
| 20 |
+
os.makedirs(cursor_dir, exist_ok=True)
|
| 21 |
+
os.makedirs(ANNOTATIONS_DIR, exist_ok=True)
|
| 22 |
+
# Step 1: Extract frames
|
| 23 |
+
for video_file in os.listdir(video_dir):
|
| 24 |
+
if video_file.lower().endswith(('.mp4', '.avi', '.mov')):
|
| 25 |
+
extract_frames(os.path.join(video_dir, video_file), FRAMES_DIR)
|
| 26 |
+
# Step 2: Track cursor
|
| 27 |
+
track_cursor(FRAMES_DIR, cursor_dir, os.path.join(ANNOTATIONS_DIR, "cursor_positions.json"))
|
| 28 |
+
# Step 3: Zip frames and annotations
|
| 29 |
+
with zipfile.ZipFile(FRAMES_ZIP, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 30 |
+
for root, _, files in os.walk(FRAMES_DIR):
|
| 31 |
+
for file in files:
|
| 32 |
+
zipf.write(os.path.join(root, file), arcname=file)
|
| 33 |
+
with zipfile.ZipFile(ANNOTATIONS_ZIP, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 34 |
+
for root, _, files in os.walk(ANNOTATIONS_DIR):
|
| 35 |
+
for file in files:
|
| 36 |
+
zipf.write(os.path.join(root, file), arcname=file)
|
| 37 |
+
print("Pipeline complete and outputs zipped.")
|
| 38 |
+
yield # This is required for the lifespan context
|
| 39 |
+
|
| 40 |
+
app = FastAPI(lifespan=lifespan)
|
| 41 |
+
|
| 42 |
+
@app.get("/download/frames")
|
| 43 |
+
def download_frames():
|
| 44 |
+
return FileResponse(FRAMES_ZIP, filename=FRAMES_ZIP)
|
| 45 |
+
|
| 46 |
+
@app.get("/download/annotations")
|
| 47 |
+
def download_annotations():
|
| 48 |
+
return FileResponse(ANNOTATIONS_ZIP, filename=ANNOTATIONS_ZIP)
|