首页
看点啥
插画图片
首页 看点啥 再谈表格数据合成-GOT-OCR

再谈表格数据合成-GOT-OCR

2026-08-15 0

再次提醒:首先你必须要把环境准备好,不然就进行不下去了。下面的内容都是在环境准备好的前提下来做的。

再谈表格数据合成-GOT-OCR

基本效果复现准备 latex 模板,就用下面最简单的模版就可以了,因为这里合成的仅仅是表格,不需要其它格式。

documentclass[10pt]{article}

usepackage[top=.5in,bottom=1in,left=.5in,right=.5in]{geometry}

usepackage[UTF8]{ctex}

usepackage[pagebackref=true,breaklinks=true,colorlinks,bookmarks=false]{hyperref}

usepackage{amsmath,amsfonts,mathrsfs,amssymb}

% 设置中文字体

usepackage{multirow}

renewcommand{normalsize}{fontsize{6pt}{6pt}selectfont}

pagestyle{empty} % 去掉页码

setCJKmainfont{SimSun}[BoldFont=KaiTi, ItalicFont=SimHei]

begin{document}

begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}

hline

& & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \

hline

品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \

hline

& 1 & 0.86 & 0.81 & 0.53 & 0.73 & 0.80 & 0.22 & 0.69 & 0.80 & 0.15 & 0.70 & 0.78 & 0.26 & 0.72 & 0.76 & 0.31 \

cline{2-17}

& 2 & 0.64 & 0.69 & 0.56 & 0.57 & 0.65 & 0.20 & 0.46 & 0.69 & 0.19 & 0.59 & 0.69 & 0.27 & 0.54 & 0.65 & 0.22 \

cline{2-17}

& 3 & 0.48 & 0.63 & 0.57 & 0.48 & 0.50 & 0.23 & 0.39 & 0.63 & 0.20 & 0.47 & 0.61 & 0.21 & 0.47 & 0.61 & 0.22 \

cline{2-17}

& 4 & 0.37 & 0.59 & 0.60 & 0.42 & 0.52 & 0.24 & 0.33 & 0.57 & 0.21 & 0.34 & 0.54 & 0.18 & 0.40 & 0.58 & 0.24 \

cline{2-17}

& 5 & 0.31 & 0.56 & 0.61 & 0.36 & 0.47 & 0.23 & 0.25 & 0.52 & 0.20 & 0.28 & 0.48 & 0.20 & 0.32 & 0.52 & 0.26 \

hline

& & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \

hline

品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \

hline

& 1 & 0.85 & 0.77 & 0.47 & 0.87 & 0.81 & 0.56 & 0.74 & 0.81 & 0.13 & 0.88 & 0.85 & 0.60 & 0.72 & 0.82 & 0.19 \

cline{2-17}

& 2 & 0.64 & 0.65 & 0.43 & 0.60 & 0.64 & 0.55 & 0.55 & 0.68 & 0.16 & 0.71 & 0.76 & 0.59 & 0.54 & 0.69 & 0.18 \

cline{2-17}

& 3 & 0.50 & 0.58 & 0.49 & 0.42 & 0.59 & 0.55 & 0.45 & 0.59 & 0.18 & 0.59 & 0.70 & 0.63 & 0.40 & 0.62 & 0.16 \

cline{2-17}

& 4 & 0.38 & 0.58 & 0.53 & 0.37 & 0.56 & 0.54 & 0.37 & 0.54 & 0.19 & 0.49 & 0.65 & 0.68 & 0.36 & 0.56 & 0.15 \

cline{2-17}

& 5 & 0.30 & 0.55 & 0.56 & 0.24 & 0.54 & 0.58 & 0.32 & 0.49 & 0.18 & 0.35 & 0.60 & 0.68 & 0.28 & 0.48 & 0.14 \

hline

end{tabular}

end{document}

假设上面的文件命名 tmp.tex,然后运行下面命令:

lualatex -output-directory=./tmp -interaction=nonstopmode  --shell-escape tmp_copy.tex

可获得如下渲染后的pdf结果:

相关问题先不往下看,我这里提几个简单问题,想想看,给你你怎么去解决!!!

  • 问题 1:表格的多样性包含哪些?怎么渲染获得到?
  • 问题 2:渲染后得到的 pdf,怎么定位到真正的表格位置,然后截取下来?
  • 问题 3:怎么自动化实现渲染过程?
  • 下面我们就围绕这最基本的三个问题再仔细的聊一聊!

    表格的多样性最直接能想到的多样性有以下几点:

  • 表格线框的多样性;
  • 表格内容的多样性;
  • 表格字体的多样性;
  • 表格形状的多样性;
  • 表格颜色的多样性;
  • 其中表格线框、内容、形状的多样性依赖于原始的 latex 文本内容的生成,这部分参考表格数据合成—GOT_OCR数据合成 这一部分内容。主要就是怎么利用大模型来生成格式各样的表格数据。

    表格字体和表格颜色的多样性就靠对 latex 语法的了解程度了。上面的 latex 代码中有这么一句 setCJKmainfont{SimSun}[BoldFont=KaiTi, ItalicFont=SimHei],它就是用于设置字体的。这下你明白了,怎么让字体央视丰富了吧!是的,就是在你的系统中多安装一些各式各样或者你的业务场景需要的字体,然后设置它就好了。

    颜色的多样性这一块,如果仅仅在文档场景可以先不考虑,因为你训练的时候将图片灰度化就好啦。

    pdf 表格获取最简单的方法就是将 pdf 转成图片,然后通过二值化方法进行获取。Opencv 中有很多二值化,如:

  • 简单阈值化:适用于简单的二值化需求。
  • 自适应阈值化:适用于光照不均匀的图像。
  • Otsu's 二值化:适用于双峰直方图的图像。
  • 三角形法二值化:适用于单峰直方图的图像。
  • 双阈值化:适用于特定范围的像素值二值化。
  • 这里使用自适应阈值化就基本够用了。下面代码样例:

    import fitz  # PyMuPDF

    import numpy as np

    import matplotlib.pyplot as plt

    import cv2

    def pdf_to_images_with_bbox(pdf_path):

    # 打开PDF文件

    pdf_document = fitz.open(pdf_path)

    for page_num in range(len(pdf_document)):

    # 获取当前页

    page = pdf_document.load_page(page_num)

    # 将页面转换为PIL图像

    pix = page.get_pixmap(dpi=288)

    img = np.frombuffer(buffer=pix.samples, dtype=np.uint8).reshape((pix.height, pix.width, 3))

    image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    # 应用自适应阈值化

    max_value = 255

    adaptive_method = cv2.ADAPTIVE_THRESH_MEAN_C # 或 cv2.ADAPTIVE_THRESH_GAUSSIAN_C

    block_size = 11

    C = 2

    binary_image = cv2.adaptiveThreshold(image, max_value, adaptive_method, cv2.THRESH_BINARY, block_size, C)

    print(binary_image.shape)

    y = np.sum(255 - binary_image, axis=-1)

    x = np.sum(255 - binary_image, axis=0)

    x = np.where(x>=1)[0]

    y = np.where(y >= 1)[0]

    x_min, x_max = x[0], x[-1]

    y_min, y_max = y[0], y[-1]

    print([x_min, y_min, y_min, y_max])

    cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (0,0, 255), 3)

    plt.imshow(img)

    plt.show()

    # 示例用法

    pdf_path = "./tmp.pdf"

    pdf_to_images_with_bbox(pdf_path)

    注:在截图的时候四边随机往外扩一点,贴着边把图截取下来感觉不是太好的。

    怎么自动化的生成我把上面的 latex 分成三段你可能就明白了:

    第一段:固定不变的
    documentclass[10pt]{article}

    usepackage[top=.5in,bottom=1in,left=.5in,right=.5in]{geometry}

    usepackage[UTF8]{ctex}

    usepackage[pagebackref=true,breaklinks=true,colorlinks,bookmarks=false]{hyperref}

    usepackage{amsmath,amsfonts,mathrsfs,amssymb}

    % 设置中文字体

    usepackage{multirow}

    renewcommand{normalsize}{fontsize{6pt}{6pt}selectfont}

    pagestyle{empty} % 去掉页码

    这里的字体大小,可以通过后处理成图像时候 dpi 来控制,这里就固定不变就好。

    第二段:可变的
    setCJKmainfont{SimSun}[BoldFont=KaiTi, ItalicFont=SimHei]

    这里只给出字体可变的全局设置,如果你对 latex 很熟,你可以把这里可变的都放在这里。我们把这段可变再改成适合 python 代码的:

    fonts = ["字体1","字体2","字体3",..., "字体n"]

    temp = f"\setCJKmainfont{{{font1}}}[BoldFont={font2}, ItalicFont={font3}]"

    set_font = temp.format(font1=random.choice(fonts),

    font2=random.choice(fonts),

    font3=random.choice(fonts),)

    这样 set_font 就具有随机性了吧。

    第三段: 表格多样性
    temp = f"""\begin{{document}}

    {tabular}

    \end{{document}}

    """

    tabular_text = r"""

    begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}

    hline

    & & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \

    hline

    品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \

    hline

    & 1 & 0.86 & 0.81 & 0.53 & 0.73 & 0.80 & 0.22 & 0.69 & 0.80 & 0.15 & 0.70 & 0.78 & 0.26 & 0.72 & 0.76 & 0.31 \

    cline{2-17}

    & 2 & 0.64 & 0.69 & 0.56 & 0.57 & 0.65 & 0.20 & 0.46 & 0.69 & 0.19 & 0.59 & 0.69 & 0.27 & 0.54 & 0.65 & 0.22 \

    cline{2-17}

    & 3 & 0.48 & 0.63 & 0.57 & 0.48 & 0.50 & 0.23 & 0.39 & 0.63 & 0.20 & 0.47 & 0.61 & 0.21 & 0.47 & 0.61 & 0.22 \

    cline{2-17}

    & 4 & 0.37 & 0.59 & 0.60 & 0.42 & 0.52 & 0.24 & 0.33 & 0.57 & 0.21 & 0.34 & 0.54 & 0.18 & 0.40 & 0.58 & 0.24 \

    cline{2-17}

    & 5 & 0.31 & 0.56 & 0.61 & 0.36 & 0.47 & 0.23 & 0.25 & 0.52 & 0.20 & 0.28 & 0.48 & 0.20 & 0.32 & 0.52 & 0.26 \

    hline

    & & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \

    hline

    品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \

    hline

    & 1 & 0.85 & 0.77 & 0.47 & 0.87 & 0.81 & 0.56 & 0.74 & 0.81 & 0.13 & 0.88 & 0.85 & 0.60 & 0.72 & 0.82 & 0.19 \

    cline{2-17}

    & 2 & 0.64 & 0.65 & 0.43 & 0.60 & 0.64 & 0.55 & 0.55 & 0.68 & 0.16 & 0.71 & 0.76 & 0.59 & 0.54 & 0.69 & 0.18 \

    cline{2-17}

    & 3 & 0.50 & 0.58 & 0.49 & 0.42 & 0.59 & 0.55 & 0.45 & 0.59 & 0.18 & 0.59 & 0.70 & 0.63 & 0.40 & 0.62 & 0.16 \

    cline{2-17}

    & 4 & 0.38 & 0.58 & 0.53 & 0.37 & 0.56 & 0.54 & 0.37 & 0.54 & 0.19 & 0.49 & 0.65 & 0.68 & 0.36 & 0.56 & 0.15 \

    cline{2-17}

    & 5 & 0.30 & 0.55 & 0.56 & 0.24 & 0.54 & 0.58 & 0.32 & 0.49 & 0.18 & 0.35 & 0.60 & 0.68 & 0.28 & 0.48 & 0.14 \

    hline

    end{tabular}

    """

    table_text = temp.format(tabular=tabular_text)

    经过上面三段表示,将三段表示合并起来:第一段 + 第二段 + 第三段,这样就可以获得完整的一个 latex 表达的、可渲染的文本了。

    总结上面的内容就是最简单的自动化生成多样性表格数据集的过程了,其实渲染过程还有很多细节,需要使用者自行去处理,如我还遇到过:

  • 渲染的表格超出 pdf 边界了怎么办?
  • pdf 渲染不出来怎么处理?
  • 特殊字体渲染不出来怎么办?
  • 等等!

    喜欢(0)
    猜你喜欢