File size: 1,505 Bytes
50386b1
 
 
 
 
a03cf4d
50386b1
 
4616f86
 
 
 
 
 
50386b1
 
36604c8
 
 
 
8e5eef4
 
50386b1
 
8e5eef4
50386b1
 
4616f86
 
 
50386b1
 
 
 
 
 
6fd1f7c
50386b1
8e5eef4
 
 
 
 
 
 
 
db486c9
50386b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# main.py

from fastapi import FastAPI, UploadFile, File
import shutil
import os
from app.services.inference import detect_license_plate
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path

app = FastAPI(title="PlateVision Backend")


FRONTEND_URL = os.environ.get("FRONTEND_URL")


# BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
# UPLOAD_DIR = os.path.join(BASE_DIR, "temp")

BASE_DIR = Path(__file__).resolve().parent.parent.parent
# UPLOAD_DIR = BASE_DIR / "temp"
UPLOAD_DIR = "/app/temp"

os.makedirs(UPLOAD_DIR, exist_ok=True)

app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        FRONTEND_URL
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


app.mount("/temp", StaticFiles(directory=UPLOAD_DIR), name="temp")



@app.get("/")
async def root():
    return {"status": "ok", "message": "PlateVision API is running"}



@app.post("/api/detect")
async def detect(file: UploadFile = File(...)):
    file_path = os.path.join(UPLOAD_DIR, file.filename)

    # Save uploaded file
    with open(file_path, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    # Run inference
    detections, output_path = detect_license_plate(file_path)

    # return ONLY filename
    output_filename = os.path.basename(output_path)

    return {
        "filename": file.filename,
        "detections": detections,
        "output_image": output_filename
    }