File size: 1,946 Bytes
f3f94e2
32b9be1
f3f94e2
32b9be1
e2b3614
32b9be1
 
e2b3614
39b59cf
ca29a48
 
 
39b59cf
e2b3614
ca29a48
e2b3614
 
ca29a48
 
 
 
 
e2b3614
ca29a48
 
 
 
 
e2b3614
 
ca29a48
e2b3614
ca29a48
 
e2b3614
 
 
 
 
ca29a48
e2b3614
ca29a48
e2b3614
 
 
ca29a48
 
 
 
 
 
 
 
 
 
 
e2b3614
ca29a48
 
 
 
 
e2b3614
 
 
ca29a48
 
e2b3614
ca29a48
e2b3614
ca29a48
 
 
 
 
 
 
 
 
39b59cf
e2b3614
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os

os.environ["FLAGS_use_mkldnn"] = "0"
os.environ["FLAGS_enable_pir_api"] = "1"
os.environ["FLAGS_allocator_strategy"] = "auto_growth"
os.environ["OMP_NUM_THREADS"] = "1"


import gradio as gr
from paddleocr import PaddleOCR
from PIL import Image
import numpy as np

# Inisialisasi OCR (load sekali saja)
ocr = PaddleOCR(
    use_angle_cls=False,
    lang="en"
)

def ocr_image(image: Image.Image):
    if image is None:
        return "Silakan upload gambar terlebih dahulu.", None

    try:
        image = image.convert("RGB")
        image_np = np.array(image)
        width, height = image.size

        # Jalankan OCR
        result = ocr.ocr(image_np)

        texts = []
        output = []

        if result and result[0]:
            for line in result[0]:
                box = line[0]
                text = line[1][0]
                score = float(line[1][1])

                texts.append(text)
                output.append({
                    "bounding_box": box,
                    "text": text,
                    "confidence": score
                })

        summary = {
            "resolution": {
                "width": width,
                "height": height
            },
            "total_text": len(output),
            "results": output
        }

        return "\n".join(texts), summary

    except Exception as e:
        return f"Error: {str(e)}", None


with gr.Blocks(title="OCR App Paddle 3") as demo:
    gr.Markdown("## 📝 OCR Image Reader (PaddleOCR + Paddle 3)")

    with gr.Row():
        image_input = gr.Image(type="pil", label="Upload Gambar")

    with gr.Row():
        text_output = gr.Textbox(label="Hasil OCR (Text)", lines=12)
        json_output = gr.JSON(label="Detail Output")

    btn = gr.Button("Proses OCR")

    btn.click(
        fn=ocr_image,
        inputs=image_input,
        outputs=[text_output, json_output]
    )

if __name__ == "__main__":
    demo.launch()