score_service.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import cv2
  2. from app.core.config import settings
  3. from app.core.logger import get_logger
  4. from app.services.defect_service import DefectInferenceService
  5. from app.services.card_rectify_and_center import CardRectifyAndCenter
  6. from app.utils.score_inference.CardScorer import CardScorer
  7. import numpy as np
  8. import json
  9. logger = get_logger(__name__)
  10. class ScoreService:
  11. def __init__(self):
  12. self.scoring_config_path = settings.SCORE_CONFIG_PATH
  13. self.card_scorer = CardScorer(config_path=self.scoring_config_path)
  14. self.defect_service = DefectInferenceService()
  15. self.rectify_center_service = CardRectifyAndCenter()
  16. def score_inference(self, score_type: str, is_reflect_card: bool,
  17. img_bgr: np.ndarray) -> dict:
  18. # 解包返回值,获取转正后的外框数据
  19. img_bgr, transformed_outer_json = self.rectify_center_service.rectify_and_center(img_bgr)
  20. if img_bgr is None:
  21. raise ValueError("图像转正处理失败")
  22. imageHeight, imageWidth = img_bgr.shape[:2]
  23. logger.info("开始进行卡片分数推理, 使用变换后的外框")
  24. # 定义通用参数,传入 pre_calculated_outer_result
  25. defect_kwargs = {
  26. "img_bgr": img_bgr.copy(),
  27. "pre_calculated_outer_result": transformed_outer_json
  28. }
  29. if score_type == 'front_ring' or score_type == 'front_coaxial':
  30. center_data = self.defect_service.defect_inference("pokemon_front_card_center", **defect_kwargs)
  31. else:
  32. center_data = self.defect_service.defect_inference("pokemon_back_card_center", **defect_kwargs)
  33. if is_reflect_card:
  34. if score_type == 'front_ring':
  35. defect_data = self.defect_service.defect_inference('pokemon_front_face_reflect_ring_light_defect',
  36. **defect_kwargs)
  37. elif score_type == 'front_coaxial':
  38. defect_data = self.defect_service.defect_inference('pokemon_front_face_reflect_coaxial_light_defect',
  39. **defect_kwargs)
  40. elif score_type == 'back_ring':
  41. defect_data = self.defect_service.defect_inference('pokemon_back_face_ring_light_defect',
  42. **defect_kwargs)
  43. elif score_type == 'back_coaxial':
  44. defect_data = self.defect_service.defect_inference('pokemon_back_face_coaxial_light_defect',
  45. **defect_kwargs)
  46. else:
  47. return {}
  48. else:
  49. if score_type == 'front_ring':
  50. defect_data = self.defect_service.defect_inference('pokemon_front_face_no_reflect_ring_light_defect',
  51. **defect_kwargs)
  52. elif score_type == 'front_coaxial':
  53. defect_data = self.defect_service.defect_inference('pokemon_front_face_no_reflect_coaxial_light_defect',
  54. **defect_kwargs)
  55. elif score_type == 'back_ring':
  56. defect_data = self.defect_service.defect_inference('pokemon_back_face_ring_light_defect',
  57. **defect_kwargs)
  58. elif score_type == 'back_coaxial':
  59. defect_data = self.defect_service.defect_inference('pokemon_back_face_coaxial_light_defect',
  60. **defect_kwargs)
  61. else:
  62. return {}
  63. logger.info("模型推理结束, 开始计算分数")
  64. if score_type == 'front_ring' or score_type == 'front_coaxial':
  65. card_aspect = "front"
  66. else:
  67. card_aspect = "back"
  68. if score_type == 'front_ring' or score_type == 'back_ring':
  69. card_light_type = "ring"
  70. else:
  71. card_light_type = "coaxial"
  72. if card_light_type == 'ring':
  73. center_score_data = self.card_scorer.calculate_centering_score(card_aspect, center_data, True)
  74. # 计算角, 边, 面的分数, 会把分数写入json, 然后传入刚写好的, 继续写边的分数
  75. defect_data = self.card_scorer.calculate_defect_score('corner', card_aspect, card_light_type,
  76. defect_data, True)
  77. defect_data = self.card_scorer.calculate_defect_score('edge', card_aspect, card_light_type,
  78. defect_data, True)
  79. defect_score_data = self.card_scorer.calculate_defect_score('face', card_aspect, card_light_type,
  80. defect_data, True)
  81. elif card_light_type == 'coaxial':
  82. # 居中
  83. center_score_data = {}
  84. # 同轴光照片只计算面缺陷
  85. defect_score_data = self.card_scorer.calculate_defect_score('face', card_aspect, card_light_type,
  86. defect_data, True)
  87. else:
  88. return {}
  89. result_json = self.card_scorer.formate_one_card_result(center_score_data, defect_score_data,
  90. card_light_type=card_light_type,
  91. card_aspect=card_aspect,
  92. imageHeight=imageHeight,
  93. imageWidth=imageWidth)
  94. temp_score_json_path = settings.TEMP_WORK_DIR / f'{score_type}_score.json'
  95. with open(temp_score_json_path, 'w', encoding='utf-8') as f:
  96. json.dump(result_json, f, ensure_ascii=False, indent=2)
  97. logger.info("分数推理完成 ")
  98. return result_json
  99. def recalculate_defect_score(self, score_type: str, json_data: dict):
  100. center_json_data = json_data["result"]['center_result']
  101. defect_json_data = json_data["result"]['defect_result']
  102. imageHeight = json_data["result"].get('imageHeight', 0)
  103. imageWidth = json_data["result"].get('imageWidth', 0)
  104. # 正反面类型分类
  105. if score_type == 'front_ring' or score_type == 'front_coaxial':
  106. card_aspect = "front"
  107. else:
  108. card_aspect = "back"
  109. if score_type == 'front_ring' or score_type == 'back_ring':
  110. card_light_type = "ring"
  111. else:
  112. card_light_type = "coaxial"
  113. logger.info("开始进行缺陷信息重计算")
  114. defect_data = self.defect_service.re_inference_from_json(card_light_type=card_light_type,
  115. center_json=center_json_data,
  116. defect_json=defect_json_data)
  117. logger.info("开始重新计算分数")
  118. if card_light_type == 'ring':
  119. logger.info("开始进行居中信息重计算")
  120. center_data = self.defect_service.re_inference_from_json(card_light_type="center",
  121. center_json=center_json_data,
  122. defect_json=defect_json_data)
  123. center_score_data = self.card_scorer.calculate_centering_score(card_aspect, center_data, True)
  124. # 计算角, 边, 面的分数, 会把分数写入json, 然后传入刚写好的, 继续写边的分数
  125. logger.info("开始重新计算:边角面")
  126. defect_data = self.card_scorer.calculate_defect_score('corner', card_aspect, card_light_type,
  127. defect_data, True)
  128. defect_data = self.card_scorer.calculate_defect_score('edge', card_aspect, card_light_type,
  129. defect_data, True)
  130. defect_score_data = self.card_scorer.calculate_defect_score('face', card_aspect, card_light_type,
  131. defect_data, True)
  132. elif card_light_type == 'coaxial':
  133. # 居中
  134. center_score_data = {}
  135. # 同轴光照片只计算面缺陷
  136. logger.info("开始重新计算:面")
  137. defect_score_data = self.card_scorer.calculate_defect_score('face', card_aspect, card_light_type,
  138. defect_data, True)
  139. else:
  140. return {}
  141. result_json = self.card_scorer.formate_one_card_result(center_score_data, defect_score_data,
  142. card_light_type=card_light_type,
  143. card_aspect=card_aspect,
  144. imageHeight=imageHeight,
  145. imageWidth=imageWidth)
  146. temp_score_json_path = settings.TEMP_WORK_DIR / f're_{score_type}_score.json'
  147. with open(temp_score_json_path, 'w', encoding='utf-8') as f:
  148. json.dump(result_json, f, ensure_ascii=False, indent=2)
  149. logger.info("分数推理完成 ")
  150. return result_json