Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,24 @@ from fastapi import FastAPI, File, UploadFile
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
from PIL import Image
|
| 7 |
from io import BytesIO
|
| 8 |
import easyocr
|
| 9 |
from ultralytics import YOLO
|
| 10 |
-
import os
|
| 11 |
-
|
| 12 |
-
# ✅ Force YOLO & EasyOCR cache dirs
|
| 13 |
-
os.environ["EASYOCR_MODULE_PATH"] = "/app/.EasyOCR"
|
| 14 |
-
os.environ["EASYOCR_CACHE_DIR"] = "/app/.EasyOCR"
|
| 15 |
-
os.environ["YOLO_CONFIG_DIR"] = "/app/.yolo"
|
| 16 |
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
-
# ✅
|
| 20 |
-
reader = easyocr.Reader(['en'], model_storage_directory=
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
yolo_weights_path = "/root/.cache/torch/hub/checkpoints/yolov8n.pt"
|
| 24 |
-
model = YOLO(yolo_weights_path)
|
| 25 |
-
print("✅ YOLO model loaded from cache:", yolo_weights_path)
|
| 26 |
|
| 27 |
-
# ✅ CORS settings
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
| 30 |
allow_origins=["*"], allow_credentials=True,
|
| 31 |
allow_methods=["*"], allow_headers=["*"],
|
| 32 |
)
|
| 33 |
|
| 34 |
-
@app.get("/")
|
| 35 |
-
async def root():
|
| 36 |
-
return {"message": "YOLO Plate Detector API is running."}
|
| 37 |
-
|
| 38 |
@app.post("/detect")
|
| 39 |
async def detect_plate(file: UploadFile = File(...)):
|
| 40 |
try:
|
|
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
import easyocr
|
| 8 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# ✅ Use preloaded weights
|
| 13 |
+
reader = easyocr.Reader(['en'], model_storage_directory='/app/.EasyOCR')
|
| 14 |
+
model = YOLO("/app/yolov8n.pt")
|
| 15 |
+
print("✅ YOLO model loaded from /app/yolov8n.pt")
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"], allow_credentials=True,
|
| 20 |
allow_methods=["*"], allow_headers=["*"],
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.post("/detect")
|
| 24 |
async def detect_plate(file: UploadFile = File(...)):
|
| 25 |
try:
|