fasdfsa commited on
Commit
192db92
·
1 Parent(s): e129411
Files changed (4) hide show
  1. .gitignore +1 -1
  2. ocr/draw_box.py +3 -4
  3. ocr/gendatav2.py +119 -108
  4. ocr/getdata.py +4 -2
.gitignore CHANGED
@@ -1,4 +1,4 @@
1
-
2
  *.pdf
3
  !戚蓼生序本石头记.pdf
4
 
 
1
+ __pycache__/
2
  *.pdf
3
  !戚蓼生序本石头记.pdf
4
 
ocr/draw_box.py CHANGED
@@ -38,12 +38,11 @@ def draw_box(pth_webp, pth_boxs):
38
  path_jpg = pth_webp.replace(".webp", ".jpg")
39
  cv2.imwrite(path_jpg, img_color)
40
 
41
-
42
-
43
- if __name__ == "__main__":
44
  webps = glob.glob('./out2/*.webp', recursive=False)
45
  for pth_webp in webps:
46
  pth_box = pth_webp.replace(".webp", ".boxs")
47
  draw_box(pth_webp, pth_box)
48
 
49
- pass
 
 
38
  path_jpg = pth_webp.replace(".webp", ".jpg")
39
  cv2.imwrite(path_jpg, img_color)
40
 
41
+ def do_drawbox():
 
 
42
  webps = glob.glob('./out2/*.webp', recursive=False)
43
  for pth_webp in webps:
44
  pth_box = pth_webp.replace(".webp", ".boxs")
45
  draw_box(pth_webp, pth_box)
46
 
47
+ if __name__ == "__main__":
48
+ do_drawbox()
ocr/gendatav2.py CHANGED
@@ -10,119 +10,130 @@ from pathlib import Path
10
  import cv2
11
  import numpy as np
12
 
13
- dir_out2 = "out2"
14
- if Path(dir_out2).exists():
15
- shutil.rmtree(dir_out2)
16
-
17
- if not Path(dir_out2).exists():
18
- os.makedirs(dir_out2)
19
-
20
- pageIds_pageNames = {}
21
-
22
- raw_jsons = glob.glob('./out/raw/**/*.json', recursive=True)
23
- box_jsons = {}
24
- paragraphs = []
25
- texts = ""
26
- text_pages = {}
27
- pages = []
28
- for raw_json in raw_jsons:
29
- if 'pages_200' in raw_json:
30
- with open(raw_json, encoding='utf-8') as fp:
31
- pages_json = json.load(fp)
32
- page = pages_json["data"]["pages"]
33
- pages += page
34
- fp.close()
35
- if 'paragraphs_200' in raw_json:
36
- with open(raw_json, encoding='utf-8') as fp:
37
- paragraphs_json = json.load(fp)
38
- paragraphs += paragraphs_json["data"]["paragraphs"]
39
- fp.close()
40
-
41
- if 'word_box_200' in raw_json:
42
- with open(raw_json, encoding='utf-8') as fp:
43
- word_box_json = json.load(fp)
44
- box_jsons = {**box_jsons, **word_box_json["data"]["pageId2WordBoxContent"]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  fp.close()
46
-
47
- currPageId = -1
48
- lastPageId = -1
49
- currPageText = ""
50
-
51
- for i, js in enumerate(paragraphs):
52
- startPageId = js["startPageId"]
53
- endPageId = js["endPageId"]
54
- content = js["content"]
55
- content = json.loads(content, strict=False )
56
- lines = content["lines"]
57
- lastLineNum = -1
58
- for line in lines:
59
- lineNum = line["lineNum"]
60
- lineType = line["lineType"]
61
- content_line = line["content"].replace('\ufeff', '').replace("\u3000", " ") #.replace(" ", "")
62
- if 'pagePass' in line:
63
- pagePass = line["pagePass"]
64
- PageId = pagePass["PageId"]
65
-
66
- if currPageId == -1:
67
- currPageId = PageId
68
-
69
- if currPageText: # 页切换,保存上一页文本
70
- text_pages[currPageId] = currPageText
71
- currPageText = ""
72
- currPageId = PageId
73
 
74
-
75
- currPageText += content_line
76
-
77
- texts += content_line
78
-
79
- if i == len(paragraphs) - 1:
80
- if currPageText: # 最后一个段落,保存上一页文本
81
- if currPageId not in text_pages:
82
- text_pages[currPageId] = currPageText
83
- currPageText = ""
84
- currPageId = PageId
85
- else:
86
- currPageText += " "
87
- texts += " "
88
-
89
 
90
- # pages = pages_json["data"]["pages"]
91
-
92
- for page in pages:
93
- pageId = page["pageId"]
94
- pageNum = page["pageNum"]
95
- uri = page["uri"]
96
- imageName = uri.split("-")[-1]
97
- baseName = Path(imageName).stem
98
- pth_img = Path("out/images") / imageName
99
- if not pth_img.exists():
100
- raise Exception(f"image {imageName} not found")
101
- if pageId not in box_jsons:
102
- raise Exception(f"pageId {pageId} no box_json")
103
-
104
- pageIds_pageNames[pageId] = baseName
105
- boxs = box_jsons[pageId]['wordBoxList']
106
- pth_boxs = str( dir_out2 / Path( imageName.replace(".webp", ".boxs") ) )
107
-
108
- shutil.copy(pth_img, str( dir_out2 / Path(imageName) ) )
109
- with open(pth_boxs, 'w', encoding='utf-8') as fp:
110
- json.dump(boxs, fp, indent=4, ensure_ascii=False)
111
  fp.close()
112
 
113
- pth_text_pages = str(Path(dir_out2) / "page_texts.json")
114
- with open(pth_text_pages, 'w', encoding='utf-8') as fp:
115
- json.dump(text_pages, fp, indent=4, ensure_ascii=False)
116
- fp.close()
117
 
118
- pth_pageIds_pageNames = str(Path(dir_out2) / "pageIds_pageNames.json")
119
- with open(pth_pageIds_pageNames, 'w', encoding='utf-8') as fp:
120
- json.dump(pageIds_pageNames, fp, indent=4, ensure_ascii=False)
121
- fp.close()
 
 
122
 
 
 
123
 
124
- pth_paragraphs = str(Path(dir_out2) / "paragraphs.json")
125
- with open(pth_paragraphs, 'w', encoding='utf-8') as fp:
126
- json.dump(paragraphs, fp, indent=4, ensure_ascii=False)
127
- fp.close()
128
-
 
10
  import cv2
11
  import numpy as np
12
 
13
+ from draw_box import do_drawbox
14
+
15
+
16
+ def main():
17
+ dir_out2 = "out2"
18
+ if Path(dir_out2).exists():
19
+ shutil.rmtree(dir_out2)
20
+
21
+ if not Path(dir_out2).exists():
22
+ os.makedirs(dir_out2)
23
+
24
+ pageIds_pageNames = {}
25
+
26
+ raw_jsons = glob.glob('./out/raw/**/*.json', recursive=True)
27
+ box_jsons = {}
28
+ paragraphs = []
29
+ texts = ""
30
+ text_pages = {}
31
+ pages = []
32
+ for raw_json in raw_jsons:
33
+ if 'pages_200' in raw_json:
34
+ with open(raw_json, encoding='utf-8') as fp:
35
+ pages_json = json.load(fp)
36
+ page = pages_json["data"]["pages"]
37
+ pages += page
38
+ fp.close()
39
+ if 'paragraphs_200' in raw_json:
40
+ with open(raw_json, encoding='utf-8') as fp:
41
+ paragraphs_json = json.load(fp)
42
+ paragraphs += paragraphs_json["data"]["paragraphs"]
43
+ fp.close()
44
+
45
+ if 'word_box_200' in raw_json:
46
+ with open(raw_json, encoding='utf-8') as fp:
47
+ word_box_json = json.load(fp)
48
+ box_jsons = {**box_jsons, **word_box_json["data"]["pageId2WordBoxContent"]}
49
+ fp.close()
50
+
51
+ currPageId = -1
52
+ lastPageId = -1
53
+ currPageText = ""
54
+
55
+ for i, js in enumerate(paragraphs):
56
+ startPageId = js["startPageId"]
57
+ endPageId = js["endPageId"]
58
+ content = js["content"]
59
+ content = json.loads(content, strict=False )
60
+ lines = content["lines"]
61
+ lastLineNum = -1
62
+ for line in lines:
63
+ lineNum = line["lineNum"]
64
+ lineType = line["lineType"]
65
+ content_line = line["content"].replace('\ufeff', '').replace("\u3000", " ") #.replace(" ", "")
66
+ if 'pagePass' in line:
67
+ pagePass = line["pagePass"]
68
+ PageId = pagePass["PageId"]
69
+
70
+ if currPageId == -1:
71
+ currPageId = PageId
72
+
73
+ if currPageText: # 页切换,保存上一页文本
74
+ text_pages[currPageId] = currPageText
75
+ currPageText = ""
76
+ currPageId = PageId
77
+
78
+
79
+ currPageText += content_line
80
+
81
+ texts += content_line
82
+
83
+ if i == len(paragraphs) - 1:
84
+ if currPageText: # 最后一个段落,保存上一页文本
85
+ if currPageId not in text_pages:
86
+ text_pages[currPageId] = currPageText
87
+ currPageText = ""
88
+ currPageId = PageId
89
+ else:
90
+ currPageText += " "
91
+ texts += " "
92
+
93
+
94
+ # pages = pages_json["data"]["pages"]
95
+
96
+ for page in pages:
97
+ pageId = page["pageId"]
98
+ pageNum = page["pageNum"]
99
+ uri = page["uri"]
100
+ imageName = uri.split("-")[-1]
101
+ baseName = Path(imageName).stem
102
+ pth_img = Path("out/images") / imageName
103
+ if not pth_img.exists():
104
+ raise Exception(f"image {imageName} not found")
105
+ if pageId not in box_jsons:
106
+ raise Exception(f"pageId {pageId} no box_json")
107
+
108
+ pageIds_pageNames[pageId] = baseName
109
+ boxs = box_jsons[pageId]['wordBoxList']
110
+ pth_boxs = str( dir_out2 / Path( imageName.replace(".webp", ".boxs") ) )
111
+
112
+ shutil.copy(pth_img, str( dir_out2 / Path(imageName) ) )
113
+ with open(pth_boxs, 'w', encoding='utf-8') as fp:
114
+ json.dump(boxs, fp, indent=4, ensure_ascii=False)
115
  fp.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
+ pth_text_pages = str(Path(dir_out2) / "page_texts.json")
118
+ with open(pth_text_pages, 'w', encoding='utf-8') as fp:
119
+ json.dump(text_pages, fp, indent=4, ensure_ascii=False)
120
+ fp.close()
 
 
 
 
 
 
 
 
 
 
 
121
 
122
+ pth_pageIds_pageNames = str(Path(dir_out2) / "pageIds_pageNames.json")
123
+ with open(pth_pageIds_pageNames, 'w', encoding='utf-8') as fp:
124
+ json.dump(pageIds_pageNames, fp, indent=4, ensure_ascii=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  fp.close()
126
 
 
 
 
 
127
 
128
+ pth_paragraphs = str(Path(dir_out2) / "paragraphs.json")
129
+ with open(pth_paragraphs, 'w', encoding='utf-8') as fp:
130
+ json.dump(paragraphs, fp, indent=4, ensure_ascii=False)
131
+ fp.close()
132
+
133
+ do_drawbox()
134
 
135
+ def do_gendata():
136
+ main()
137
 
138
+ if __name__ == "__main__":
139
+ do_gendata()
 
 
 
ocr/getdata.py CHANGED
@@ -15,7 +15,6 @@ from selenium.webdriver.common.by import By
15
  from selenium.webdriver.support import expected_conditions as EC
16
  from selenium.webdriver.support.ui import WebDriverWait
17
 
18
-
19
  TARGET_PATTERNS = {
20
  "paragraphs": re.compile(r"/api/ancientlib/read/book/paragraphs/v2(?:\?|$)"),
21
  "pages": re.compile(r"/api/ancientlib/read/book/pages/v3/(?:\?|$)"),
@@ -376,7 +375,7 @@ def _enter_image_mode(driver: webdriver.Chrome) -> None:
376
  def main() -> int:
377
  ap = argparse.ArgumentParser()
378
  ap.add_argument("--book-id", default="SWX0005")
379
- ap.add_argument("--chapter", default="第回")
380
  ap.add_argument("--out", default="out")
381
  ap.add_argument("--headless", action="store_true")
382
  ap.add_argument("--timeout", type=int, default=20000)
@@ -662,6 +661,9 @@ def main() -> int:
662
  except Exception:
663
  pass
664
 
 
 
665
 
666
  if __name__ == "__main__":
667
  raise SystemExit(main())
 
 
15
  from selenium.webdriver.support import expected_conditions as EC
16
  from selenium.webdriver.support.ui import WebDriverWait
17
 
 
18
  TARGET_PATTERNS = {
19
  "paragraphs": re.compile(r"/api/ancientlib/read/book/paragraphs/v2(?:\?|$)"),
20
  "pages": re.compile(r"/api/ancientlib/read/book/pages/v3/(?:\?|$)"),
 
375
  def main() -> int:
376
  ap = argparse.ArgumentParser()
377
  ap.add_argument("--book-id", default="SWX0005")
378
+ ap.add_argument("--chapter", default="第十一回")
379
  ap.add_argument("--out", default="out")
380
  ap.add_argument("--headless", action="store_true")
381
  ap.add_argument("--timeout", type=int, default=20000)
 
661
  except Exception:
662
  pass
663
 
664
+ from gendatav2 import do_gendata
665
+ do_gendata()
666
 
667
  if __name__ == "__main__":
668
  raise SystemExit(main())
669
+