Spaces:
Running

File size: 1,187 Bytes
1bda2f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import hf_hub_download
from rapidocr_onnxruntime import RapidOCR

repo = "Rebeccaaaaaaaa/korean-handwriting-rec"

# Load the model and dictionary files directly from the repository
engine = RapidOCR(
    params={
        "Rec.model_path": hf_hub_download(repo, "model/rec_korean_hw.onnx"),
        "Rec.rec_keys_path": hf_hub_download(repo, "model/ppocrv5_korean_dict.txt"),
    }
)


def recognize_korean(image):
    if image is None:
        return "Please upload an image."

    # Perform OCR on the uploaded image
    result, _ = engine(image)

    if result:
        # Join all detected text lines
        return "\n".join([line[1] for line in result])
    return "No text detected."


# Create a Gradio web interface
demo = gr.Interface(
    fn=recognize_korean,
    inputs=gr.Image(type="filepath", label="Upload Korean Handwriting Image"),
    outputs=gr.Textbox(label="Recognized Text"),
    title="Korean Handwriting OCR",
    description=(
        "Upload an image containing Korean handwriting or printed documents to"
        " extract text using the fine-tuned PP-OCRv5 model."
    ),
)

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