Fred808 commited on
Commit
61dd50a
·
verified ·
1 Parent(s): 8fa8cd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -31
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
- ANNOTATIONS_DIR = "annotations"
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
- os.makedirs(cursor_dir, exist_ok=True)
20
- os.makedirs(ANNOTATIONS_DIR, exist_ok=True)
21
- # Step 1: Extract frames
22
- for video_file in os.listdir(video_dir):
23
  if video_file.lower().endswith(('.mp4', '.avi', '.mov')):
24
- extract_frames(os.path.join(video_dir, video_file), FRAMES_DIR)
25
- # Step 2: Track cursor
26
- track_cursor(FRAMES_DIR, cursor_dir, os.path.join(ANNOTATIONS_DIR, "cursor_positions.json"))
27
- # Step 3: Zip frames and annotations
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
- @app.get("/download/frames")
42
- def download_frames():
43
- return FileResponse(FRAMES_ZIP, filename=FRAMES_ZIP)
 
 
 
 
 
44
 
45
- @app.get("/download/annotations")
46
- def download_annotations():
47
- return FileResponse(ANNOTATIONS_ZIP, filename=ANNOTATIONS_ZIP)
 
 
 
 
 
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)