I want to create a table OCR with table transformer
Hi everyone !I want to create a table OCR for read invoices but my code returns random cuts.from PIL import Imagefrom transformers import DetrImageProcessor,TableTransformerForObjectDetection,DetrFeatureExtractor,AutoImageProcessor,AutoFeatureExtractor,AutoModelForObjectDetectionimport torchdef 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=0for 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+1ifname== “main”:image = Image.open(“facture.jpg”).convert(“RGB”)image = table_detector(image)table_struct_recog(image)Thanks for your help