utils.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. import os
  3. from pathlib import Path
  4. import yaml
  5. from PIL import Image
  6. os.environ['NO_ALBUMENTATIONS_UPDATE'] = '1' # 禁止albumentations检查更新
  7. from magic_pdf.config.constants import MODEL_NAME
  8. from magic_pdf.data.utils import load_images_from_pdf
  9. from magic_pdf.libs.config_reader import get_local_models_dir, get_device
  10. from magic_pdf.libs.pdf_check import extract_pages
  11. from magic_pdf.model.model_list import AtomicModel
  12. from magic_pdf.model.sub_modules.model_init import AtomModelSingleton
  13. def get_model_config():
  14. local_models_dir = get_local_models_dir()
  15. device = get_device()
  16. current_file_path = os.path.abspath(__file__)
  17. root_dir = Path(current_file_path).parents[3]
  18. model_config_dir = os.path.join(root_dir, 'resources', 'model_config')
  19. config_path = os.path.join(model_config_dir, 'model_configs.yaml')
  20. with open(config_path, 'r', encoding='utf-8') as f:
  21. configs = yaml.load(f, Loader=yaml.FullLoader)
  22. return root_dir, local_models_dir, device, configs
  23. def get_text_images(simple_images):
  24. _, local_models_dir, device, configs = get_model_config()
  25. atom_model_manager = AtomModelSingleton()
  26. temp_layout_model = atom_model_manager.get_atom_model(
  27. atom_model_name=AtomicModel.Layout,
  28. layout_model_name=MODEL_NAME.DocLayout_YOLO,
  29. doclayout_yolo_weights=str(
  30. os.path.join(
  31. local_models_dir, configs['weights'][MODEL_NAME.DocLayout_YOLO]
  32. )
  33. ),
  34. device=device,
  35. )
  36. text_images = []
  37. for simple_image in simple_images:
  38. image = Image.fromarray(simple_image['img'])
  39. layout_res = temp_layout_model.predict(image)
  40. # 给textblock截图
  41. for res in layout_res:
  42. if res['category_id'] in [1]:
  43. x1, y1, _, _, x2, y2, _, _ = res['poly']
  44. # 初步清洗(宽和高都小于100)
  45. if x2 - x1 < 100 and y2 - y1 < 100:
  46. continue
  47. text_images.append(image.crop((x1, y1, x2, y2)))
  48. return text_images
  49. def auto_detect_lang(pdf_bytes: bytes):
  50. sample_docs = extract_pages(pdf_bytes)
  51. sample_pdf_bytes = sample_docs.tobytes()
  52. simple_images = load_images_from_pdf(sample_pdf_bytes, dpi=200)
  53. text_images = get_text_images(simple_images)
  54. langdetect_model = model_init(MODEL_NAME.YOLO_V11_LangDetect)
  55. lang = langdetect_model.do_detect(text_images)
  56. return lang
  57. def model_init(model_name: str):
  58. atom_model_manager = AtomModelSingleton()
  59. if model_name == MODEL_NAME.YOLO_V11_LangDetect:
  60. root_dir, _, device, _ = get_model_config()
  61. model = atom_model_manager.get_atom_model(
  62. atom_model_name=AtomicModel.LangDetect,
  63. langdetect_model_name=MODEL_NAME.YOLO_V11_LangDetect,
  64. langdetect_model_weight=str(os.path.join(root_dir, 'resources', 'yolov11-langdetect', 'yolo_v11_ft.pt')),
  65. device=device,
  66. )
  67. else:
  68. raise ValueError(f"model_name {model_name} not found")
  69. return model