jinhybr commited on
Commit
7c1ac46
1 Parent(s): 5d50c04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -7
app.py CHANGED
@@ -29,6 +29,13 @@ processor = LiltForTokenClassification.from_pretrained("SCUT-DLVCLab/lilt-robert
29
  model = LayoutLMv3Processor.from_pretrained(
30
  "jinhybr/LiLt-funsd-en"
31
  )
 
 
 
 
 
 
 
32
 
33
  # load image example
34
  dataset = load_dataset("nielsr/funsd-layoutlmv3", split="test")
@@ -38,14 +45,11 @@ image.save("document.png")
38
 
39
  labels = dataset.features["ner_tags"].feature.names
40
  id2label = {v: k for v, k in enumerate(labels)}
41
- label2color = {
42
- "question": "blue",
43
- "answer": "green",
44
- "header": "orange",
45
- "other": "violet",
46
- }
47
 
48
 
 
 
 
49
  def unnormalize_box(bbox, width, height):
50
  return [
51
  width * (bbox[0] / 1000),
@@ -55,6 +59,18 @@ def unnormalize_box(bbox, width, height):
55
  ]
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def iob_to_label(label):
59
  label = label[2:]
60
  if not label:
@@ -62,6 +78,27 @@ def iob_to_label(label):
62
  return label
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def process_image(image):
66
  width, height = image.size
67
 
@@ -89,7 +126,9 @@ def process_image(image):
89
  if not is_subword[idx]
90
  ]
91
 
92
- # draw predictions over the image
 
 
93
  draw = ImageDraw.Draw(image)
94
  font = ImageFont.load_default()
95
  for prediction, box in zip(true_predictions, true_boxes):
@@ -101,6 +140,9 @@ def process_image(image):
101
  fill=label2color[predicted_label],
102
  font=font,
103
  )
 
 
 
104
 
105
  return image
106
 
 
29
  model = LayoutLMv3Processor.from_pretrained(
30
  "jinhybr/LiLt-funsd-en"
31
  )
32
+ ####
33
+
34
+
35
+
36
+
37
+
38
+ ####
39
 
40
  # load image example
41
  dataset = load_dataset("nielsr/funsd-layoutlmv3", split="test")
 
45
 
46
  labels = dataset.features["ner_tags"].feature.names
47
  id2label = {v: k for v, k in enumerate(labels)}
 
 
 
 
 
 
48
 
49
 
50
+
51
+
52
+ # helper function to unnormalize bboxes for drawing onto the image
53
  def unnormalize_box(bbox, width, height):
54
  return [
55
  width * (bbox[0] / 1000),
 
59
  ]
60
 
61
 
62
+ label2color = {
63
+ "B-HEADER": "blue",
64
+ "B-QUESTION": "red",
65
+ "B-ANSWER": "green",
66
+ "I-HEADER": "blue",
67
+ "I-QUESTION": "red",
68
+ "I-ANSWER": "green",
69
+ }
70
+
71
+
72
+
73
+
74
  def iob_to_label(label):
75
  label = label[2:]
76
  if not label:
 
78
  return label
79
 
80
 
81
+
82
+
83
+ # draw results onto the image
84
+ def draw_boxes(image, boxes, predictions):
85
+ width, height = image.size
86
+ normalizes_boxes = [unnormalize_box(box, width, height) for box in boxes]
87
+
88
+ # draw predictions over the image
89
+ draw = ImageDraw.Draw(image)
90
+ font = ImageFont.load_default()
91
+ for prediction, box in zip(predictions, normalizes_boxes):
92
+ if prediction == "O":
93
+ continue
94
+ draw.rectangle(box, outline="black")
95
+ draw.rectangle(box, outline=label2color[prediction])
96
+ draw.text((box[0] + 10, box[1] - 10), text=prediction, fill=label2color[prediction], font=font)
97
+ return image
98
+
99
+
100
+
101
+
102
  def process_image(image):
103
  width, height = image.size
104
 
 
126
  if not is_subword[idx]
127
  ]
128
 
129
+ draw_boxes(image, true_boxes, true_predictions)
130
+
131
+ '''' # draw predictions over the image
132
  draw = ImageDraw.Draw(image)
133
  font = ImageFont.load_default()
134
  for prediction, box in zip(true_predictions, true_boxes):
 
140
  fill=label2color[predicted_label],
141
  font=font,
142
  )
143
+ ''''
144
+
145
+
146
 
147
  return image
148