card_rectify_and_center.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import cv2
  2. import numpy as np
  3. from ..core.model_loader import get_predictor
  4. from app.utils.defect_inference.img_rectify_and_center import (
  5. FryCardProcessParams, FryCardProcessor, CenterMode, FillMode)
  6. from app.core.config import settings
  7. from app.core.logger import get_logger
  8. import json
  9. logger = get_logger(__name__)
  10. class CardRectifyAndCenter:
  11. def __init__(self):
  12. self.inference_type = "outer_box"
  13. self.outer_box_model = get_predictor(self.inference_type)
  14. def rectify_and_center(self, img_bgr: np.ndarray) -> np.ndarray:
  15. # 1. 设置处理参数
  16. params = FryCardProcessParams(
  17. debug_level="detail",
  18. label_name="outer_box",
  19. center_mode=CenterMode.BOUNDING_RECT,
  20. fill_mode=FillMode.BLACK
  21. )
  22. # 2. 初始化处理器
  23. processor = FryCardProcessor()
  24. seg_json = self.outer_box_model.predict_from_image(img_bgr)
  25. # 4. 执行处理
  26. final_image = processor.process_image_with_json(img_bgr, seg_json, params)
  27. temp_img_path = settings.TEMP_WORK_DIR / "rectify_center_img.jpg"
  28. cv2.imwrite(temp_img_path, final_image)
  29. return final_image