MCPcopy
hub / github.com/microsoft/Magma / remove_overlap_new

Function remove_overlap_new

agents/ui_agent/util/utils.py:231–309  ·  view source on GitHub ↗

ocr_bbox format: [{'type': 'text', 'bbox':[x,y], 'interactivity':False, 'content':str }, ...] boxes format: [{'type': 'icon', 'bbox':[x,y], 'interactivity':True, 'content':None }, ...]

(boxes, iou_threshold, ocr_bbox=None)

Source from the content-addressed store, hash-verified

229
230
231def remove_overlap_new(boxes, iou_threshold, ocr_bbox=None):
232 '''
233 ocr_bbox format: [{'type': 'text', 'bbox':[x,y], 'interactivity':False, 'content':str }, ...]
234 boxes format: [{'type': 'icon', 'bbox':[x,y], 'interactivity':True, 'content':None }, ...]
235
236 '''
237 assert ocr_bbox is None or isinstance(ocr_bbox, List)
238
239 def box_area(box):
240 return (box[2] - box[0]) * (box[3] - box[1])
241
242 def intersection_area(box1, box2):
243 x1 = max(box1[0], box2[0])
244 y1 = max(box1[1], box2[1])
245 x2 = min(box1[2], box2[2])
246 y2 = min(box1[3], box2[3])
247 return max(0, x2 - x1) * max(0, y2 - y1)
248
249 def IoU(box1, box2):
250 intersection = intersection_area(box1, box2)
251 union = box_area(box1) + box_area(box2) - intersection + 1e-6
252 if box_area(box1) > 0 and box_area(box2) > 0:
253 ratio1 = intersection / box_area(box1)
254 ratio2 = intersection / box_area(box2)
255 else:
256 ratio1, ratio2 = 0, 0
257 return max(intersection / union, ratio1, ratio2)
258
259 def is_inside(box1, box2):
260 # return box1[0] >= box2[0] and box1[1] >= box2[1] and box1[2] <= box2[2] and box1[3] <= box2[3]
261 intersection = intersection_area(box1, box2)
262 ratio1 = intersection / box_area(box1)
263 return ratio1 > 0.80
264
265 # boxes = boxes.tolist()
266 filtered_boxes = []
267 if ocr_bbox:
268 filtered_boxes.extend(ocr_bbox)
269 # print('ocr_bbox!!!', ocr_bbox)
270 for i, box1_elem in enumerate(boxes):
271 box1 = box1_elem['bbox']
272 is_valid_box = True
273 for j, box2_elem in enumerate(boxes):
274 # keep the smaller box
275 box2 = box2_elem['bbox']
276 if i != j and IoU(box1, box2) > iou_threshold and box_area(box1) > box_area(box2):
277 is_valid_box = False
278 break
279 if is_valid_box:
280 if ocr_bbox:
281 # keep yolo boxes + prioritize ocr label
282 box_added = False
283 ocr_labels = ''
284 for box3_elem in ocr_bbox:
285 if not box_added:
286 box3 = box3_elem['bbox']
287 if is_inside(box3, box1): # ocr inside icon
288 # box_added = True

Callers 1

get_som_labeled_imgFunction · 0.85

Calls 3

is_insideFunction · 0.85
IoUFunction · 0.70
box_areaFunction · 0.70

Tested by

no test coverage detected