🚀 pix2struct-base-table2html
表の画像をHTMLに変換します!
🚀 クイックスタート
このモデルは表の画像を入力として受け取り、HTMLを出力します。モデルは画像を解析し、光学文字認識(OCR)と構造認識を行い、HTML形式に変換します。
✨ 主な機能
- 表の画像をHTMLに変換することができます。
- モデルはPix2Struct base modelをベースに、max_patch_lengthを1024、max generation lengthを1024でファインチューニングされています。
📦 インストール
このライブラリはtransformers
を使用しています。必要に応じてインストールしてください。
pip install transformers
💻 使用例
基本的な使用法
以下は、モデルをロードし、表の画像の例に対して推論を行う完全な例です(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])
高度な使用法
このモデルのmax_patch_length
は推論時に変更しない方が良いですが、generation length
は変更することができます。以下のようにmax_new_tokens
を変更することができます。
predictions = model.generate(flattened_patches=flattened_patches, attention_mask=attention_mask, max_new_tokens=2048)
サンプル画像

サンプル画像に対するモデルの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
|
|
📚 ドキュメント
デモアプリ
デモアプリを試してみてください。このアプリには表の検出と認識の両方が含まれています。
注意事項
訓練データ
このモデルは、2つのデータセットを使用して訓練されています。
📄 ライセンス
このプロジェクトはMITライセンスの下で公開されています。