Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Initialize OCR engine (loads models into memory once)
|
| 7 |
+
# lang='en' supports English. Change to 'ch' for Chinese/English mix.
|
| 8 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)
|
| 9 |
+
|
| 10 |
+
def run_ocr(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
return "No image provided"
|
| 13 |
+
|
| 14 |
+
# Gradio passes image as numpy array, perfect for PaddleOCR
|
| 15 |
+
result = ocr.ocr(image, cls=True)
|
| 16 |
+
|
| 17 |
+
txts = []
|
| 18 |
+
if result and result[0]:
|
| 19 |
+
# Extract just the text from the complex result
|
| 20 |
+
txts = [line[1][0] for line in result[0]]
|
| 21 |
+
|
| 22 |
+
return "\n".join(txts)
|
| 23 |
+
|
| 24 |
+
# Create the interface
|
| 25 |
+
# The 'api_name' is crucial - it's how your HTML file will find this function
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=run_ocr,
|
| 28 |
+
inputs=gr.Image(type="numpy"),
|
| 29 |
+
outputs="text",
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo.launch()
|