Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# Load OCR pipeline once (server-side)
|
| 8 |
+
ocr = pipeline("image-to-text", model="microsoft/trocr-base-stage1")
|
| 9 |
+
|
| 10 |
+
def ocr_from_url(image_url: str):
|
| 11 |
+
try:
|
| 12 |
+
# Download image
|
| 13 |
+
resp = requests.get(image_url, timeout=10)
|
| 14 |
+
resp.raise_for_status()
|
| 15 |
+
img = Image.open(BytesIO(resp.content)).convert("RGB")
|
| 16 |
+
|
| 17 |
+
# Run OCR
|
| 18 |
+
result = ocr(img)
|
| 19 |
+
# pipeline returns list of dicts like [{'generated_text': 'ABC123'}]
|
| 20 |
+
text = result[0].get("generated_text", "UNKNOWN")
|
| 21 |
+
return text
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"ERROR: {str(e)}"
|
| 24 |
+
|
| 25 |
+
# Gradio interface: single text input (image URL) -> text output (plate)
|
| 26 |
+
demo = gr.Interface(fn=ocr_from_url,
|
| 27 |
+
inputs=gr.Textbox(label="Image URL"),
|
| 28 |
+
outputs=gr.Textbox(label="Detected Plate"),
|
| 29 |
+
title="License Plate OCR")
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
demo.launch()
|