Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,30 @@ 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="/app/.EasyOCR")
|
| 21 |
model = YOLO("/app/yolov8n.pt")
|
| 22 |
print("✅ YOLO model loaded from /app/yolov8n.pt")
|
| 23 |
|
|
|
|
| 24 |
app.add_middleware(
|
| 25 |
CORSMiddleware,
|
| 26 |
allow_origins=["*"], allow_credentials=True,
|
|
@@ -52,3 +57,29 @@ async def detect_plate(file: UploadFile = File(...)):
|
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
import os
|
| 10 |
|
| 11 |
+
# Gradio imports
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import requests
|
| 14 |
+
import io
|
| 15 |
+
|
| 16 |
+
# Set writable cache folders
|
| 17 |
os.environ["EASYOCR_MODULE_PATH"] = "/app/.EasyOCR"
|
| 18 |
os.environ["EASYOCR_CACHE_DIR"] = "/app/.EasyOCR"
|
| 19 |
os.environ["YOLO_CONFIG_DIR"] = "/app/.yolo"
|
| 20 |
|
| 21 |
app = FastAPI()
|
| 22 |
|
| 23 |
+
# Load models
|
| 24 |
reader = easyocr.Reader(['en'], model_storage_directory="/app/.EasyOCR")
|
| 25 |
model = YOLO("/app/yolov8n.pt")
|
| 26 |
print("✅ YOLO model loaded from /app/yolov8n.pt")
|
| 27 |
|
| 28 |
+
# CORS middleware
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
| 31 |
allow_origins=["*"], allow_credentials=True,
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 60 |
+
|
| 61 |
+
# --- Gradio frontend ---
|
| 62 |
+
|
| 63 |
+
def detect_plate_gradio(image):
|
| 64 |
+
buf = io.BytesIO()
|
| 65 |
+
image.save(buf, format="JPEG")
|
| 66 |
+
buf.seek(0)
|
| 67 |
+
files = {"file": ("image.jpg", buf, "image/jpeg")}
|
| 68 |
+
# Call your own deployed API endpoint
|
| 69 |
+
api_url = "https://huggingface.co/spaces/muddasser/Plate_Reader/api/detect"
|
| 70 |
+
response = requests.post(api_url, files=files)
|
| 71 |
+
if response.status_code == 200:
|
| 72 |
+
return response.json()
|
| 73 |
+
else:
|
| 74 |
+
return f"Error: {response.status_code}"
|
| 75 |
+
|
| 76 |
+
iface = gr.Interface(
|
| 77 |
+
fn=detect_plate_gradio,
|
| 78 |
+
inputs=gr.Image(type="pil"),
|
| 79 |
+
outputs="json",
|
| 80 |
+
title="License Plate Reader",
|
| 81 |
+
description="Upload an image to detect license plates"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
iface.launch()
|