ethancoen/paddleocr-handwritings
0
1import gradio as gr
2from paddleocr import PaddleOCR, draw_ocr
3from PIL import Image
4import numpy as np
5import cv2
6
7ocr = PaddleOCR(use_angle_cls=True, lang='en') # You can add 'bn' for Bangla too
8
9def ocr_image(input_image):
10 image_np = np.array(input_image)
11 image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
12
13 result = ocr.ocr(image_cv, cls=True)
14
15 boxes = [line[0] for line in result[0]]
16 txts = [line[1][0] for line in result[0]]
17 scores = [line[1][1] for line in result[0]]
18
19 annotated_img = draw_ocr(image_cv, boxes, txts, scores, font_path='Arial.ttf')
20 annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
21
22 text_result = "\n".join(txts)
23 return Image.fromarray(annotated_img), text_result
24
25iface = gr.Interface(
26 fn=ocr_image,
27 inputs=gr.Image(type="pil"),
28 outputs=[gr.Image(label="OCR Output"), gr.Textbox(label="Detected Text")],
29 title="๐ PaddleOCR Handwriting Reader",
30 description="Upload an image with handwriting. PaddleOCR will detect and extract the text."
31)
32
33iface.launch()
34 