amir22010 commited on
Commit
41d9d30
·
1 Parent(s): 98a11ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -59,24 +59,27 @@ def infer(image):
59
  # Use this if you're loading images
60
  #image = Image.open(img_path).convert("RGB")
61
  #image = image.convert("RGB") # loading PDFs
62
- encoding = processor(image, return_offsets_mapping=True, return_tensors="pt", truncation=True, max_length=514)#max_positional_embeddings
63
- offset_mapping = encoding.pop('offset_mapping')
64
- outputs = model(**encoding)
65
- predictions = outputs.logits.argmax(-1).squeeze().tolist()
66
- token_boxes = encoding.bbox.squeeze().tolist()
67
- width, height = image.size
68
- is_subword = np.array(offset_mapping.squeeze().tolist())[:,0] != 0
69
-
70
- true_predictions = [id2label[pred] for idx, pred in enumerate(predictions) if not is_subword[idx]]
71
- true_boxes = [unnormalize_box(box, width, height) for idx, box in enumerate(token_boxes) if not is_subword[idx]]
72
- draw = ImageDraw.Draw(image)
73
-
74
- font = ImageFont.load_default()
75
-
76
- for prediction, box in zip(true_predictions, true_boxes):
77
- predicted_label = iob_to_label(prediction).lower()
78
- draw.rectangle(box, outline=label2color[predicted_label])
79
- draw.text((box[0]+10, box[1]-10), text=predicted_label, fill=label2color[predicted_label], font=font)
 
 
 
80
 
81
  return image
82
 
 
59
  # Use this if you're loading images
60
  #image = Image.open(img_path).convert("RGB")
61
  #image = image.convert("RGB") # loading PDFs
62
+ try:
63
+ encoding = processor(image, return_offsets_mapping=True, return_tensors="pt", truncation=True, max_length=514)#max_positional_embeddings
64
+ offset_mapping = encoding.pop('offset_mapping')
65
+ outputs = model(**encoding)
66
+ predictions = outputs.logits.argmax(-1).squeeze().tolist()
67
+ token_boxes = encoding.bbox.squeeze().tolist()
68
+ width, height = image.size
69
+ is_subword = np.array(offset_mapping.squeeze().tolist())[:,0] != 0
70
+
71
+ true_predictions = [id2label[pred] for idx, pred in enumerate(predictions) if not is_subword[idx]]
72
+ true_boxes = [unnormalize_box(box, width, height) for idx, box in enumerate(token_boxes) if not is_subword[idx]]
73
+ draw = ImageDraw.Draw(image)
74
+
75
+ font = ImageFont.load_default()
76
+
77
+ for prediction, box in zip(true_predictions, true_boxes):
78
+ predicted_label = iob_to_label(prediction).lower()
79
+ draw.rectangle(box, outline=label2color[predicted_label])
80
+ draw.text((box[0]+10, box[1]-10), text=predicted_label, fill=label2color[predicted_label], font=font)
81
+ except Exception as e:
82
+ print(e)
83
 
84
  return image
85