|
|
| |
|
|
| if __name__ == '__main__': |
|
|
| import glob |
| import os |
| import json |
| import numpy as np |
| from pathlib import Path |
| chs = glob.glob('data/ch*', recursive=False) |
| m5s = {} |
| for i, pth in enumerate(chs): |
| pth = pth.replace('\\', '/') |
| parts = pth.split('/') |
| chapter = parts[1] |
| |
| out2 = f'data/{chapter}/out2' |
|
|
|
|
| with open(f"{out2}/page_texts.json", "r", encoding='UTF-8') as fp: |
| page_texts = json.load(fp) |
|
|
| with open(f"{out2}/pageIds_pageNames.json", "r", encoding='UTF-8') as fp: |
| pageIds_pageNames = json.load(fp) |
|
|
| boxs = glob.glob(f"{out2}/*.boxs", recursive=False) |
|
|
| last_x = -1 |
| new_line = False |
| lines = [] |
| line = [] |
| for j, pt in enumerate(boxs): |
| pt_lines = pt.replace('.boxs', '.lines') |
| with open(pt, "r", encoding='UTF-8') as fp: |
| box = json.load(fp) |
|
|
| for bx in box: |
| charPoly = bx["charPoly"] |
| lu = [charPoly["x0"], charPoly["y0"]] |
| ru = [charPoly["x1"], charPoly["y1"]] |
| rd = [charPoly["x2"], charPoly["y2"]] |
| ld = [charPoly["x3"], charPoly["y3"]] |
| width = max(ru[0], rd[0]) - min(lu[0], ld[0]) |
| height = max( ld[1], rd[1] ) - min(lu[1], ru[1]) |
| points = np.array([lu, ru, rd, ld]) |
|
|
| if last_x == -1: |
| last_x = lu[0] |
| else: |
| x_diff = abs(lu[0] - last_x) |
| if x_diff > int(width / 2): |
| new_line = True |
| |
| if new_line: |
| lines.append(line) |
| line = [] |
| new_line = False |
|
|
| line.append(bx) |
| |
| |
| last_x = lu[0] |
|
|
|
|
| with open(pt_lines, 'w', encoding='utf-8') as fp: |
| json.dump(lines, fp, indent=4, ensure_ascii=False) |
| |
| print(f"box {j+1} / {len(boxs)}") |
|
|
| print(f"{chapter} {i+1} / {len(pths)}") |
|
|