当前位置:   article > 正文

【ERNIE + PaddleOCR】 快速生成latex表格代码_latex自动生成

latex自动生成

一、项目背景

在撰写论文时,使用LaTeX编辑表格往往相当繁琐,需要深入掌握其语法,并参照已有论文模板进行细致调试。而现在,借助PaddleOCR和ERNIE等智能工具,我们可以迅速提取模板论文的表格信息并自动生成规范表格,极大地提升了工作效率和准确性。方便大家快速的对论文表格进行调整。

二、项目方案

首先利用PaddleOCR的表格识别功能,对论文里的表格进行识别,然后将结果的html代码进行解析,通过ERNIE转换html代码转换为latex代码。

html代码不需要再过多的处理,可以直接进行转换,十分方便。

三、数据说明

准备好论文的表格图片。

四、代码实现

4.1 环境安装

安装所需环境,需要安装PaddleOCR,最好从源代码进行安装,目前该项目更新较快,使用新版本更不容易出现错误。源代码安装首先根据requirements.txt安装所需环境以及一些必要的包(使用中报错提示需要安装一些额外的包)

In [ ]

  1. %cd ~
  2. # 首先建议你先从github上下载PaddleOCR的源码,https://github.com/PaddlePaddle/PaddleOCR.git
  3. # 先不要使用左侧套件里的PaddleOCR,它的版本使用起来有各种小问题。
  4. # 我这里先下载好了源代码并上传解压。
  5. !unzip PaddleOCR-main.zip
  6. # 安装源码所依赖环境
  7. %cd ~/PaddleOCR-main
  8. !pip install --user -r requirements.txt
  9. !pip install --user paddleclas PyMuPDF==1.19.0
  10. !pip install --user premailer openpyxl
  11. # 运行setup安装
  12. !python setup.py build install

4.2 PaddleOCR表格识别

有两种方法可以使用版面分析和文本识别,一种是命令行运行,一种是python代码运行。

命令行方式运行

In [ ]

  1. %cd ~/work
  2. !paddleocr --image_dir=table.png --type=structure --layout=false

python脚本方式运行

In [ ]

  1. import os
  2. import cv2
  3. from paddleocr import PPStructure,save_structure_res
  4. table_engine = PPStructure(layout=False, show_log=True)
  5. save_folder = './output'
  6. img_path = 'table.png'
  7. img = cv2.imread(img_path)
  8. result = table_engine(img)
  9. save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])
  10. for line in result:
  11. line.pop('img')
  12. print(line)

运行完毕后的文件在work的output文件夹里。

4.3 ERNIE生成latex

之后使用ERNIE Bot进行转换。

In [ ]

  1. # 安装ERNIE Bot
  2. !pip install --upgrade erniebot

设计prompt在后续使用

In [ ]

  1. # 设计prompt
  2. prompt = (
  3. "我给你一个html代码,它里面是一个table表格,"
  4. "帮我转换为latex的代码。"
  5. "html代码为:"
  6. )

获得PadedleOCR返回的html数据

In [ ]

  1. # 表格识别结果文件
  2. file_path = '/home/aistudio/work/output/table/res_0.txt'
  3. html_list = []
  4. with open(file_path, 'r', encoding='utf-8') as file:
  5. # 逐行读取文件内容
  6. for line in file:
  7. # 尝试将字符串转换为字典
  8. try:
  9. data_dict = eval(line)
  10. # 在这里处理数据
  11. if data_dict['type'] == 'table' and isinstance(data_dict['res'], dict):
  12. html = data_dict['res']['html']
  13. html_list.append(html)
  14. except Exception as e:
  15. # 如果转换失败,打印错误信息
  16. print(f"Error evaluating line in {filename}: {e}")

五、效果展示

使用ernie-3.5来进行转换,得到转换的结果,获得基本的latex表格代码。

In [ ]

  1. import erniebot
  2. models = erniebot.Model.list()
  3. print(models)
  4. # Set authentication params
  5. erniebot.api_type = "aistudio"
  6. erniebot.access_token = "你的token"
  7. # 这里用了一个图片,所以list里只有一个list元素
  8. for html in html_list:
  9. response = erniebot.ChatCompletion.create(
  10. model="ernie-3.5",
  11. messages=[{"role": "user", "content": prompt+html}]
  12. )
  13. print(response.get_result())

此项目已发布应用,可以来体验一下。https://aistudio.baidu.com/application/detail/28910

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/965410
推荐阅读
相关标签
  

闽ICP备14008679号