Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
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 |
|