Spaces:
Build error
Build error
File size: 1,878 Bytes
1865436 |
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 |
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)
|