File size: 2,209 Bytes
ceeb3e5
 
 
 
 
 
 
 
 
 
 
735f62b
7bab241
72850a8
 
ceeb3e5
 
c9dff95
ceeb3e5
 
 
 
 
 
 
72850a8
2a74343
72850a8
 
735f62b
ceeb3e5
7bab241
 
ceeb3e5
 
 
 
 
735f62b
 
 
71dc437
735f62b
72850a8
ceeb3e5
 
 
 
 
 
 
 
735f62b
ceeb3e5
 
 
72850a8
ceeb3e5
 
72850a8
ceeb3e5
 
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
import gradio as gr
from paddleocr import PaddleOCR, draw_ocr
import json
import os
import cv2
import numpy as np
from PIL import Image

ocr_en = PaddleOCR(use_angle_cls=True, lang="en")
ocr_ch = PaddleOCR(use_angle_cls=True, lang='ch')
ocr_ru = PaddleOCR(use_angle_cls=True, lang='cyrillic')
ocr_ar = PaddleOCR(use_angle_cls=True, lang='ar')
ocr_korean = PaddleOCR(user_angle_cls=True, lang="korean")
ocr_fa = PaddleOCR(user_angle_cls=True, lang="fa")

#ocr_ch = PaddleOCR(det_model_dir="models/det/ch/ch_PP-OCRv4_det_infer", rec_model_dir="models/rec/ch/ch_PP-OCRv4_rec_infer", cls_model_dir="models/cls/ch_ppocr_mobile_v2.0_cls_infer", rec_char_dict_path="models/dict/ppocr_keys_v1.txt", lang="ch")

def perform_ocr(img, lang):
    
    if lang == "English":
        ocr = ocr_en
    elif lang == "Chinese (Simplified)":
        ocr = ocr_ch
    elif lang == "Russian" or lang == "Ukrainian":
        ocr = ocr_ru
    elif lang == "Arabic":
        ocr = ocr_ar
    elif lang == "Persian":
        ocr = ocr_fa
    elif lang == "Only Arabic Lang":
        ocr = ocr_ar
    elif lang == "Korean":
        ocr = ocr_korean

    result = ocr.ocr(img, cls=True)
    final_result = ""
 
    image = Image.open(img).convert('RGB')
    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    # im_show = draw_ocr(image, boxes, txts, scores, font_path='fonts/simfang.ttf')

    return [img, txts, result]

demo = gr.Blocks()

with demo:
    gr.Markdown("# Multilingual OCR")
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(source="upload", type="filepath")
            input_radio = gr.Radio(["English", "Chinese (Simplified)", "Russian", "Ukrainian", "Arabic", "Persian", "Only Arabic Lang"], label="Languages")
            input_button = gr.Button("Run!")
        with gr.Column():
            output_image = gr.Image()
            output_texts = gr.Textbox(label="Texts")
            output_text = gr.Textbox(label="Results")

    input_button.click(fn=perform_ocr, inputs=[input_image, input_radio], outputs=[output_image, output_texts, output_text])

demo.launch(server_name="0.0.0.0", server_port=7860)