🚀 pix2struct-base-table2html
将表格图像转换为HTML!
本模型能够解析表格图像,通过光学字符识别(OCR)和结构识别技术,将其转换为HTML格式,为表格数据的处理和展示提供了便利。
🚀 快速开始
你可以尝试使用包含表格检测和识别功能的演示应用!
✨ 主要特性
此模型接收表格图像并输出HTML。它会解析图像,执行光学字符识别(OCR)和结构识别,将其转换为HTML格式。
📦 安装指南
文档未提及安装步骤,故跳过此章节。
💻 使用示例
基础用法
以下是一个完整的示例,展示了如何加载模型并对示例表格图像(来自MMTab数据集)进行推理:
import torch
from transformers import AutoProcessor, Pix2StructForConditionalGeneration
from PIL import Image
import requests
from io import BytesIO
device = "cuda" if torch.cuda.is_available() else "cpu"
processor = AutoProcessor.from_pretrained("KennethTM/pix2struct-base-table2html")
model = Pix2StructForConditionalGeneration.from_pretrained("KennethTM/pix2struct-base-table2html")
model.to(device)
model.eval()
url = "https://huggingface.co/KennethTM/pix2struct-base-table2html/resolve/main/example_recog_1.jpg"
response = requests.get(url)
image = Image.open(BytesIO(response.content))
encoding = processor(image, return_tensors="pt", max_patches=1024)
with torch.inference_mode():
flattened_patches = encoding.pop("flattened_patches").to(device)
attention_mask = encoding.pop("attention_mask").to(device)
predictions = model.generate(flattened_patches=flattened_patches, attention_mask=attention_mask, max_new_tokens=1024)
predictions_decoded = processor.tokenizer.batch_decode(predictions, skip_special_tokens=True)
print(predictions_decoded[0])
示例图像:

示例图像的模型HTML输出:
<table border="1" cellspacing="0">
<tr>
<th>
Rank
</th>
<th>
Lane
</th>
<th>
Name
</th>
<th>
Nationality
</th>
<th>
Time
</th>
<th>
Notes
</th>
</tr>
<tr>
<td>
</td>
<td>
4
</td>
<td>
Michael Phelps
</td>
<td>
United States
</td>
<td>
51.25
</td>
<td>
OR
</td>
</tr>
<tr>
<td>
</td>
<td>
3
</td>
<td>
Ian Crocker
</td>
<td>
United States
</td>
<td>
51.29
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
5
</td>
<td>
Andriy Serdinov
</td>
<td>
Ukraine
</td>
<td>
51.36
</td>
<td>
EU
</td>
</tr>
<tr>
<td>
4
</td>
<td>
1
</td>
<td>
Thomas Rupprath
</td>
<td>
Germany
</td>
<td>
52.27
</td>
<td>
</td>
</tr>
<tr>
<td>
5
</td>
<td>
6
</td>
<td>
Igor Marchenko
</td>
<td>
Russia
</td>
<td>
52.32
</td>
<td>
</td>
</tr>
<tr>
<td>
6
</td>
<td>
2
</td>
<td>
Gabriel Mangabeira
</td>
<td>
Brazil
</td>
<td>
52.34
</td>
<td>
</td>
</tr>
<tr>
<td>
7
</td>
<td>
8
</td>
<td>
Duje Draganja
</td>
<td>
Croatia
</td>
<td>
52.46
</td>
<td>
</td>
</tr>
<tr>
<td>
8
</td>
<td>
7
</td>
<td>
Geoff Huegill
</td>
<td>
Australia
</td>
<td>
52.56
</td>
<td>
</td>
</tr>
</table>
渲染后的HTML表格:
Rank
|
Lane
|
Name
|
Nationality
|
Time
|
Notes
|
|
4
|
Michael Phelps
|
United States
|
51.25
|
OR
|
|
3
|
Ian Crocker
|
United States
|
51.29
|
|
|
5
|
Andriy Serdinov
|
Ukraine
|
51.36
|
EU
|
4
|
1
|
Thomas Rupprath
|
Germany
|
52.27
|
|
5
|
6
|
Igor Marchenko
|
Russia
|
52.32
|
|
6
|
2
|
Gabriel Mangabeira
|
Brazil
|
52.34
|
|
7
|
8
|
Duje Draganja
|
Croatia
|
52.46
|
|
8
|
7
|
Geoff Huegill
|
Australia
|
52.56
|
|
📚 详细文档
文档未提供详细说明,故跳过此章节。
🔧 技术细节
文档未提及技术实现细节,故跳过此章节。
📄 许可证
本项目采用MIT许可证。