ivelin commited on
Commit
63af266
·
1 Parent(s): 1404129

fix: bbox bugs

Browse files

Signed-off-by: ivelin <[email protected]>

Files changed (1) hide show
  1. app.py +40 -8
app.py CHANGED
@@ -55,8 +55,38 @@ def process_refexp(image: Image, prompt: str):
55
  print(
56
  fr"predicted decoder sequence before token2json: {html.escape(sequence)}")
57
  bbox = processor.token2json(sequence)
58
- bbox = bbox['target_bounding_box']
59
- print(f"predicted bounding box: {bbox}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  print(f"image object: {image}")
62
  print(f"image size: {image.size}")
@@ -65,23 +95,25 @@ def process_refexp(image: Image, prompt: str):
65
  print(f"processed prompt: {prompt}")
66
 
67
  # safeguard in case text prediction is missing some bounding box coordinates
68
- xmin = math.floor(width*float(bbox.get("xmin", 0)))
69
- ymin = math.floor(height*float(bbox.get("ymin", 0)))
70
- xmax = math.floor(width*float(bbox.get("xmax", 1)))
71
- ymax = math.floor(height*float(bbox.get("ymax", 1)))
72
 
73
  print(
74
  f"to image pixel values: xmin, ymin, xmax, ymax: {xmin, ymin, xmax, ymax}")
75
 
76
  shape = [(xmin, ymin), (xmax, ymax)]
77
 
78
- # create rectangle image
79
  img1 = ImageDraw.Draw(image)
80
  img1.rectangle(shape, outline="green", width=5)
 
 
81
  return image, bbox
82
 
83
 
84
- title = "Demo: Donut 🍩 for UI RefExp"
85
  description = "Gradio Demo for Donut RefExp task, an instance of `VisionEncoderDecoderModel` fine-tuned on UIBert RefExp Dataset (UI Referring Expression). To use it, simply upload your image and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
86
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2111.15664' target='_blank'>Donut: OCR-free Document Understanding Transformer</a> | <a href='https://github.com/clovaai/donut' target='_blank'>Github Repo</a></p>"
87
  examples = [["example_1.jpg", "select the setting icon from top right corner"],
 
55
  print(
56
  fr"predicted decoder sequence before token2json: {html.escape(sequence)}")
57
  bbox = processor.token2json(sequence)
58
+
59
+ # safeguard in case predicted sequence does not include a target_bounding_box token
60
+ bbox = target_bbox.get('target_bounding_box')
61
+ if bbox is None:
62
+ print(
63
+ f"token2bbox seq has no predicted target_bounding_box, seq:{seq}")
64
+ bbox = bbox = {"xmin": 0, "ymin": 0, "xmax": 0, "ymax": 0}
65
+ return bbox
66
+
67
+ print(f"predicted bounding box with text coordinates: {bbox}")
68
+ # safeguard in case text prediction is missing some bounding box coordinates
69
+ # or coordinates are not valid numeric values
70
+ try:
71
+ xmin = float(bbox.get("xmin", 0))
72
+ except ValueError:
73
+ xmin = 0
74
+ try:
75
+ ymin = float(bbox.get("ymin", 0))
76
+ except ValueError:
77
+ ymin = 0
78
+ try:
79
+ xmax = float(bbox.get("xmax", 1))
80
+ except ValueError:
81
+ xmax = 1
82
+ try:
83
+ ymax = float(bbox.get("ymax", 1))
84
+ except ValueError:
85
+ ymax = 1
86
+ # replace str with float coords
87
+ bbox = {"xmin": xmin, "ymin": ymin, "xmax": xmax,
88
+ "ymax": ymax, "decoder output sequence": sequence}
89
+ print(f"predicted bounding box with float coordinates: {bbox}")
90
 
91
  print(f"image object: {image}")
92
  print(f"image size: {image.size}")
 
95
  print(f"processed prompt: {prompt}")
96
 
97
  # safeguard in case text prediction is missing some bounding box coordinates
98
+ xmin = math.floor(width*bbox["xmin"])
99
+ ymin = math.floor(height*bbox["ymin"])
100
+ xmax = math.floor(width*bbox["xmax"])
101
+ ymax = math.floor(height*bbox["ymax"])
102
 
103
  print(
104
  f"to image pixel values: xmin, ymin, xmax, ymax: {xmin, ymin, xmax, ymax}")
105
 
106
  shape = [(xmin, ymin), (xmax, ymax)]
107
 
108
+ # deaw bbox rectangle
109
  img1 = ImageDraw.Draw(image)
110
  img1.rectangle(shape, outline="green", width=5)
111
+ img1.rectangle(shape, outline="white", width=2)
112
+
113
  return image, bbox
114
 
115
 
116
+ title = "Demo: Donut 🍩 for UI RefExp (by GuardianUI)"
117
  description = "Gradio Demo for Donut RefExp task, an instance of `VisionEncoderDecoderModel` fine-tuned on UIBert RefExp Dataset (UI Referring Expression). To use it, simply upload your image and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
118
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2111.15664' target='_blank'>Donut: OCR-free Document Understanding Transformer</a> | <a href='https://github.com/clovaai/donut' target='_blank'>Github Repo</a></p>"
119
  examples = [["example_1.jpg", "select the setting icon from top right corner"],