Spaces:
Sleeping
Sleeping
Download app.py from SASORI666/EEE4483_SCPMS: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/SASORI666/EEE4483_SCPMS/resolve/main/app.py
- Command line
-
hf download hf://spaces/SASORI666/EEE4483_SCPMS/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SASORI666/EEE4483_SCPMS/resolve/main/app.py
1.03 kB
| import gradio as gr | |
| import requests | |
| from PIL import Image | |
| from io import BytesIO | |
| from transformers import pipeline | |
| # Load OCR pipeline once (server-side) | |
| ocr = pipeline("image-to-text", model="microsoft/trocr-base-stage1") | |
| def ocr_from_url(image_url: str): | |
| try: | |
| # Download image | |
| resp = requests.get(image_url, timeout=10) | |
| resp.raise_for_status() | |
| img = Image.open(BytesIO(resp.content)).convert("RGB") | |
| # Run OCR | |
| result = ocr(img) | |
| # pipeline returns list of dicts like [{'generated_text': 'ABC123'}] | |
| text = result[0].get("generated_text", "UNKNOWN") | |
| return text | |
| except Exception as e: | |
| return f"ERROR: {str(e)}" | |
| # Gradio interface: single text input (image URL) -> text output (plate) | |
| demo = gr.Interface(fn=ocr_from_url, | |
| inputs=gr.Textbox(label="Image URL"), | |
| outputs=gr.Textbox(label="Detected Plate"), | |
| title="License Plate OCR") | |
| if __name__ == "__main__": | |
| demo.launch() |