Update download_api.py
Browse files- download_api.py +31 -34
download_api.py
CHANGED
|
@@ -5,7 +5,10 @@ import os
|
|
| 5 |
import threading
|
| 6 |
import requests
|
| 7 |
import json
|
| 8 |
-
from
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -13,47 +16,39 @@ app = FastAPI()
|
|
| 13 |
REMOTE_FRAMES_LIST_URL = "https://fred808-cu2.hf.space/frames"
|
| 14 |
REMOTE_FRAME_BASE_URL = "https://fred808-cu2.hf.space/frames"
|
| 15 |
|
| 16 |
-
LOCAL_FRAMES_DIR = Path("frames/blender_day1").resolve()
|
| 17 |
ANNOTATIONS_DIR = Path("annotations").resolve()
|
| 18 |
ANNOTATION_OUTPUT = ANNOTATIONS_DIR / "blender1.json"
|
| 19 |
CURSOR_TEMPLATES_DIR = Path("cursors").resolve()
|
| 20 |
|
| 21 |
-
# Ensure
|
| 22 |
-
LOCAL_FRAMES_DIR.mkdir(parents=True, exist_ok=True)
|
| 23 |
ANNOTATIONS_DIR.mkdir(parents=True, exist_ok=True)
|
| 24 |
CURSOR_TEMPLATES_DIR.mkdir(parents=True, exist_ok=True)
|
| 25 |
|
| 26 |
-
# Only run once per server start
|
| 27 |
-
cursor_tracking_started = False
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
"""
|
| 31 |
-
Fetch frame filenames from the remote /frames endpoint,
|
| 32 |
-
then download each one into the local frames directory.
|
| 33 |
-
"""
|
| 34 |
try:
|
| 35 |
-
print("[INFO] Fetching frame list
|
| 36 |
res = requests.get(REMOTE_FRAMES_LIST_URL)
|
| 37 |
res.raise_for_status()
|
| 38 |
-
|
| 39 |
frame_list = res.json().get("frames", [])
|
| 40 |
print(f"[INFO] Found {len(frame_list)} frames.")
|
| 41 |
|
|
|
|
| 42 |
for fname in frame_list:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
print("[✅] All frames downloaded successfully.")
|
| 53 |
-
return True
|
| 54 |
except Exception as e:
|
| 55 |
-
print(f"[ERROR] Failed to
|
| 56 |
-
return
|
| 57 |
|
| 58 |
def run_cursor_tracker_once():
|
| 59 |
global cursor_tracking_started
|
|
@@ -61,13 +56,17 @@ def run_cursor_tracker_once():
|
|
| 61 |
return
|
| 62 |
cursor_tracking_started = True
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
threshold=0.8,
|
| 70 |
-
start_frame=0,
|
| 71 |
email_results=False
|
| 72 |
)
|
| 73 |
print("[✅] Cursor tracking completed.")
|
|
@@ -76,9 +75,7 @@ def run_cursor_tracker_once():
|
|
| 76 |
|
| 77 |
@app.on_event("startup")
|
| 78 |
def startup_event():
|
| 79 |
-
|
| 80 |
-
if success:
|
| 81 |
-
threading.Thread(target=run_cursor_tracker_once, daemon=True).start()
|
| 82 |
|
| 83 |
@app.get("/download/{filename}")
|
| 84 |
def download_file(filename: str):
|
|
|
|
| 5 |
import threading
|
| 6 |
import requests
|
| 7 |
import json
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import numpy as np
|
| 11 |
+
from cursor_tracker import track_cursor_from_images # <-- newly added in your module
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 16 |
REMOTE_FRAMES_LIST_URL = "https://fred808-cu2.hf.space/frames"
|
| 17 |
REMOTE_FRAME_BASE_URL = "https://fred808-cu2.hf.space/frames"
|
| 18 |
|
|
|
|
| 19 |
ANNOTATIONS_DIR = Path("annotations").resolve()
|
| 20 |
ANNOTATION_OUTPUT = ANNOTATIONS_DIR / "blender1.json"
|
| 21 |
CURSOR_TEMPLATES_DIR = Path("cursors").resolve()
|
| 22 |
|
| 23 |
+
# Ensure necessary directories
|
|
|
|
| 24 |
ANNOTATIONS_DIR.mkdir(parents=True, exist_ok=True)
|
| 25 |
CURSOR_TEMPLATES_DIR.mkdir(parents=True, exist_ok=True)
|
| 26 |
|
| 27 |
+
cursor_tracking_started = False # Only run once per server start
|
|
|
|
| 28 |
|
| 29 |
+
def stream_remote_frames_as_images():
|
| 30 |
+
"""Stream frames from remote endpoint and return as a list of (filename, image ndarray)."""
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
+
print("[INFO] Fetching remote frame list...")
|
| 33 |
res = requests.get(REMOTE_FRAMES_LIST_URL)
|
| 34 |
res.raise_for_status()
|
|
|
|
| 35 |
frame_list = res.json().get("frames", [])
|
| 36 |
print(f"[INFO] Found {len(frame_list)} frames.")
|
| 37 |
|
| 38 |
+
images = []
|
| 39 |
for fname in frame_list:
|
| 40 |
+
url = f"{REMOTE_FRAME_BASE_URL}/{fname}"
|
| 41 |
+
print(f"[INFO] Streaming: {url}")
|
| 42 |
+
img_bytes = requests.get(url).content
|
| 43 |
+
image = Image.open(BytesIO(img_bytes)).convert("RGB")
|
| 44 |
+
np_img = np.array(image)
|
| 45 |
+
images.append((fname, np_img))
|
| 46 |
+
print("[✅] All frames streamed successfully.")
|
| 47 |
+
return images
|
| 48 |
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
+
print(f"[ERROR] Failed to stream frames: {e}")
|
| 51 |
+
return []
|
| 52 |
|
| 53 |
def run_cursor_tracker_once():
|
| 54 |
global cursor_tracking_started
|
|
|
|
| 56 |
return
|
| 57 |
cursor_tracking_started = True
|
| 58 |
try:
|
| 59 |
+
images = stream_remote_frames_as_images()
|
| 60 |
+
if not images:
|
| 61 |
+
print("[ERROR] No images received for tracking.")
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
print("[INFO] Running cursor tracker on streamed images...")
|
| 65 |
+
track_cursor_from_images(
|
| 66 |
+
images=images,
|
| 67 |
+
cursor_templates_dir=str(CURSOR_TEMPLATES_DIR),
|
| 68 |
+
output_json_path=str(ANNOTATION_OUTPUT),
|
| 69 |
threshold=0.8,
|
|
|
|
| 70 |
email_results=False
|
| 71 |
)
|
| 72 |
print("[✅] Cursor tracking completed.")
|
|
|
|
| 75 |
|
| 76 |
@app.on_event("startup")
|
| 77 |
def startup_event():
|
| 78 |
+
threading.Thread(target=run_cursor_tracker_once, daemon=True).start()
|
|
|
|
|
|
|
| 79 |
|
| 80 |
@app.get("/download/{filename}")
|
| 81 |
def download_file(filename: str):
|