Spaces:
Running
Running
| import gradio as gr | |
| from paddleocr import PaddleOCR | |
| import numpy as np | |
| import cv2 | |
| # Load OCR model (text only) | |
| ocr_model = PaddleOCR( | |
| use_angle_cls=True, | |
| lang='en', | |
| det=True, | |
| rec=True | |
| ) | |
| def extract_text(image): | |
| if image is None: | |
| return "No image uploaded" | |
| # Convert PIL → cv2 BGR | |
| img = np.array(image) | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
| # Run OCR | |
| result = ocr_model.ocr(img, cls=True) | |
| # Parse extracted text | |
| lines = [] | |
| if result: | |
| for block in result: | |
| if isinstance(block, list): | |
| for line in block: | |
| if len(line) >= 2: | |
| lines.append(line[1][0]) | |
| if not lines: | |
| return "No text detected." | |
| return "\n".join(lines) | |
| demo = gr.Interface( | |
| fn=extract_text, | |
| inputs=gr.Image(type="pil", label="Upload Image"), | |
| outputs=gr.Textbox(label="Extracted Text"), | |
| title="PaddleOCR — Text Extraction Only", | |
| description="Upload an image to extract readable text using PaddleOCR." | |
| ) | |
| demo.launch() | |