File size: 1,645 Bytes
a89d9fd
17e862b
5e7197d
a89d9fd
 
 
 
17e862b
a89d9fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17e862b
 
 
 
 
 
 
 
 
5e7197d
 
 
 
a89d9fd
17e862b
a89d9fd
17e862b
d1182db
a89d9fd
d1182db
a89d9fd
 
 
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
import os, io
from paddleocr import PaddleOCR, draw_ocr,PPStructure
from ppocr.utils.visual import draw_ser_results
from PIL import Image, ImageDraw
import gradio as gr


def inference__ppocr(img_path):

    ocr = PaddleOCR(
        rec_char_dict_path='zhtw_common_dict.txt',
        use_gpu=False,
        rec_image_shape="3, 48, 320"
    )
    
    result = ocr.ocr(img_path)
    
    for idx in range(len(result)):
        res = result[idx]
        for line in res:
            print(line)
    
    result = result[0]
    image = Image.open(img_path).convert('RGB')
    boxes = [line[0] for line in result]
    txts = [line[1][0] if line[1] else '' for line in result]  # 確保在無文字時 txts 還是個空字串
    scores = [line[1][1] for line in result]
    im_show_pil = draw_ocr(image, boxes, txts, scores, font_path="./simfang.ttf")

    return im_show_pil, "\n".join(txts)


def inference__ppstructure(img_path):

    ppsutructure = PPStructure(
        rec_char_dict_path='zhtw_common_dict.txt',
        use_gpu=False,
        rec_image_shape="3, 48, 320",
        ser_dict_path='ppocr/utils/dict/kie/clinical_class_list.txt'
    )
    
    result,res2 = ppsutructure.__call__(img_path)
    image = draw_ser_results(img_path,result,font_path='./simfang.ttf')
    result = [''.join(f"{element['pred']}:{element['transcription']}") for element in result if element['pred']!='O']
    return image, "\n".join(result)

    
gr.Interface(
    inference__ppstructure,
    [gr.Image(type='filepath', label='圖片上傳')],
    outputs=[
        gr.Image(type="pil", label="識別結果"),
        "text"
    ],
    ).launch(debug=True)