Spaces:
Running
Running
Your HuggingFaceUsername commited on
Commit ·
4ba5ed3
0
Parent(s):
first commit
Browse files- app.py +42 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
reader = easyocr.Reader(['en'])
|
| 13 |
+
model = YOLO("yolov8n.pt")
|
| 14 |
+
print("✅ YOLO model loaded")
|
| 15 |
+
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"], allow_credentials=True,
|
| 19 |
+
allow_methods=["*"], allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
@app.post("/detect")
|
| 23 |
+
async def detect_plate(file: UploadFile = File(...)):
|
| 24 |
+
try:
|
| 25 |
+
contents = await file.read()
|
| 26 |
+
image = Image.open(BytesIO(contents)).convert("RGB")
|
| 27 |
+
image_np = np.array(image)
|
| 28 |
+
|
| 29 |
+
results = model(image_np)[0]
|
| 30 |
+
plate_texts = []
|
| 31 |
+
|
| 32 |
+
for box in results.boxes:
|
| 33 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 34 |
+
plate_crop = image_np[y1:y2, x1:x2]
|
| 35 |
+
text = reader.readtext(plate_crop, detail=0)
|
| 36 |
+
if text:
|
| 37 |
+
plate_texts.extend(text)
|
| 38 |
+
|
| 39 |
+
return JSONResponse(content={"plates": plate_texts})
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|
| 5 |
+
opencv-python-headless
|
| 6 |
+
torch
|
| 7 |
+
torchvision
|
| 8 |
+
easyocr
|
| 9 |
+
ultralytics
|
| 10 |
+
python-multipart
|