-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastAPI.py
More file actions
52 lines (40 loc) · 1.62 KB
/
FastAPI.py
File metadata and controls
52 lines (40 loc) · 1.62 KB
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
from fastapi import FastAPI, UploadFile, File, Request, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates
import os,shutil
import torch
from tracker import *
import uvicorn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"])
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("upload.html", {"request": request})
@app.post("/process_video/")
async def process_video_endpoint(file: UploadFile = File(...)):
input_path = f"temp_{file.filename}"
output_path = f"output_{file.filename}"
with open(input_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = get_the_model(ckpt_path="/home/muhammad/projects/SEE_assessment/detection_model.ckpt",device= device)
tracker = load_tracker()
try:
summary = process_video(input_path, output_path, model, tracker, device)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
finally:
if os.path.exists(input_path):
os.remove(input_path)
return JSONResponse(
content={"message": "Video processed successfully", "output_path": output_path, "summary": summary})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)