File size: 4,147 Bytes
2c67080
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# coding: utf-8

import os

from pix2text import TextFormulaOCR, merge_line_texts


def test_mfd():
    config = dict()
    model = TextFormulaOCR.from_config(config)

    res = model.recognize(
        './docs/examples/zh1.jpg', save_analysis_res='./analysis_res.jpg',
    )
    print(res)


def test_example():
    # img_fp = './docs/examples/formula.jpg'
    img_fp = './docs/examples/mixed.jpg'
    formula_config = {
        'model_name': 'mfr-pro',
        'model_backend': 'onnx',
    }
    p2t = TextFormulaOCR.from_config(total_configs={'formula': formula_config})
    print(p2t.recognize(img_fp))
    # print(p2t.recognize_formula(img_fp))
    # outs = p2t(img_fp, resized_shape=608, save_analysis_res='./analysis_res.jpg')  # can also use `p2t.recognize(img_fp)`
    # print(outs)
    # # To get just the text contents, use:
    # only_text = merge_line_texts(outs, auto_line_break=True)
    # print(only_text)


def test_blog_example():
    img_fp = './docs/examples/mixed.jpg'

    total_config = dict(
        mfd=dict(  # 声明 MFD 的初始化参数
            model_path=os.path.expanduser(
                '~/.pix2text/1.1/mfd-onnx/mfd-v20240618.onnx'
            ),  # 注:修改成你的模型文件所存储的路径
        ),
        formula=dict(
            model_name='mfr-pro',
            model_backend='onnx',
            model_dir=os.path.expanduser(
                '~/.pix2text/1.1/mfr-pro-onnx'
            ),  # 注:修改成你的模型文件所存储的路径
        ),
    )
    p2t = TextFormulaOCR.from_config(total_configs=total_config)
    outs = p2t.recognize(
        img_fp, resized_shape=608, return_text=False
    )  # 也可以使用 `p2t(img_fp)` 获得相同的结果
    print(outs)
    # 如果只需要识别出的文字和Latex表示,可以使用下面行的代码合并所有结果
    only_text = merge_line_texts(outs, auto_line_break=True)
    print(only_text)


def test_blog_pro_example():
    img_fp = './docs/examples/mixed.jpg'

    total_config = dict(
        languages=('en', 'ch_sim'),
        mfd=dict(  # 声明 MFD 的初始化参数
            model_path=os.path.expanduser(
                '~/.pix2text/1.1/mfd-onnx/mfd-v20240618.onnx'
            ),  # 注:修改成你的模型文件所存储的路径
        ),
        formula=dict(
            model_name='mfr-pro',
            model_backend='onnx',
            model_dir=os.path.expanduser(
                '~/.pix2text/1.1/mfr-pro-onnx'
            ),  # 注:修改成你的模型文件所存储的路径
        ),
        text=dict(
            rec_model_name='doc-densenet_lite_666-gru_large',
            rec_model_backend='onnx',
            rec_model_fp=os.path.expanduser(
                '~/.cnocr/2.3/doc-densenet_lite_666-gru_large/cnocr-v2.3-doc-densenet_lite_666-gru_large-epoch=005-ft-model.onnx'
                # noqa
            ),  # 注:修改成你的模型文件所存储的路径
        ),
    )
    p2t = TextFormulaOCR.from_config(total_configs=total_config)
    outs = p2t.recognize(
        img_fp, resized_shape=608, return_text=False
    )  # 也可以使用 `p2t(img_fp)` 获得相同的结果
    print(outs)
    # 如果只需要识别出的文字和Latex表示,可以使用下面行的代码合并所有结果
    only_text = merge_line_texts(outs, auto_line_break=True)
    print(only_text)


def test_example_mixed():
    img_fp = './docs/examples/en1.jpg'
    p2t = TextFormulaOCR.from_config()
    outs = p2t.recognize(
        img_fp, resized_shape=608, return_text=False
    )  # 也可以使用 `p2t(img_fp)` 获得相同的结果
    print(outs)
    # 如果只需要识别出的文字和Latex表示,可以使用下面行的代码合并所有结果
    only_text = merge_line_texts(outs, auto_line_break=True)
    print(only_text)


def test_example_formula():
    img_fp = './docs/examples/math-formula-42.png'
    p2t = TextFormulaOCR.from_config()
    outs = p2t.recognize_formula(img_fp)
    print(outs)


def test_example_text():
    img_fp = './docs/examples/general.jpg'
    p2t = TextFormulaOCR()
    outs = p2t.recognize_text(img_fp)
    print(outs)