File size: 2,301 Bytes
423e2c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import gradio as gr

import boot
from ocr import detection, recognition


boot.warmup_model()


def extract_text_from_image(image):
    """
    Main function that will be called by Gradio.
    This function processes the image and returns both the image and extracted text.
    """
    if image is None:
        return None, "Please upload an image first"

    progress = gr.Progress()

    # Progress callback function for OCR
    def ocr_progress(current, total, message):
        progress(current / total, desc=message)

    detection_result = detection.inference(image, lang="mya", psm=6, min_conf=-1)
    recognition_result = recognition.inference(detection_result, progress_callback=ocr_progress)

    result_img = detection.draw_boxes(image, detection_result)
    image_with_detection_boxes = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)

    return image_with_detection_boxes, recognition_result


# Create the Gradio interface
with gr.Blocks(title="Myanmar OCR Demo", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Myanmar OCR Demo Application")

    with gr.Row():
        # Left column - Input
        with gr.Column(scale=1):
            gr.Markdown("## Input")
            input_image = gr.Image(label="Upload Image", type="numpy", height=400)
            process_btn = gr.Button("Extract Text", variant="primary", size="lg")

        # Right column - Output
        with gr.Column(scale=1):
            gr.Markdown("## Output")

            # Top part - Show input image
            with gr.Group():
                gr.Markdown("### Text Detection (Tesseract)")
                output_image = gr.Image(label="Detection Result", height=600, interactive=False)

            # Bottom part - Show extracted text
            with gr.Group():
                gr.Markdown("### Text Recognition (SMOL-VLM)")
                output_text = gr.Textbox(
                    label="OCR Results",
                    lines=10,
                    max_lines=15,
                    interactive=False,
                    placeholder="Extracted text will appear here...",
                )

    # Connect the button to the function
    process_btn.click(fn=extract_text_from_image, inputs=[input_image], outputs=[output_image, output_text])


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