|
|
| """ |
| 转换一章数据 |
| """ |
|
|
| import json |
| import glob |
| from pathlib import Path |
| import cv2 |
| import numpy as np |
|
|
|
|
| pth_txt = "out/text/text.txt" |
|
|
|
|
| PageIds = [] |
|
|
| with open(pth_txt, 'r', encoding='utf-8') as f: |
| for txtline in f: |
| txtline = txtline.strip() |
| if not txtline: |
| continue |
| data = json.loads(txtline) |
| lines = data['lines'] |
| for line in lines: |
| if "pagePass" in line: |
| PageId = line["pagePass"]["PageId"] |
| PageIds.append(PageId) |
|
|
|
|
| dic = {} |
| dic_img = {} |
| num = 0 |
| charTexts = "" |
| for PageId in PageIds: |
| print(PageId) |
| paths = glob.glob(f'./out/coords/*.json', recursive=False) |
| for pth_json in paths: |
| with open(pth_json, "r", encoding="utf-8") as fp: |
|
|
| stem = Path(pth_json).stem |
| image_id = stem.split('_')[-1] |
|
|
| txt = fp.read() |
| data = json.loads(txt) |
| pageId2WordBoxContent = data["data"]["pageId2WordBoxContent"] |
| if PageId not in pageId2WordBoxContent: |
| continue |
|
|
| wordBoxContents = pageId2WordBoxContent[PageId] |
|
|
|
|
|
|
| if PageId not in dic: |
| dic[PageId] = True |
| else: |
| continue |
|
|
|
|
|
|
| images = glob.glob(f"./out/images/*{image_id}.webp", recursive=False) |
| assert len(images) > 0 |
| pth_img = images[0] |
| pth_img = str( Path(pth_img).resolve() ) |
|
|
|
|
|
|
| new_img = False |
| if PageId not in dic_img: |
| new_img = True |
| img = cv2.imread(pth_img) |
| if len(img.shape) != 3: |
| img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) |
| img_color = img.copy() |
| dic_img[PageId] = { "img_color": img_color, "img": img } |
| else: |
| img = dic_img[PageId]["img"] |
| img_color = dic_img[PageId]["img_color"] |
|
|
|
|
|
|
| |
|
|
| |
| wordBoxList = wordBoxContents["wordBoxList"] |
| for wordBox in wordBoxList: |
| if "charPoly" not in wordBox: |
| continue |
| charPoly = wordBox["charPoly"] |
| lu = [ charPoly['x0'], charPoly['y0'] ] |
| ru = [ charPoly['x1'], charPoly['y1'] ] |
| rb = [ charPoly['x2'], charPoly['y2'] ] |
| lb = [ charPoly['x3'], charPoly['y3'] ] |
| points = [lu, ru, rb, lb] |
| points = np.array(points) |
| cv2.polylines(img_color, [points], isClosed=True, color=(255, 0, 0), thickness=1) |
|
|
| charText = wordBox["charText"] |
| charTexts += charText |
| print(charText, end='') |
|
|
| charTexts += "\n\n\n" |
| print("\n\n\n") |
|
|
|
|
| if new_img: |
| cv2.imwrite(f"./out2/{num:04d}_{image_id}.webp", img_color) |
| num += 1 |
|
|
| break |
|
|
| with open("out.txt", 'w', encoding='utf-8') as f: |
| f.write(charTexts) |
|
|
| pass |
|
|