File size: 1,068 Bytes
3750475
 
 
 
 
6f36436
3750475
 
 
 
6f36436
3750475
 
6f36436
 
 
3750475
6f36436
 
 
3750475
6f36436
 
3750475
6f36436
 
 
 
 
 
 
 
3750475
6f36436
 
3750475
6f36436
3750475
 
 
6f36436
dc07266
6f36436
 
 
3750475
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()