# I want to create a table OCR with table transformer

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fi-want-to-create-a-table-ocr-with-table-transformer%2F62244%3E%5C
**Category:** Beginners\
**Created:** [November 14, 2023, 5:04pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fi-want-to-create-a-table-ocr-with-table-transformer%2F62244 "2023-11-14T17:04:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chlamy](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@chlamy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fchlamy)%5C
**Post date:** [November 14, 2023, 5:04pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fi-want-to-create-a-table-ocr-with-table-transformer%2F62244%2F1 "2023-11-14T17:04:30Z")

</div>

Hi everyone !

I want to create a table OCR for read invoices but my code returns random cuts.

> from PIL import Image  
> from transformers import DetrImageProcessor,TableTransformerForObjectDetection,DetrFeatureExtractor,AutoImageProcessor,AutoFeatureExtractor,AutoModelForObjectDetection  
> import torch
> 
> def table\_detector(image):  
> processor = AutoImageProcessor.from\_pretrained(“microsoft/table-transformer-detection”)  
> model = TableTransformerForObjectDetection.from\_pretrained(“microsoft/table-transformer-detection”)  
> with torch.no\_grad():  
> inputs = processor(images=image, return\_tensors=“pt”)  
> outputs = model(\*\*inputs)  
> target\_sizes = torch.tensor([image.size[::-1]])  
> results = processor.post\_process\_object\_detection(outputs, threshold=0.9, target\_sizes=target\_sizes)[0]
> 
> for score, label, box in zip(results[“scores”], results[“labels”], results[“boxes”]):  
> box = [round(i, 2) for i in box.tolist()]  
> x, y, w, h = tuple(box)  
> return image.crop((x, y, x + w, y + h))
> 
> def table\_struct\_recog(image):  
> feature\_extractor = AutoFeatureExtractor.from\_pretrained(“microsoft/table-transformer-structure-recognition”)  
> model = AutoModelForObjectDetection.from\_pretrained(“microsoft/table-transformer-structure-recognition”)  
> inputs = feature\_extractor(images=image, return\_tensors=“pt”)  
> outputs = model(\*\*inputs)  
> target\_sizes = torch.tensor([image.size[::-1]])  
> results = feature\_extractor.post\_process\_object\_detection(outputs, threshold=0.9, \>target\_sizes=target\_sizes)[0]  
> i=0  
> for score, label, box in zip(results[“scores”], results[“labels”], results[“boxes”]):  
> box = [round(i, 2) for i in box.tolist()]  
> x, y, w, h = tuple(box)  
> image.crop((x, y, x + w, y + h)).save(str(i)+“.jpg”)  
> i = i+1
> 
> if **name** == “ **main** ”:  
> image = Image.open(“facture.jpg”).convert(“RGB”)  
> image = table\_detector(image)  
> table\_struct\_recog(image)

Thanks for your help 🙂
