import os import os.path as osp import json import random import transformers from tqdm import tqdm from multiprocessing import Pool from functools import partial from data_class import domNode, nodeBbox MAX_TOKEN_LEN = 800 MIN_TOKEN_LEN = 200 MAX_IoU = 0 HTML2BBOX_TEMPLATE = [ "Where is the given dom tree?", # Other template questions here... ] BBOX2HTML_TEMPLATE = [ "Can you give me a description of the region in image?", # Other template questions here... ] class NodewithHtml(domNode): def __init__(self, id, info=None, children=None, father=None, tokenizer=None, img_w=None, img_h=None, task=""): super().__init__(id, info, children, father, tokenizer, img_w=img_w, img_h=img_h, task="") self.dom_xml = self.get_dom_xml(mode="dom_xml") self.dom_xml_with_bbox = self.get_dom_xml(mode="dom_xml_with_bbox") def bbox2str(self) -> str: x1_rel = round(max(0, (self.info.bbox.x1 / IMG_W)) * 1000) y1_rel = round(max(0, (self.info.bbox.y1 / IMG_H)) * 1000) x2_rel = round(min(0.999, (self.info.bbox.x2 / IMG_W)) * 1000) y2_rel = round(min(0.999, (self.info.bbox.y2 / IMG_H)) * 1000) coords = [x1_rel, y1_rel, x2_rel, y2_rel] bbox_str = ["{:03}".format(coord) for coord in coords] bbox_str = "[" + ", ".join(bbox_str) + "]" return bbox_str def center2str(self) -> str: cx = round(max(0, min(0.999, self.info.bbox.cx / IMG_W)) * 1000) cy = round(max(0, min(0.999, self.info.bbox.cy / IMG_H)) * 1000) point = [cx, cy] point_str = ["{:03}".format(p) for p in point] point_str = "[" + ", ".join(point_str) + "]" return point_str def node2xml_withbbox(self) -> str: text = self.info.text bbox_str = self.bbox2str() if self.is_text() or self.has_single_text_child(): pseudo_html_line = f"" elif self.is_img(): if self.class_for_caption == 'img': pseudo_html_line = f"{text}" elif self.class_for_caption == 'svg': pseudo_html_line = f"" else: return "" elif self.info.func == 'type': pseudo_html_line = f"{text}" elif self.class_for_caption == 'svg': pseudo_html_line = f"" else: return "" elif self.info.func == 'type': pseudo_html_line = f"