import sys sys.path.append("./") import os import uvicorn import shutil from fastapi import FastAPI, File, UploadFile, Request from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles from src.tracker.signboard_track import SignboardTracker signboardTracker = SignboardTracker() app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") @app.get("/upload", response_class=HTMLResponse) async def upload(request: Request): return templates.TemplateResponse("tracker.html", {"request": request}) @app.post("/uploadfile") async def create_upload_file(file: UploadFile = File(...)): if file.content_type.split("/")[0] != "video": return {"file_name": file.filename, "file_type": file.content_type, "warning": "Need a video file instead."} else: output_folder = ('.').join(file.filename.split('.')[:-1]) if os.path.exists("./static/videos/" + output_folder): shutil.rmtree("./static/videos/" + output_folder) os.mkdir("./static/videos/" + output_folder) os.mkdir("./static/videos/" + output_folder + "/frames") with open(f"./static/videos/{output_folder}/{file.filename}", "wb") as buffer: shutil.copyfileobj(file.file, buffer) output = f"./static/videos/{output_folder}/{('.').join(file.filename.split('.')[:-1])}_traked.{file.filename.split('.')[-1]}" output_format = "mp4v" results = signboardTracker.inference_signboard(f"./static/videos/{output_folder}/{file.filename}", output, output_format, "./static/videos/" + output_folder + "/frames") return {"results": results} if __name__ == "__main__": # main() uvicorn.run("api.tracker:app", reload=True, port=8000)