Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,22 @@ 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 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
import easyocr
|
| 8 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
# ✅
|
| 13 |
-
reader = easyocr.Reader(['en'], model_storage_directory=
|
| 14 |
model = YOLO("/app/yolov8n.pt")
|
| 15 |
print("✅ YOLO model loaded from /app/yolov8n.pt")
|
| 16 |
|
|
@@ -20,6 +27,10 @@ app.add_middleware(
|
|
| 20 |
allow_methods=["*"], allow_headers=["*"],
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.post("/detect")
|
| 24 |
async def detect_plate(file: UploadFile = File(...)):
|
| 25 |
try:
|
|
|
|
| 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 EasyOCR & YOLO to use writable cache folders
|
| 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 |
+
# ✅ Load EasyOCR and YOLO from local files
|
| 20 |
+
reader = easyocr.Reader(['en'], model_storage_directory="/app/.EasyOCR")
|
| 21 |
model = YOLO("/app/yolov8n.pt")
|
| 22 |
print("✅ YOLO model loaded from /app/yolov8n.pt")
|
| 23 |
|
|
|
|
| 27 |
allow_methods=["*"], allow_headers=["*"],
|
| 28 |
)
|
| 29 |
|
| 30 |
+
@app.get("/")
|
| 31 |
+
async def root():
|
| 32 |
+
return {"message": "YOLO + EasyOCR API is running. Use POST /detect to detect plates."}
|
| 33 |
+
|
| 34 |
@app.post("/detect")
|
| 35 |
async def detect_plate(file: UploadFile = File(...)):
|
| 36 |
try:
|