File size: 2,018 Bytes
80298ee
c9a595a
 
 
f9a67e7
48bfb35
c9a595a
 
 
a8cf8ca
c9a595a
 
 
 
365510a
c9a595a
a8cf8ca
c9a595a
 
 
a8cf8ca
dc22437
 
 
9214ebb
a87b5b7
9240efa
a8cf8ca
 
 
 
b4bc2b8
a8cf8ca
 
c9a595a
 
 
a8cf8ca
 
 
cd4dcaf
c9a595a
 
 
 
 
 
cd4dcaf
c9a595a
9cb950b
 
dc22437
 
66598b9
9cb950b
 
66598b9
a8cf8ca
 
c9a595a
a8cf8ca
 
 
 
c9a595a
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
import gradio as gr
import os
import cv2
import numpy as np
from PIL import Image
from paddleocr import PPStructure, save_structure_res, draw_structure_result
from paddleocr.ppstructure.recovery.recovery_to_doc import sorted_layout_boxes, convert_info_docx

# Chinese image
# table_engine = PPStructure(recovery=True)

# English image
# table_engine = PPStructure(recovery=True, lang='en')

examples = [['0.png']]

def find_layout(image, mode):
    save_folder = './output'
    # img = cv2.imread(image)
    img = np.array(image)

    print("The variable image is of type:", type(image))
    print("The variable img is of type:", type(img))

    if mode is not None:
        mode = mode[0]
    print("Mode: ", mode)
    if mode=="LayoutAnalysis":
        table_engine = PPStructure(table=False, ocr=False, show_log=True)
    elif mode=="TableRecognition":
        table_engine = PPStructure(layout=False, show_log=True)
    else:
        table_engine = PPStructure(recovery=True)
    
    result = table_engine(img)
    save_structure_res(result, save_folder, os.path.basename("result").split('.')[0])

    if mode=="LayoutReccovery":
        h, w, _ = img.shape
        res = sorted_layout_boxes(result, w)
    
    final_text = []
    for line in result:
        line.pop('img')
        print(line)
        final_text.append(line)

    # convert_info_docx(img, res, save_folder, os.path.basename("result").split('.')[0])

    font_path = 'simfang.ttf' # PaddleOCR下提供字体包

    # image = Image.open(img_path).convert('RGB')
    image = image.convert('RGB')
    # img = img.convert('RGB')
    im_show = draw_structure_result(image, result, font_path=font_path)
    im_show = Image.fromarray(im_show)
    # im_show.save('result.jpg')

    return final_text, im_show

iface = gr.Interface(fn=find_layout, inputs=[
        gr.Image(type="pil"),
        gr.CheckboxGroup(["LayoutAnalysis", "TableRecognition", "LayoutRecovery"], label="mode"),
    ], outputs=["text", "image"], examples=examples)
iface.launch()