magic_model.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. import json
  2. import math
  3. from magic_pdf.libs.commons import fitz
  4. from loguru import logger
  5. from magic_pdf.libs.commons import join_path
  6. from magic_pdf.libs.coordinate_transform import get_scale_ratio
  7. from magic_pdf.libs.ocr_content_type import ContentType
  8. from magic_pdf.rw.AbsReaderWriter import AbsReaderWriter
  9. from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
  10. from magic_pdf.libs.math import float_gt
  11. from magic_pdf.libs.boxbase import (
  12. _is_in,
  13. bbox_relative_pos,
  14. bbox_distance,
  15. _is_part_overlap,
  16. calculate_overlap_area_in_bbox1_area_ratio, calculate_iou,
  17. )
  18. from magic_pdf.libs.ModelBlockTypeEnum import ModelBlockTypeEnum
  19. CAPATION_OVERLAP_AREA_RATIO = 0.6
  20. class MagicModel:
  21. """
  22. 每个函数没有得到元素的时候返回空list
  23. """
  24. def __fix_axis(self):
  25. for model_page_info in self.__model_list:
  26. need_remove_list = []
  27. page_no = model_page_info["page_info"]["page_no"]
  28. horizontal_scale_ratio, vertical_scale_ratio = get_scale_ratio(
  29. model_page_info, self.__docs[page_no]
  30. )
  31. layout_dets = model_page_info["layout_dets"]
  32. for layout_det in layout_dets:
  33. x0, y0, _, _, x1, y1, _, _ = layout_det["poly"]
  34. bbox = [
  35. int(x0 / horizontal_scale_ratio),
  36. int(y0 / vertical_scale_ratio),
  37. int(x1 / horizontal_scale_ratio),
  38. int(y1 / vertical_scale_ratio),
  39. ]
  40. layout_det["bbox"] = bbox
  41. # 删除高度或者宽度小于等于0的spans
  42. if bbox[2] - bbox[0] <= 0 or bbox[3] - bbox[1] <= 0:
  43. need_remove_list.append(layout_det)
  44. for need_remove in need_remove_list:
  45. layout_dets.remove(need_remove)
  46. def __fix_by_remove_low_confidence(self):
  47. for model_page_info in self.__model_list:
  48. need_remove_list = []
  49. layout_dets = model_page_info["layout_dets"]
  50. for layout_det in layout_dets:
  51. if layout_det["score"] <= 0.05:
  52. need_remove_list.append(layout_det)
  53. else:
  54. continue
  55. for need_remove in need_remove_list:
  56. layout_dets.remove(need_remove)
  57. def __fix_by_remove_high_iou_and_low_confidence(self):
  58. for model_page_info in self.__model_list:
  59. need_remove_list = []
  60. layout_dets = model_page_info["layout_dets"]
  61. for layout_det1 in layout_dets:
  62. for layout_det2 in layout_dets:
  63. if layout_det1 == layout_det2:
  64. continue
  65. if layout_det1["category_id"] in [0,1,2,3,4,5,6,7,8,9] and layout_det2["category_id"] in [0,1,2,3,4,5,6,7,8,9]:
  66. if calculate_iou(layout_det1['bbox'], layout_det2['bbox']) > 0.9:
  67. if layout_det1['score'] < layout_det2['score']:
  68. layout_det_need_remove = layout_det1
  69. else:
  70. layout_det_need_remove = layout_det2
  71. if layout_det_need_remove not in need_remove_list:
  72. need_remove_list.append(layout_det_need_remove)
  73. else:
  74. continue
  75. else:
  76. continue
  77. for need_remove in need_remove_list:
  78. layout_dets.remove(need_remove)
  79. def __init__(self, model_list: list, docs: fitz.Document):
  80. self.__model_list = model_list
  81. self.__docs = docs
  82. '''为所有模型数据添加bbox信息(缩放,poly->bbox)'''
  83. self.__fix_axis()
  84. '''删除置信度特别低的模型数据(<0.05),提高质量'''
  85. self.__fix_by_remove_low_confidence()
  86. '''删除高iou(>0.9)数据中置信度较低的那个'''
  87. self.__fix_by_remove_high_iou_and_low_confidence()
  88. def __reduct_overlap(self, bboxes):
  89. N = len(bboxes)
  90. keep = [True] * N
  91. for i in range(N):
  92. for j in range(N):
  93. if i == j:
  94. continue
  95. if _is_in(bboxes[i]["bbox"], bboxes[j]["bbox"]):
  96. keep[i] = False
  97. return [bboxes[i] for i in range(N) if keep[i]]
  98. def __tie_up_category_by_distance(
  99. self, page_no, subject_category_id, object_category_id
  100. ):
  101. """
  102. 假定每个 subject 最多有一个 object (可以有多个相邻的 object 合并为单个 object),每个 object 只能属于一个 subject
  103. """
  104. ret = []
  105. MAX_DIS_OF_POINT = 10**9 + 7
  106. def expand_bbox(bbox1, bbox2):
  107. x0 = min(bbox1[0], bbox2[0])
  108. y0 = min(bbox1[1], bbox2[1])
  109. x1 = max(bbox1[2], bbox2[2])
  110. y1 = max(bbox1[3], bbox2[3])
  111. return [x0, y0, x1, y1]
  112. def get_bbox_area(bbox):
  113. return abs(bbox[2] - bbox[0]) * abs(bbox[3] - bbox[1])
  114. # subject 和 object 的 bbox 会合并成一个大的 bbox (named: merged bbox)。 筛选出所有和 merged bbox 有 overlap 且 overlap 面积大于 object 的面积的 subjects。
  115. # 再求出筛选出的 subjects 和 object 的最短距离!
  116. def may_find_other_nearest_bbox(subject_idx, object_idx):
  117. ret = float("inf")
  118. x0 = min(
  119. all_bboxes[subject_idx]["bbox"][0], all_bboxes[object_idx]["bbox"][0]
  120. )
  121. y0 = min(
  122. all_bboxes[subject_idx]["bbox"][1], all_bboxes[object_idx]["bbox"][1]
  123. )
  124. x1 = max(
  125. all_bboxes[subject_idx]["bbox"][2], all_bboxes[object_idx]["bbox"][2]
  126. )
  127. y1 = max(
  128. all_bboxes[subject_idx]["bbox"][3], all_bboxes[object_idx]["bbox"][3]
  129. )
  130. object_area = abs(
  131. all_bboxes[object_idx]["bbox"][2] - all_bboxes[object_idx]["bbox"][0]
  132. ) * abs(
  133. all_bboxes[object_idx]["bbox"][3] - all_bboxes[object_idx]["bbox"][1]
  134. )
  135. for i in range(len(all_bboxes)):
  136. if (
  137. i == subject_idx
  138. or all_bboxes[i]["category_id"] != subject_category_id
  139. ):
  140. continue
  141. if _is_part_overlap([x0, y0, x1, y1], all_bboxes[i]["bbox"]) or _is_in(
  142. all_bboxes[i]["bbox"], [x0, y0, x1, y1]
  143. ):
  144. i_area = abs(
  145. all_bboxes[i]["bbox"][2] - all_bboxes[i]["bbox"][0]
  146. ) * abs(all_bboxes[i]["bbox"][3] - all_bboxes[i]["bbox"][1])
  147. if i_area >= object_area:
  148. ret = min(float("inf"), dis[i][object_idx])
  149. return ret
  150. subjects = self.__reduct_overlap(
  151. list(
  152. map(
  153. lambda x: {"bbox": x["bbox"], "score": x["score"]},
  154. filter(
  155. lambda x: x["category_id"] == subject_category_id,
  156. self.__model_list[page_no]["layout_dets"],
  157. ),
  158. )
  159. )
  160. )
  161. objects = self.__reduct_overlap(
  162. list(
  163. map(
  164. lambda x: {"bbox": x["bbox"], "score": x["score"]},
  165. filter(
  166. lambda x: x["category_id"] == object_category_id,
  167. self.__model_list[page_no]["layout_dets"],
  168. ),
  169. )
  170. )
  171. )
  172. subject_object_relation_map = {}
  173. subjects.sort(
  174. key=lambda x: x["bbox"][0] ** 2 + x["bbox"][1] ** 2
  175. ) # get the distance !
  176. all_bboxes = []
  177. for v in subjects:
  178. all_bboxes.append(
  179. {
  180. "category_id": subject_category_id,
  181. "bbox": v["bbox"],
  182. "score": v["score"],
  183. }
  184. )
  185. for v in objects:
  186. all_bboxes.append(
  187. {
  188. "category_id": object_category_id,
  189. "bbox": v["bbox"],
  190. "score": v["score"],
  191. }
  192. )
  193. N = len(all_bboxes)
  194. dis = [[MAX_DIS_OF_POINT] * N for _ in range(N)]
  195. for i in range(N):
  196. for j in range(i):
  197. if (
  198. all_bboxes[i]["category_id"] == subject_category_id
  199. and all_bboxes[j]["category_id"] == subject_category_id
  200. ):
  201. continue
  202. dis[i][j] = bbox_distance(all_bboxes[i]["bbox"], all_bboxes[j]["bbox"])
  203. dis[j][i] = dis[i][j]
  204. used = set()
  205. for i in range(N):
  206. # 求第 i 个 subject 所关联的 object
  207. if all_bboxes[i]["category_id"] != subject_category_id:
  208. continue
  209. seen = set()
  210. candidates = []
  211. arr = []
  212. for j in range(N):
  213. pos_flag_count = sum(
  214. list(
  215. map(
  216. lambda x: 1 if x else 0,
  217. bbox_relative_pos(
  218. all_bboxes[i]["bbox"], all_bboxes[j]["bbox"]
  219. ),
  220. )
  221. )
  222. )
  223. if pos_flag_count > 1:
  224. continue
  225. if (
  226. all_bboxes[j]["category_id"] != object_category_id
  227. or j in used
  228. or dis[i][j] == MAX_DIS_OF_POINT
  229. ):
  230. continue
  231. left, right, _, _ = bbox_relative_pos(all_bboxes[i]["bbox"], all_bboxes[j]["bbox"]) # 由 pos_flag_count 相关逻辑保证本段逻辑准确性
  232. if left or right:
  233. one_way_dis = all_bboxes[i]["bbox"][2] - all_bboxes[i]["bbox"][0]
  234. else:
  235. one_way_dis = all_bboxes[i]["bbox"][3] - all_bboxes[i]["bbox"][1]
  236. if dis[i][j] > one_way_dis:
  237. continue
  238. arr.append((dis[i][j], j))
  239. arr.sort(key=lambda x: x[0])
  240. if len(arr) > 0:
  241. # bug: 离该subject 最近的 object 可能跨越了其它的 subject 。比如 [this subect] [some sbuject] [the nearest objec of subject]
  242. if may_find_other_nearest_bbox(i, arr[0][1]) >= arr[0][0]:
  243. candidates.append(arr[0][1])
  244. seen.add(arr[0][1])
  245. # 已经获取初始种子
  246. for j in set(candidates):
  247. tmp = []
  248. for k in range(i + 1, N):
  249. pos_flag_count = sum(
  250. list(
  251. map(
  252. lambda x: 1 if x else 0,
  253. bbox_relative_pos(
  254. all_bboxes[j]["bbox"], all_bboxes[k]["bbox"]
  255. ),
  256. )
  257. )
  258. )
  259. if pos_flag_count > 1:
  260. continue
  261. if (
  262. all_bboxes[k]["category_id"] != object_category_id
  263. or k in used
  264. or k in seen
  265. or dis[j][k] == MAX_DIS_OF_POINT
  266. or dis[j][k] > dis[i][j]
  267. ):
  268. continue
  269. is_nearest = True
  270. for l in range(i + 1, N):
  271. if l in (j, k) or l in used or l in seen:
  272. continue
  273. if not float_gt(dis[l][k], dis[j][k]):
  274. is_nearest = False
  275. break
  276. if is_nearest:
  277. tmp.append(k)
  278. seen.add(k)
  279. candidates = tmp
  280. if len(candidates) == 0:
  281. break
  282. # 已经获取到某个 figure 下所有的最靠近的 captions,以及最靠近这些 captions 的 captions 。
  283. # 先扩一下 bbox,
  284. x0s = [all_bboxes[idx]["bbox"][0] for idx in seen] + [
  285. all_bboxes[i]["bbox"][0]
  286. ]
  287. y0s = [all_bboxes[idx]["bbox"][1] for idx in seen] + [
  288. all_bboxes[i]["bbox"][1]
  289. ]
  290. x1s = [all_bboxes[idx]["bbox"][2] for idx in seen] + [
  291. all_bboxes[i]["bbox"][2]
  292. ]
  293. y1s = [all_bboxes[idx]["bbox"][3] for idx in seen] + [
  294. all_bboxes[i]["bbox"][3]
  295. ]
  296. ox0, oy0, ox1, oy1 = min(x0s), min(y0s), max(x1s), max(y1s)
  297. ix0, iy0, ix1, iy1 = all_bboxes[i]["bbox"]
  298. # 分成了 4 个截取空间,需要计算落在每个截取空间下 objects 合并后占据的矩形面积
  299. caption_poses = [
  300. [ox0, oy0, ix0, oy1],
  301. [ox0, oy0, ox1, iy0],
  302. [ox0, iy1, ox1, oy1],
  303. [ix1, oy0, ox1, oy1],
  304. ]
  305. caption_areas = []
  306. for bbox in caption_poses:
  307. embed_arr = []
  308. for idx in seen:
  309. if (
  310. calculate_overlap_area_in_bbox1_area_ratio(
  311. all_bboxes[idx]["bbox"], bbox
  312. )
  313. > CAPATION_OVERLAP_AREA_RATIO
  314. ):
  315. embed_arr.append(idx)
  316. if len(embed_arr) > 0:
  317. embed_x0 = min([all_bboxes[idx]["bbox"][0] for idx in embed_arr])
  318. embed_y0 = min([all_bboxes[idx]["bbox"][1] for idx in embed_arr])
  319. embed_x1 = max([all_bboxes[idx]["bbox"][2] for idx in embed_arr])
  320. embed_y1 = max([all_bboxes[idx]["bbox"][3] for idx in embed_arr])
  321. caption_areas.append(
  322. int(abs(embed_x1 - embed_x0) * abs(embed_y1 - embed_y0))
  323. )
  324. else:
  325. caption_areas.append(0)
  326. subject_object_relation_map[i] = []
  327. if max(caption_areas) > 0:
  328. max_area_idx = caption_areas.index(max(caption_areas))
  329. caption_bbox = caption_poses[max_area_idx]
  330. for j in seen:
  331. if (
  332. calculate_overlap_area_in_bbox1_area_ratio(
  333. all_bboxes[j]["bbox"], caption_bbox
  334. )
  335. > CAPATION_OVERLAP_AREA_RATIO
  336. ):
  337. used.add(j)
  338. subject_object_relation_map[i].append(j)
  339. for i in sorted(subject_object_relation_map.keys()):
  340. result = {
  341. "subject_body": all_bboxes[i]["bbox"],
  342. "all": all_bboxes[i]["bbox"],
  343. "score": all_bboxes[i]["score"],
  344. }
  345. if len(subject_object_relation_map[i]) > 0:
  346. x0 = min(
  347. [all_bboxes[j]["bbox"][0] for j in subject_object_relation_map[i]]
  348. )
  349. y0 = min(
  350. [all_bboxes[j]["bbox"][1] for j in subject_object_relation_map[i]]
  351. )
  352. x1 = max(
  353. [all_bboxes[j]["bbox"][2] for j in subject_object_relation_map[i]]
  354. )
  355. y1 = max(
  356. [all_bboxes[j]["bbox"][3] for j in subject_object_relation_map[i]]
  357. )
  358. result["object_body"] = [x0, y0, x1, y1]
  359. result["all"] = [
  360. min(x0, all_bboxes[i]["bbox"][0]),
  361. min(y0, all_bboxes[i]["bbox"][1]),
  362. max(x1, all_bboxes[i]["bbox"][2]),
  363. max(y1, all_bboxes[i]["bbox"][3]),
  364. ]
  365. ret.append(result)
  366. total_subject_object_dis = 0
  367. # 计算已经配对的 distance 距离
  368. for i in subject_object_relation_map.keys():
  369. for j in subject_object_relation_map[i]:
  370. total_subject_object_dis += bbox_distance(
  371. all_bboxes[i]["bbox"], all_bboxes[j]["bbox"]
  372. )
  373. # 计算未匹配的 subject 和 object 的距离(非精确版)
  374. with_caption_subject = set(
  375. [
  376. key
  377. for key in subject_object_relation_map.keys()
  378. if len(subject_object_relation_map[i]) > 0
  379. ]
  380. )
  381. for i in range(N):
  382. if all_bboxes[i]["category_id"] != object_category_id or i in used:
  383. continue
  384. candidates = []
  385. for j in range(N):
  386. if (
  387. all_bboxes[j]["category_id"] != subject_category_id
  388. or j in with_caption_subject
  389. ):
  390. continue
  391. candidates.append((dis[i][j], j))
  392. if len(candidates) > 0:
  393. candidates.sort(key=lambda x: x[0])
  394. total_subject_object_dis += candidates[0][1]
  395. with_caption_subject.add(j)
  396. return ret, total_subject_object_dis
  397. def get_imgs(self, page_no: int): # @许瑞
  398. records, _ = self.__tie_up_category_by_distance(page_no, 3, 4)
  399. return [
  400. {
  401. "bbox": record["all"],
  402. "img_body_bbox": record["subject_body"],
  403. "img_caption_bbox": record.get("object_body", None),
  404. "score": record["score"],
  405. }
  406. for record in records
  407. ]
  408. def get_tables(
  409. self, page_no: int
  410. ) -> list: # 3个坐标, caption, table主体,table-note
  411. with_captions, _ = self.__tie_up_category_by_distance(page_no, 5, 6)
  412. with_footnotes, _ = self.__tie_up_category_by_distance(page_no, 5, 7)
  413. ret = []
  414. N, M = len(with_captions), len(with_footnotes)
  415. assert N == M
  416. for i in range(N):
  417. record = {
  418. "score": with_captions[i]["score"],
  419. "table_caption_bbox": with_captions[i].get("object_body", None),
  420. "table_body_bbox": with_captions[i]["subject_body"],
  421. "table_footnote_bbox": with_footnotes[i].get("object_body", None),
  422. }
  423. x0 = min(with_captions[i]["all"][0], with_footnotes[i]["all"][0])
  424. y0 = min(with_captions[i]["all"][1], with_footnotes[i]["all"][1])
  425. x1 = max(with_captions[i]["all"][2], with_footnotes[i]["all"][2])
  426. y1 = max(with_captions[i]["all"][3], with_footnotes[i]["all"][3])
  427. record["bbox"] = [x0, y0, x1, y1]
  428. ret.append(record)
  429. return ret
  430. def get_equations(self, page_no: int) -> list: # 有坐标,也有字
  431. inline_equations = self.__get_blocks_by_type(
  432. ModelBlockTypeEnum.EMBEDDING.value, page_no, ["latex"]
  433. )
  434. interline_equations = self.__get_blocks_by_type(
  435. ModelBlockTypeEnum.ISOLATED.value, page_no, ["latex"]
  436. )
  437. interline_equations_blocks = self.__get_blocks_by_type(
  438. ModelBlockTypeEnum.ISOLATE_FORMULA.value, page_no
  439. )
  440. return inline_equations, interline_equations, interline_equations_blocks
  441. def get_discarded(self, page_no: int) -> list: # 自研模型,只有坐标
  442. blocks = self.__get_blocks_by_type(ModelBlockTypeEnum.ABANDON.value, page_no)
  443. return blocks
  444. def get_text_blocks(self, page_no: int) -> list: # 自研模型搞的,只有坐标,没有字
  445. blocks = self.__get_blocks_by_type(ModelBlockTypeEnum.PLAIN_TEXT.value, page_no)
  446. return blocks
  447. def get_title_blocks(self, page_no: int) -> list: # 自研模型,只有坐标,没字
  448. blocks = self.__get_blocks_by_type(ModelBlockTypeEnum.TITLE.value, page_no)
  449. return blocks
  450. def get_ocr_text(self, page_no: int) -> list: # paddle 搞的,有字也有坐标
  451. text_spans = []
  452. model_page_info = self.__model_list[page_no]
  453. layout_dets = model_page_info["layout_dets"]
  454. for layout_det in layout_dets:
  455. if layout_det["category_id"] == "15":
  456. span = {
  457. "bbox": layout_det["bbox"],
  458. "content": layout_det["text"],
  459. }
  460. text_spans.append(span)
  461. return text_spans
  462. def get_all_spans(self, page_no: int) -> list:
  463. def remove_duplicate_spans(spans):
  464. new_spans = []
  465. for span in spans:
  466. if not any(span == existing_span for existing_span in new_spans):
  467. new_spans.append(span)
  468. return new_spans
  469. all_spans = []
  470. model_page_info = self.__model_list[page_no]
  471. layout_dets = model_page_info["layout_dets"]
  472. allow_category_id_list = [3, 5, 13, 14, 15]
  473. """当成span拼接的"""
  474. # 3: 'image', # 图片
  475. # 5: 'table', # 表格
  476. # 13: 'inline_equation', # 行内公式
  477. # 14: 'interline_equation', # 行间公式
  478. # 15: 'text', # ocr识别文本
  479. for layout_det in layout_dets:
  480. category_id = layout_det["category_id"]
  481. if category_id in allow_category_id_list:
  482. span = {
  483. "bbox": layout_det["bbox"],
  484. "score": layout_det["score"]
  485. }
  486. if category_id == 3:
  487. span["type"] = ContentType.Image
  488. elif category_id == 5:
  489. span["type"] = ContentType.Table
  490. elif category_id == 13:
  491. span["content"] = layout_det["latex"]
  492. span["type"] = ContentType.InlineEquation
  493. elif category_id == 14:
  494. span["content"] = layout_det["latex"]
  495. span["type"] = ContentType.InterlineEquation
  496. elif category_id == 15:
  497. span["content"] = layout_det["text"]
  498. span["type"] = ContentType.Text
  499. all_spans.append(span)
  500. return remove_duplicate_spans(all_spans)
  501. def get_page_size(self, page_no: int): # 获取页面宽高
  502. # 获取当前页的page对象
  503. page = self.__docs[page_no]
  504. # 获取当前页的宽高
  505. page_w = page.rect.width
  506. page_h = page.rect.height
  507. return page_w, page_h
  508. def __get_blocks_by_type(
  509. self, type: int, page_no: int, extra_col: list[str] = []
  510. ) -> list:
  511. blocks = []
  512. for page_dict in self.__model_list:
  513. layout_dets = page_dict.get("layout_dets", [])
  514. page_info = page_dict.get("page_info", {})
  515. page_number = page_info.get("page_no", -1)
  516. if page_no != page_number:
  517. continue
  518. for item in layout_dets:
  519. category_id = item.get("category_id", -1)
  520. bbox = item.get("bbox", None)
  521. if category_id == type:
  522. block = {
  523. "bbox": bbox,
  524. "score": item.get("score"),
  525. }
  526. for col in extra_col:
  527. block[col] = item.get(col, None)
  528. blocks.append(block)
  529. return blocks
  530. def get_model_list(self, page_no):
  531. return self.__model_list[page_no]
  532. if __name__ == "__main__":
  533. drw = DiskReaderWriter(r"D:/project/20231108code-clean")
  534. if 0:
  535. pdf_file_path = r"linshixuqiu\19983-00.pdf"
  536. model_file_path = r"linshixuqiu\19983-00_new.json"
  537. pdf_bytes = drw.read(pdf_file_path, AbsReaderWriter.MODE_BIN)
  538. model_json_txt = drw.read(model_file_path, AbsReaderWriter.MODE_TXT)
  539. model_list = json.loads(model_json_txt)
  540. write_path = r"D:\project\20231108code-clean\linshixuqiu\19983-00"
  541. img_bucket_path = "imgs"
  542. img_writer = DiskReaderWriter(join_path(write_path, img_bucket_path))
  543. pdf_docs = fitz.open("pdf", pdf_bytes)
  544. magic_model = MagicModel(model_list, pdf_docs)
  545. if 1:
  546. model_list = json.loads(
  547. drw.read("/opt/data/pdf/20240418/j.chroma.2009.03.042.json")
  548. )
  549. pdf_bytes = drw.read(
  550. "/opt/data/pdf/20240418/j.chroma.2009.03.042.pdf", AbsReaderWriter.MODE_BIN
  551. )
  552. pdf_docs = fitz.open("pdf", pdf_bytes)
  553. magic_model = MagicModel(model_list, pdf_docs)
  554. for i in range(7):
  555. print(magic_model.get_imgs(i))