热门搜索:和平精英 原神 街篮2 

您的位置:首页 > > 教程攻略 > ai资讯 >再谈表格数据合成-GOT-OCR

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

来源:互联网 更新时间:2026-06-10 14:10

说到表格数据合成,尤其是基于LaTeX的渲染方案,其实核心思路并不复杂,但有几个关键的坎儿必须迈过去。今天就结合GOT-OCR这条路子,把整个流程掰开揉碎了聊聊。

先得说清楚:环境得先搭好,不然下面这些操作都无从谈起。

再谈表格数据合成-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语法的熟悉程度了。刚才的代码里有一句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渲染不出来怎么处理?
  • 特殊字体渲染不出来怎么办?

等等。遇到问题动手去调,才能真正把这条路走通。

AI自动绘画大师
AI自动绘画大师

类型:益智休闲

大小:5.72MB

语言:简体中文

平台:互联网

游戏下载

热门手游

相关攻略

手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc