🚀 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許可證。