跳到主要内容

9. 多模态表格扫描应用

9. 多模态表格扫描应用1. 概念介绍1.1 “多模态表格扫描”是什么?1.2 实现原理简述2. 代码解析关键代码1. 工具层入口 (largemodel/utils/tools_manager.py)2. 模型接口层 (largemodel/utils/large_model_interface.py)代码解析3. 实践操作3.1 配置在线LLM3.2 启动并测试功能

1. 概念介绍

1.1 “多模态表格扫描”是什么?

多模态表格扫描 是一种利用图像处理和人工智能技术从图片或PDF文档中识别并提取表格信息的技术。它不仅关注视觉上的表格结构识别,还结合文本内容、布局信息等多模态数据以增强对表格的理解。而大型语言模型(LLM) 则在理解这些提取出的信息方面提供了强大的语义分析能力,二者相辅相成,共同提升文档处理的智能化水平。

1.2 实现原理简述

  1. 表格检测与内容识别

    • 利用计算机视觉技术定位文档中的表格,并通过OCR技术将表格内的文字转换为可编辑格式。
    • 采用深度学习方法解析表格结构(行列划分、合并单元格等),生成结构化数据表示。
  2. 多模态融合

    • 将视觉(如表格布局)、文本(OCR结果)以及可能存在的元数据(如文件类型、来源)整合起来,形成一个综合的数据视图。
    • 使用专门设计的多模态模型(例如LayoutLM)来同时处理这些不同类型的数据,以便更准确地理解表格内容及其上下文关系。

2. 代码解析

关键代码

1. 工具层入口 (largemodel/utils/tools_manager.py)

此文件中的scan_table函数定义了该工具的执行流程,特别是它如何构建一个要求返回Markdown格式的Prompt。

​ x # From largemodel/utils/tools_manager.py class ToolsManager : # ... def scan_table ( self , args ): """ Scan a table from an image and save the content as a Markdown file. 从图像中扫描表格,并将内容保存为Markdown文件。 ​ :param args: Arguments containing the image path. :return: Dictionary with file path and content. """ self . node . get_logger (). info ( f"Executing scan_table() tool with args: {args}" ) try : image_path = args . get ( "image_path" ) # ... (路径检查和回退) ​ # Construct a prompt asking the large model to recognize the table and return it in Markdown format. # 构造提示,要求大模型识别表格并以Markdown格式返回。 if self . node . language == 'zh' : prompt = "请仔细分析这张图片,识别其中的表格,并将其内容以Markdown格式返回。" else : prompt = "Please carefully analyze this image, identify the table within it, and return its content in Markdown format." ​ result = self . node . model_client . infer_with_image ( image_path , prompt ) # ... (从结果中提取Markdown文本) ​ # Save the recognized content to a Markdown file. / 将识别出的内容保存到Markdown文件。 md_file_path = os . path . join ( self . node . pkg_path , "resources_file" , "scanned_tables" , f"table_{timestamp}.md" ) with open ( md_file_path , 'w' , encoding = 'utf-8' ) as f : f . write ( table_content ) ​ return { "file_path" : md_file_path , "table_content" : table_content } # ... (错误处理)

2. 模型接口层 (largemodel/utils/large_model_interface.py)

此文件中的infer_with_image函数是所有图像相关任务的统一入口。

xxxxxxxxxx # From largemodel/utils/large_model_interface.py ​ class model_interface : # ... def infer_with_image ( self , image_path , text = None , message = None ): """Unified image inference interface. / 统一的图像推理接口。""" # ... (准备消息) try : # 根据 self.llm_platform 的值,决定调用哪个具体实现 if self . llm_platform == 'ollama' : response_content = self . ollama_infer ( self . messages , image_path = image_path ) elif self . llm_platform == 'tongyi' : # ... 调用通义模型的逻辑 pass # ... (其他平台的逻辑) # ... return { 'response' : response_content , 'messages' : self . messages . copy ()}

代码解析

表格扫描功能是将非结构化的图像数据转换为结构化文本数据的典型应用。其核心技术依然是通过Prompt Engineering引导模型行为

  1. 工具层 (tools_manager.py):

    • scan_table函数是此功能的业务流程控制器。它接收一张包含表格的图像作为输入。
    • 该函数最关键的操作是构建一个目标明确的Prompt 。这个Prompt直接指示大模型执行两个任务:1. 识别图像中的表格。2. 将识别出的内容以Markdown的格式返回。这个对输出格式的强制要求是实现非结构化到结构化转换的关键。
    • 构建好Prompt后,它调用模型接口层的infer_with_image方法,将图像和这个格式化指令一同传递过去。
    • 在从模型接口层拿到返回的Markdown文本后,它会执行一个文件操作:将这段文本内容写入一个新的.md文件中。
    • 最后,它返回包含新文件路径和表格内容的结构化数据。
  2. 模型接口层 (large_model_interface.py):

    • infer_with_image函数继续作为统一的“调度中心”。它接收来自scan_table的图像和Prompt,并根据当前系统配置(self.llm_platform)将任务分派给正确的后端模型实现。
    • 无论后端是何种模型,这一层的任务都是处理与具体平台的通信细节,确保图像和文本数据被正确发送,然后将模型返回的纯文本(在这里是Markdown格式的文本)交还给工具层。

总结来说,表格扫描的通用流程是:ToolsManager接收图像并构建一个“将此图中的表格转为Markdown”的指令 -> ToolsManager调用模型接口 -> model_interface将图像和该指令打包,并根据配置发送给相应的模型平台 -> 模型返回Markdown格式的文本 -> model_interface将文本返回给ToolsManager -> ToolsManager将文本保存为.md文件并返回结果。这个流程展示了如何利用大模型的格式遵循能力,将其用作一个强大的OCR(光学字符识别)及数据结构化工具。

3. 实践操作

3.1 配置在线LLM

  1. 先从前面教程的任意一个平台中获取API Key之后获取API Key

  2. 然后需要更新配置文件中的key,打开模型接口配置文件large_model_interface.yaml:

xxxxxxxxxx vim ~/yahboom_ws/src/largemodel/config/large_model_interface.yaml 3. 填入你的API Key : 找到对应的部分,将你刚刚复制的API Key粘贴进去。这里以通义千问配置为例

xxxxxxxxxx # large_model_interface.yaml ​ ## 通义千问 qianwen_api_key : "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # 粘贴你的Key qianwen_model : "qwen-vl-max-latest" # 可以根据需要选择模型, 如qwen-turbo, qwen-plus 4. 打开主配置文件yahboom.yaml:

xxxxxxxxxx vim ~/yahboom_ws/src/largemodel/config/yahboom.yaml 5. 选择要使用的在线平台 : 修改llm_platform参数为你想要使用的平台名称

xxxxxxxxxx # yahboom.yaml ​ model_service : ros__parameters : # ... llm_platform : 'tongyi' #可选平台: 'ollama', 'tongyi', 'spark', 'qianfan', 'openrouter'

3.2 启动并测试功能

  1. 准备表格图片文件 :

将一个要测试的表格图片文件放置到以下的路径: /home/jetson/yahboom_ws/src/largemodel/resources_file/scan_table

然后将图片命名为 test_table.jpg

  1. **启动 largemodel 主程序:

打开一个终端,然后运行下面的指令:

xxxxxxxxxx ros2 launch largemodel largemodel_control.launch.py text_chat_mode:=true 3. 发送文本指令 : 再次打开另一个终端,运行下面的指令,

xxxxxxxxxx ros2 run text_chat text_chat

然后开始输入文本:“分析一下表格”。

  1. 观察结果 : 在第一个运行主程序的终端中,你将看到日志输出,显示系统接收到指令,调用scan_table工具,提示scan_table执行完成,将扫描到的信息保存到了文档。

我们可以到 ~/yahboom_ws/src/largemodel/resources_file/scan_table 路径下找到这个文档。