File size: 5,023 Bytes
e41d92c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import numpy as np
IMG_FLAG = "img"
TEXT_FLAG = "text"
class nodeBbox:
def __init__(self, img_w, img_h, left=0, top=0, width=0, height=0) -> None:
assert img_w is not None
assert img_h is not None
self.left: int = round(left)
self.top: int = round(top)
self.width: int = round(width)
self.height: int = round(height)
self.x1: int = self.left
self.y1: int = self.top
self.x2: int = self.left + self.width
self.y2: int = self.top + self.height
self.cx: int = round((self.x1 + self.x2) / 2)
self.cy: int = round((self.y1 + self.y2) / 2)
self.is_valid: bool = True
if not (0 <= self.x1 <= img_w and 0 <= self.y1 <= img_h and 0 <= self.x2 <= img_w and 0 <= self.y2 <= img_h):
self.is_valid = False
if self.x1 >= self.x2 or self.y1 >= self.y2:
self.is_valid = False
def get_max_IoU(self, others) -> float:
other_bbox = np.array([[o.x1, o.y1, o.x2, o.y2] for o in others])
other_bbox[:, 0:1] = np.clip(other_bbox[:, 0:1], a_min=self.x1, a_max=None)
other_bbox[:, 1:2] = np.clip(other_bbox[:, 1:2], a_min=self.y1, a_max=None)
other_bbox[:, 2:3] = np.clip(other_bbox[:, 2:3], a_min=None, a_max=self.x2)
other_bbox[:, 3:4] = np.clip(other_bbox[:, 3:4], a_min=None, a_max=self.y2)
inter_w = np.clip(other_bbox[:, 2] - other_bbox[:, 0], a_min=0, a_max=None)
inter_h = np.clip(other_bbox[:, 3] - other_bbox[:, 1], a_min=0, a_max=None)
IoU = inter_w * inter_h
return np.max(IoU)
class nodeInfo:
def __init__(self, name=None, type=None, func=None, text=None, title=None, bbox=None, point=None, pointer=False, tokenizer=None, img_w=None, img_h=None) -> None:
dom_name = name if isinstance(name, str) else ""
dom_type = type if isinstance(type, str) else ""
dom_func = func if isinstance(func, str) else ""
dom_text = text if isinstance(text, str) else ""
dom_title = title if isinstance(title, str) else ""
dom_bbox = nodeBbox(img_w=img_w, img_h=img_h, **bbox) if isinstance(bbox, dict) else nodeBbox()
dom_point = point if isinstance(point, dict) else {"x": 0, "y": 0}
dom_pointer = pointer if isinstance(pointer, bool) else False
is_valid = dom_bbox.is_valid
self.name: str = dom_name.lower()
self.type: str = dom_type
self.func: str = dom_func
self.text: str = dom_text
self.title: str = dom_title
self.bbox: nodeBbox = dom_bbox
self.point: dict = dom_point
self.pointer: bool = dom_pointer
self.is_valid: bool = is_valid
class domNode:
def __init__(self, id, info, children, father=None, tokenizer=None, img_w=None, img_h=None, task=""):
self.id: str = id
self.info: nodeInfo = nodeInfo(img_w=img_w, img_h=img_h, tokenizer=tokenizer, **info)
self.father: domNode = father
is_valid = self.info.is_valid
if self.is_img() and not self.info.text:
is_valid = False
children_nodes = []
for child in children:
child_node = type(self)(father=self, tokenizer=tokenizer, img_w=img_w, img_h=img_h, task=task, **child)
children_nodes.append(child_node)
if not child_node.is_valid:
is_valid = False
self.children: list[domNode] = children_nodes
self.is_valid: bool = is_valid
for child in self.children:
if child.is_leaf() and child.info.type == 'text' and self.info.func == 'text':
self.info.func = 'plain'
break
self.class_for_caption: str = ""
if self.info.name in ['a', 'button']:
self.class_for_caption = self.info.name
elif self.info.name == 'input':
if self.info.func == 'click':
self.class_for_caption = 'button'
elif self.info.func == 'type':
self.class_for_caption = 'input'
elif self.img_type():
if self.img_type() in ['img', 'bgimg']:
self.class_for_caption = 'img'
elif self.img_type() in ['svg', 'fa']:
self.class_for_caption = 'svg'
elif self.is_text():
self.class_for_caption = 'text'
def is_leaf(self) -> bool:
return len(self.children) == 0
def has_single_text_child(self) -> bool:
return len(self.children) == 1 and self.children[0].info.type == 'text'
def is_func_leaf(self) -> bool:
if self.is_leaf():
return not self.is_text()
return self.has_single_text_child()
def is_text(self) -> bool:
return self.info.type == TEXT_FLAG
def is_img(self) -> bool:
return self.info.type.startswith(IMG_FLAG)
def img_type(self) -> str:
if not self.is_img():
return ""
return self.info.type[len(IMG_FLAG) + 1: -1]
|