Table Transformer
Project description
Table Transformer Library
Original repository: https://github.com/microsoft/table-transformer
Introduction
This is the Table Transformer Model developed by Brandon Smock et al. of Microsoft AI. This repository consists of two big models: Table Detection (DETR) and Table Structure Recognition (TATR) for detecting and extracting table infomation into popular formats such as CSV or HTML table.
Usage
You can initialize the pipeline by calling the function.
table_extractor = TableExtractionPipeline(
str_config_path=config_path,
str_model_path=model_path,
str_device=device,
structure_class_thresholds=thresholds,
onnx_str_model_path=onnx_model_path)
with the following parameters:
str_config_path
: path to the structure recognition config json file (you can find one insrc/structure_config.json
)str_model_path
: path to the PyTorch model for table structure recogntion (you can download from here or find the pre-trained model inmodel/
)str_device
: inference device, eithercuda
orcpu
.structure_class_thresholds
: a dictionary of recognition thresholds, seeget_structure_class_thresholds()
insrc/inference
for more details.onnx_str_model_path
: path to the ONNX Table Structure Recognition model
The full model usage can be found here:
from table_transformer import (TableExtractionPipeline,
get_structure_class_thresholds,
visualize_cells)
from PIL import Image
import os
from tqdm import tqdm
# Get the structure class thresholds
structure_class_threshold = get_structure_class_thresholds(
table=0.5,
table_column=0.5,
table_row=0.5,
table_column_header=0.5,
table_projected_row_header=0.5,
table_spanning_cell=0.5,
no_object=10
)
# Initialize the Pipeline with the thresholds
table_transformer = TableExtractionPipeline(
structure_class_thresholds=structure_class_threshold,
onnx_str_model_path = "models/TATR-v1.1-All-msft.onnx")
# Open the table image in PIL.Image format
directory = "images\/"
# Loop through all images in a directory
for filename in tqdm(os.listdir(directory), desc="Processing images"):
img = Image.open(directory + filename)
# Run the pipeline, page_image is required
result = table_transformer(
page_image=img, onnx_inference=True,
out_cells=True,
out_html=True,
out_csv=True,
out_objects=True)
# Print the result
#print(result["objects"][0])
# print(result["cells"][0])
#print(result["html"][0])
# print(result["csv"][0])
# Visualize the result as Matplotlib plot and return the figure
visualize = visualize_cells(img=img, cells=result["cells"][0], show_plot=False)
#print(type(visualize)) # PIL.Image.Image
print("Test successful!")
Evaluation
With structure recognition, the original author has evaluated the v1.0 model on PubTables-1M with great results. With other datasets such as PubTabNet, the score is quite good.
You can check out the score and run the evaluation with your own dataset in this link.
Version history
- v1.0.1: Initial version
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for table_transformer-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da85960772a65777e769e9cb386b831c63412368a4cf3dd92995323ac06e12e4 |
|
MD5 | d7ffa6342a06e9e6c996b54c81c2377a |
|
BLAKE2b-256 | bbfc1faabd3ddf6789edff87d5345eb9e3f1b018fbcfea65b1f75ce7eca4cb09 |