def get_xml(img, config_fname, checkpoint_path, epoch): """ img: Numpy array """ model = init_detector(config_fname, checkpoint_path + epoch) result = inference_detector(model, img) res_border = [] res_bless = [] res_cell = [] root = etree.Element("document") ## for border for r in result[0][0]: if r[4] > .85: res_border.append(r[:4].astype(int)) ## for cells for r in result[0][1]: if r[4] > .85: r[4] = r[4] * 100 res_cell.append(r.astype(int)) ## for borderless for r in result[0][2]: if r[4] > .85: res_bless.append(r[:4].astype(int)) ## if border tables detected if len(res_border) != 0: ## call border script for each table in image for res in res_border: try: root.append(border(res, cv2.imread(img))) except: pass if len(res_bless) != 0: if len(res_cell) != 0: for no, res in enumerate(res_bless): root.append(borderless(res, cv2.imread(img), res_cell)) buffer = io.StringIO() myfile = open(buffer, "w") myfile.write('<?xml version="1.0" encoding="UTF-8"?>\n') myfile.write(etree.tostring(root, pretty_print=True, encoding="unicode")) myfile.close() buffer.seek(0) return buffer
for r in result[0][0]: if r[4] > .85: res_border.append(r[:4].astype(int)) ## for cells for r in result[0][1]: if r[4] > .85: r[4] = r[4] * 100 res_cell.append(r.astype(int)) ## for borderless for r in result[0][2]: if r[4] > .85: res_bless.append(r[:4].astype(int)) ## if border tables detected if len(res_border) != 0: ## call border script for each table in image for res in res_border: try: root.append(border(res, cv2.imread(i))) except: pass if len(res_bless) != 0: if len(res_cell) != 0: for no, res in enumerate(res_bless): root.append(borderless(res, cv2.imread(i), res_cell)) myfile = open(xmlPath + i.split('/')[-1][:-3] + 'xml', "w") myfile.write('<?xml version="1.0" encoding="UTF-8"?>\n') myfile.write( etree.tostring(root, pretty_print=True, encoding="unicode")) myfile.close()
res_bless.append(r[:4].astype(int)) print("bless") print(res_bless) print("border") ## if border tables detected if len(res_border) != 0: ## call border script for each table in image print("for each table") for res in res_border: print("for res") try: print("try") cur_img = cv2.imread(i) print("img") root.append(border(res, cur_img)) print("border") except Exception as e: print(e) print('ex caught') pass print("border fin") if len(res_bless) != 0: if len(res_cell) != 0: for no, res in enumerate(res_bless): cur_img = cv2.imread(i) root.append(borderless(res, cur_img, res_cell)) print("myfile open") myfile = open(xmlPath + i.split('/')[-1][:-3] + 'xml', "w") myfile.write('<?xml version="1.0" encoding="UTF-8"?>\n') myfile.write(etree.tostring(root, pretty_print=True, encoding="unicode")) myfile.close()