pp_structure_v2.py 2.57 KB
Newer Older
1
2
3
import random

from loguru import logger
4
5
6
7

try:
    from paddleocr import PPStructure
except ImportError:
赵小蒙's avatar
赵小蒙 committed
8
    logger.error('paddleocr not installed, please install by "pip install magic-pdf[cpu]" or "pip install magic-pdf[gpu]"')
9
    exit(1)
10

blue's avatar
blue committed
11

12
13
14
15
16
17
18
19
def region_to_bbox(region):
    x0 = region[0][0]
    y0 = region[0][1]
    x1 = region[2][0]
    y1 = region[2][1]
    return [x0, y0, x1, y1]


blue's avatar
blue committed
20
class CustomPaddleModel:
赵小蒙's avatar
赵小蒙 committed
21
    def __init__(self, ocr: bool = False, show_log: bool = False):
blue's avatar
blue committed
22
23
24
25
        self.model = PPStructure(table=False, ocr=ocr, show_log=show_log)

    def __call__(self, img):
        result = self.model(img)
26
27
        spans = []
        for line in result:
blue's avatar
blue committed
28
29
            line.pop("img")
            """
30
31
32
33
34
35
36
37
38
39
40
41
            为paddle输出适配type no.    
            title: 0 # 标题
            text: 1 # 文本
            header: 2 # abandon
            footer: 2 # abandon
            reference: 1 # 文本 or abandon
            equation: 8 # 行间公式 block
            equation: 14 # 行间公式 text
            figure: 3 # 图片
            figure_caption: 4 # 图片描述
            table: 5 # 表格
            table_caption: 6 # 表格描述
blue's avatar
blue committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            """
            if line["type"] == "title":
                line["category_id"] = 0
            elif line["type"] in ["text", "reference"]:
                line["category_id"] = 1
            elif line["type"] == "figure":
                line["category_id"] = 3
            elif line["type"] == "figure_caption":
                line["category_id"] = 4
            elif line["type"] == "table":
                line["category_id"] = 5
            elif line["type"] == "table_caption":
                line["category_id"] = 6
            elif line["type"] == "equation":
                line["category_id"] = 8
            elif line["type"] in ["header", "footer"]:
                line["category_id"] = 2
59
60
            else:
                logger.warning(f"unknown type: {line['type']}")
61
62
63

            # 兼容不输出score的paddleocr版本
            if line.get("score") is None:
blue's avatar
blue committed
64
                line["score"] = 0.5 + random.random() * 0.5
65

blue's avatar
blue committed
66
            res = line.pop("res", None)
67
68
            if res is not None and len(res) > 0:
                for span in res:
blue's avatar
blue committed
69
70
71
72
73
74
                    new_span = {
                        "category_id": 15,
                        "bbox": region_to_bbox(span["text_region"]),
                        "score": span["confidence"],
                        "text": span["text"],
                    }
75
76
77
78
79
                    spans.append(new_span)

        if len(spans) > 0:
            result.extend(spans)

blue's avatar
blue committed
80
        return result