| """ |
| 转换一章数据 |
| """ |
|
|
| import os |
| import shutil |
| import json |
| import glob |
| from pathlib import Path |
| import cv2 |
| import numpy as np |
|
|
| from draw_box import do_drawbox |
|
|
|
|
| def main(): |
| dir_out2 = "out2" |
| if Path(dir_out2).exists(): |
| shutil.rmtree(dir_out2) |
|
|
| if not Path(dir_out2).exists(): |
| os.makedirs(dir_out2) |
|
|
| pageIds_pageNames = {} |
|
|
| raw_jsons = glob.glob('./out/raw/**/*.json', recursive=True) |
| box_jsons = {} |
| paragraphs = [] |
| texts = "" |
| text_pages = {} |
| pages = [] |
| for raw_json in raw_jsons: |
| if 'pages_200' in raw_json: |
| with open(raw_json, encoding='utf-8') as fp: |
| pages_json = json.load(fp) |
| page = pages_json["data"]["pages"] |
| pages += page |
| fp.close() |
| if 'paragraphs_200' in raw_json: |
| with open(raw_json, encoding='utf-8') as fp: |
| paragraphs_json = json.load(fp) |
| paragraphs += paragraphs_json["data"]["paragraphs"] |
| fp.close() |
| |
| if 'word_box_200' in raw_json: |
| with open(raw_json, encoding='utf-8') as fp: |
| word_box_json = json.load(fp) |
| box_jsons = {**box_jsons, **word_box_json["data"]["pageId2WordBoxContent"]} |
| fp.close() |
| |
| currPageId = -1 |
| lastPageId = -1 |
| currPageText = "" |
|
|
| for i, js in enumerate(paragraphs): |
| startPageId = js["startPageId"] |
| endPageId = js["endPageId"] |
| content = js["content"] |
| content = json.loads(content, strict=False ) |
| lines = content["lines"] |
| lastLineNum = -1 |
| for line in lines: |
| lineNum = line["lineNum"] |
| lineType = line["lineType"] |
| content_line = line["content"].replace('\ufeff', '').replace("\u3000", " ") |
| if 'pagePass' in line: |
| pagePass = line["pagePass"] |
| PageId = pagePass["PageId"] |
|
|
| if currPageId == -1: |
| currPageId = PageId |
|
|
| if currPageText: |
| text_pages[currPageId] = currPageText |
| currPageText = "" |
| currPageId = PageId |
|
|
| |
| currPageText += content_line |
|
|
| texts += content_line |
|
|
| if i == len(paragraphs) - 1: |
| if currPageText: |
| if currPageId not in text_pages: |
| text_pages[currPageId] = currPageText |
| currPageText = "" |
| currPageId = PageId |
| else: |
| currPageText += " " |
| texts += " " |
| |
|
|
| |
|
|
| for page in pages: |
| pageId = page["pageId"] |
| pageNum = page["pageNum"] |
| uri = page["uri"] |
| imageName = uri.split("-")[-1] |
| baseName = Path(imageName).stem |
| pth_img = Path("out/images") / imageName |
| if not pth_img.exists(): |
| if pth_img.stem in { |
| 'TPM0001_00006_00089b', 'TPM0001_00009_00002a', 'TPM0001_00009_00002b', |
| 'TPM0001_00009_00003a', 'TPM0001_00009_00003b', 'TPM0001_00009_00004a', |
| 'TPM0001_00009_00004b', 'TPM0001_00011_00011b', 'TPM0001_00011_00092b' |
| }: |
| continue |
| raise Exception(f"image {imageName} not found") |
| if pageId not in box_jsons: |
| raise Exception(f"pageId {pageId} no box_json") |
|
|
| pageIds_pageNames[pageId] = baseName |
| boxs = box_jsons[pageId]['wordBoxList'] |
| pth_boxs = str( dir_out2 / Path( imageName.replace(".webp", ".boxs") ) ) |
|
|
| shutil.copy(pth_img, str( dir_out2 / Path(imageName) ) ) |
| with open(pth_boxs, 'w', encoding='utf-8') as fp: |
| json.dump(boxs, fp, indent=4, ensure_ascii=False) |
| fp.close() |
|
|
| pth_text_pages = str(Path(dir_out2) / "page_texts.json") |
| with open(pth_text_pages, 'w', encoding='utf-8') as fp: |
| json.dump(text_pages, fp, indent=4, ensure_ascii=False) |
| fp.close() |
|
|
| pth_pageIds_pageNames = str(Path(dir_out2) / "pageIds_pageNames.json") |
| with open(pth_pageIds_pageNames, 'w', encoding='utf-8') as fp: |
| json.dump(pageIds_pageNames, fp, indent=4, ensure_ascii=False) |
| fp.close() |
|
|
|
|
| pth_paragraphs = str(Path(dir_out2) / "paragraphs.json") |
| with open(pth_paragraphs, 'w', encoding='utf-8') as fp: |
| json.dump(paragraphs, fp, indent=4, ensure_ascii=False) |
| fp.close() |
| |
| do_drawbox() |
|
|
| def do_gendata(): |
| main() |
|
|
| if __name__ == "__main__": |
| do_gendata() |
|
|