Spaces:
Sleeping
Sleeping
File size: 9,977 Bytes
ec0daf5 26543c8 ec0daf5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | import os
import subprocess
import shutil
import base64
import json
from typing import Optional
from fastapi import FastAPI, UploadFile, File, Request, HTTPException, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from ultralytics import YOLO
import cv2
import numpy as np
from pathlib import Path
import uuid
import time
from fastapi import BackgroundTasks
from fastapi.responses import FileResponse
app = FastAPI()
# Setup paths
BASE_DIR = Path(__file__).resolve().parent
UPLOAD_DIR = BASE_DIR / "uploads"
MODEL_DIR = UPLOAD_DIR / "models"
VIDEO_DIR = UPLOAD_DIR / "videos"
RESULT_DIR = UPLOAD_DIR / "results"
TEMP_DIR = UPLOAD_DIR / "temp"
for d in [MODEL_DIR, TEMP_DIR, VIDEO_DIR, RESULT_DIR]:
d.mkdir(parents=True, exist_ok=True)
# Global model state and task tracking
current_model = None
model_name = ""
video_tasks = {} # task_id: {"progress": P, "status": S, "result": R}
app.mount("/static", StaticFiles(directory=str(BASE_DIR / "static")), name="static")
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse(
request=request,
name="index.html",
context={
"model_loaded": current_model is not None,
"model_name": model_name
}
)
@app.post("/upload-model")
async def upload_model(file: UploadFile = File(...)):
global current_model, model_name
if not file.filename.endswith(".pt"):
raise HTTPException(status_code=400, detail="Only .pt files are supported")
file_path = MODEL_DIR / file.filename
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
try:
current_model = YOLO(str(file_path))
model_name = file.filename
return {"status": "success", "message": f"Model {model_name} loaded successfully"}
except Exception as e:
if os.path.exists(file_path):
os.remove(file_path)
raise HTTPException(status_code=500, detail=f"Failed to load model: {str(e)}")
def apply_roi_filter(results, roi, img_w, img_h):
if not roi:
return results, []
x1_roi = int(roi['x1'] * img_w / 100)
y1_roi = int(roi['y1'] * img_h / 100)
x2_roi = int(roi['x2'] * img_w / 100)
y2_roi = int(roi['y2'] * img_h / 100)
indices = []
for i, box in enumerate(results.boxes):
bx1, by1, bx2, by2 = box.xyxy[0].tolist()
bcx = (bx1 + bx2) / 2
bcy = (by1 + by2) / 2
if x1_roi <= bcx <= x2_roi and y1_roi <= bcy <= y2_roi:
indices.append(i)
results.boxes = results.boxes[indices]
return results, [x1_roi, y1_roi, x2_roi, y2_roi]
def draw_roi_on_img(img, roi_coords):
if not roi_coords:
return img
x1, y1, x2, y2 = roi_coords
# Draw a dashed or semi-transparent rectangle for ROI
overlay = img.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), (0, 255, 255), 2)
cv2.putText(overlay, "ROI ZONE", (x1 + 5, y1 + 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
return cv2.addWeighted(overlay, 0.6, img, 0.4, 0)
@app.post("/inference")
async def run_inference(
file: UploadFile = File(...),
conf_min: float = Form(0.25),
conf_max: float = Form(1.0),
roi: Optional[str] = Form(None)
):
global current_model
if current_model is None:
raise HTTPException(status_code=400, detail="No model loaded. Please upload a model first.")
# Parse ROI if present
roi_data = json.loads(roi) if roi else None
# Read image
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
raise HTTPException(status_code=400, detail="Invalid image file")
h, w = img.shape[:2]
# Run inference with min threshold
results = current_model(img, conf=conf_min)[0]
# Apply max confidence filtering
if conf_max < 1.0:
indices = [i for i, box in enumerate(results.boxes) if float(box.conf[0]) <= conf_max]
results.boxes = results.boxes[indices]
# Apply ROI filtering
results, roi_coords = apply_roi_filter(results, roi_data, w, h)
# Draw results
annotated_img = results.plot()
# Draw ROI box
annotated_img = draw_roi_on_img(annotated_img, roi_coords)
# Encode to base64
_, buffer = cv2.imencode('.jpg', annotated_img)
img_str = base64.b64encode(buffer).decode('utf-8')
# Extract box info
boxes = []
for box in results.boxes:
boxes.append({
"cls": int(box.cls[0]),
"conf": float(box.conf[0]),
"xyxy": box.xyxy[0].tolist()
})
return {
"status": "success",
"image": f"data:image/jpeg;base64,{img_str}",
"count": len(results.boxes),
"boxes": boxes
}
def process_video_task(task_id: str, input_path: str, output_path: str, conf_min: float, conf_max: float, roi: Optional[dict]):
global current_model, video_tasks
# Temporary path for OpenCV output
temp_output = str(RESULT_DIR / f"temp_{task_id}.mp4")
try:
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
video_tasks[task_id]["status"] = "error"
video_tasks[task_id]["message"] = "Could not open video file"
return
fps = cap.get(cv2.CAP_PROP_FPS)
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Using mp4v for the intermediate file
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(temp_output, fourcc, fps, (w, h))
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Inference with min threshold
results = current_model(frame, conf=conf_min)[0]
# Apply max confidence filtering
if conf_max < 1.0:
indices = [i for i, box in enumerate(results.boxes) if float(box.conf[0]) <= conf_max]
results.boxes = results.boxes[indices]
# Apply ROI filtering
results, roi_coords = apply_roi_filter(results, roi, w, h)
# Draw results
annotated_frame = results.plot()
# Draw ROI box
annotated_frame = draw_roi_on_img(annotated_frame, roi_coords)
out.write(annotated_frame)
frame_count += 1
# Update progress (0-90% for processing)
progress = int((frame_count / total_frames) * 90)
video_tasks[task_id]["progress"] = progress
cap.release()
out.release()
# Transcode to H.264 for web compatibility
video_tasks[task_id]["progress"] = 95
video_tasks[task_id]["status"] = "transcoding"
ffmpeg_cmd = [
'ffmpeg', '-y', '-i', temp_output,
'-c:v', 'libx264', '-preset', 'ultrafast', '-crf', '28',
'-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '128k',
output_path
]
subprocess.run(ffmpeg_cmd, check=True, capture_output=True)
video_tasks[task_id]["progress"] = 100
video_tasks[task_id]["status"] = "completed"
video_tasks[task_id]["result_url"] = f"/video-result/{task_id}"
except Exception as e:
video_tasks[task_id]["status"] = "error"
video_tasks[task_id]["message"] = str(e)
finally:
# Cleanup files
if os.path.exists(input_path):
os.remove(input_path)
if os.path.exists(temp_output):
os.remove(temp_output)
@app.post("/inference-video")
async def run_video_inference(
background_tasks: BackgroundTasks,
file: UploadFile = File(...),
conf_min: float = Form(0.25),
conf_max: float = Form(1.0),
roi: Optional[str] = Form(None)
):
global current_model, video_tasks
if current_model is None:
raise HTTPException(status_code=400, detail="No model loaded. Please upload a model first.")
# Parse ROI
roi_data = json.loads(roi) if roi else None
task_id = str(uuid.uuid4())
input_filename = f"{task_id}_{file.filename}"
input_path = VIDEO_DIR / input_filename
output_filename = f"processed_{task_id}.mp4"
output_path = RESULT_DIR / output_filename
with open(input_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
video_tasks[task_id] = {
"progress": 0,
"status": "processing",
"filename": file.filename
}
background_tasks.add_task(process_video_task, task_id, str(input_path), str(output_path), conf_min, conf_max, roi_data)
return {"status": "success", "task_id": task_id}
@app.get("/video-progress/{task_id}")
async def get_video_progress(task_id: str):
if task_id not in video_tasks:
raise HTTPException(status_code=404, detail="Task not found")
return video_tasks[task_id]
@app.get("/video-result/{task_id}")
async def get_video_result(task_id: str):
output_filename = f"processed_{task_id}.mp4"
output_path = RESULT_DIR / output_filename
if not output_path.exists():
raise HTTPException(status_code=404, detail="Result not found or still processing")
return FileResponse(path=output_path, filename=f"inference_{task_id}.mp4", media_type="video/mp4")
if __name__ == "__main__":
import uvicorn
# Use port from environment variable for Hugging Face compatibility (default 7860)
port = int(os.environ.get("PORT", 7860))
uvicorn.run(app, host="0.0.0.0", port=port)
|