Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

Merge remote-tracking branch 'origin/dev' into dev

myhlolie52bd023f75f
diff --cc magic_pdf/model/pdf_extract_kit.py
index f2adfadb,2e7de163..73aef175
--- a/magic_pdf/model/pdf_extract_kit.py
+++ b/magic_pdf/model/pdf_extract_kit.py
@@@ -112,19 -141,24 +142,23 @@@ class CustomPEKModel
self.layout_model = atom_model_manager.get_atom_model(
atom_model_name=AtomicModel.Layout,
layout_model_name=MODEL_NAME.DocLayout_YOLO,
- doclayout_yolo_weights=str(os.path.join(models_dir, self.configs['weights'][self.layout_model_name])),
- device=self.device
+ doclayout_yolo_weights=str(
+ os.path.join(
+ models_dir, self.configs['weights'][self.layout_model_name]
+ )
+ ),
+ device=self.device,
)
# 初始化ocr
- if self.apply_ocr:
- self.ocr_model = atom_model_manager.get_atom_model(
- atom_model_name=AtomicModel.OCR,
- ocr_show_log=show_log,
- det_db_box_thresh=0.3,
- lang=self.lang,
- )
+ self.ocr_model = atom_model_manager.get_atom_model(
+ atom_model_name=AtomicModel.OCR,
+ ocr_show_log=show_log,
+ det_db_box_thresh=0.3,
+ lang=self.lang
+ )
# init table model
if self.apply_table:
- table_model_dir = self.configs["weights"][self.table_model_name]
+ table_model_dir = self.configs['weights'][self.table_model_name]
self.table_model = atom_model_manager.get_atom_model(
atom_model_name=AtomicModel.Table,
table_model_name=self.table_model_name,
@@@ -170,32 -203,35 +203,34 @@@
clean_vram(self.device, vram_threshold=8)
# 从layout_res中获取ocr区域、表格区域、公式区域
- ocr_res_list, table_res_list, single_page_mfdetrec_res = get_res_list_from_layout_res(layout_res)
+ ocr_res_list, table_res_list, single_page_mfdetrec_res = (
+ get_res_list_from_layout_res(layout_res)
+ )
# ocr识别
+ ocr_start = time.time()
+ # Process each area that requires OCR processing
+ for res in ocr_res_list:
+ new_image, useful_list = crop_img(res, pil_img, crop_paste_x=50, crop_paste_y=50)
+ adjusted_mfdetrec_res = get_adjusted_mfdetrec_res(single_page_mfdetrec_res, useful_list)
+
+ # OCR recognition
+ new_image = cv2.cvtColor(np.asarray(new_image), cv2.COLOR_RGB2BGR)
+ if self.apply_ocr:
+ ocr_res = self.ocr_model.ocr(new_image, mfd_res=adjusted_mfdetrec_res)[0]
+ else:
+ ocr_res = self.ocr_model.ocr(new_image, mfd_res=adjusted_mfdetrec_res, rec=False)[0]
+
+ # Integration results
+ if ocr_res:
+ ocr_result_list = get_ocr_result_list(ocr_res, useful_list)
+ layout_res.extend(ocr_result_list)
+
+ ocr_cost = round(time.time() - ocr_start, 2)
if self.apply_ocr:
- ocr_start = time.time()
- # Process each area that requires OCR processing
- for res in ocr_res_list:
- new_image, useful_list = crop_img(
- res, pil_img, crop_paste_x=50, crop_paste_y=50
- )
- adjusted_mfdetrec_res = get_adjusted_mfdetrec_res(
- single_page_mfdetrec_res, useful_list
- )
-
- # OCR recognition
- new_image = cv2.cvtColor(np.asarray(new_image), cv2.COLOR_RGB2BGR)
- ocr_res = self.ocr_model.ocr(new_image, mfd_res=adjusted_mfdetrec_res)[
- 0
- ]
-
- # Integration results
- if ocr_res:
- ocr_result_list = get_ocr_result_list(ocr_res, useful_list)
- layout_res.extend(ocr_result_list)
-
- ocr_cost = round(time.time() - ocr_start, 2)
- logger.info(f'ocr time: {ocr_cost}')
+ logger.info(f"ocr time: {ocr_cost}")
+ else:
+ logger.info(f"det time: {ocr_cost}")
# 表格识别 table recognition
if self.apply_table: