| | import gradio as gr |
| | import cv2 |
| | import numpy as np |
| | from paddleocr import PaddleOCR |
| | from PIL import Image |
| |
|
| | |
| | |
| | |
| | ocr = PaddleOCR(use_angle_cls=True, lang='en') |
| |
|
| | def run_ocr(input_image): |
| | if input_image is None: |
| | return None, "No image uploaded" |
| |
|
| | |
| | image_np = np.array(input_image) |
| | |
| | |
| | |
| | |
| | |
| | |
| | result = ocr.ocr(image_np, cls=True) |
| |
|
| | |
| | if result is None or result[0] is None: |
| | return input_image, "No text detected." |
| |
|
| | result = result[0] |
| | |
| | detected_texts = [] |
| | |
| | |
| | viz_image = image_np.copy() |
| | |
| | for line in result: |
| | box = line[0] |
| | text_content = line[1][0] |
| | score = line[1][1] |
| | |
| | |
| | detected_texts.append(f"{text_content} (Conf: {score:.2f})") |
| | |
| | |
| | box = np.array(box).astype(np.int32).reshape((-1, 1, 2)) |
| | |
| | |
| | cv2.polylines(viz_image, [box], isClosed=True, color=(0, 255, 255), thickness=2) |
| |
|
| | |
| | full_text_output = "\n".join(detected_texts) |
| | |
| | return viz_image, full_text_output |
| |
|
| | |
| | with gr.Blocks(title="PaddleOCR Demo") as demo: |
| | gr.Markdown("## 🚀 PaddleOCR CPU Demo") |
| | gr.Markdown("Upload an image to detect and recognize text using PaddleOCR running on CPU.") |
| | |
| | with gr.Row(): |
| | with gr.Column(): |
| | input_img = gr.Image(type="pil", label="Input Image") |
| | submit_btn = gr.Button("Run OCR", variant="primary") |
| | |
| | with gr.Column(): |
| | output_img = gr.Image(label="Visualization") |
| | output_text = gr.Textbox(label="Detected Text", lines=10) |
| |
|
| | submit_btn.click(fn=run_ocr, inputs=input_img, outputs=[output_img, output_text]) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch(server_name="0.0.0.0", server_port=7860) |