defect_service.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import cv2
  2. import numpy as np
  3. from ..core.model_loader import get_predictor
  4. from app.utils.defect_inference.CardDefectAggregator import CardDefectAggregator
  5. from app.utils.defect_inference.arean_anylize_draw import DefectProcessor, DrawingParams
  6. from app.utils.defect_inference.AnalyzeCenter import (
  7. analyze_centering_rotated, analyze_centering_rect, formate_center_data)
  8. from app.utils.defect_inference.DrawCenterInfo import draw_boxes_and_center_info
  9. from app.utils.defect_inference.ClassifyEdgeCorner import ClassifyEdgeCorner
  10. from app.utils.json_data_formate import formate_face_data, formate_add_edit_type
  11. from app.utils.defect_inference.FilterOutsideDefects import FilterOutsideDefects
  12. from app.utils.simplify_points import SimplifyPoints
  13. from app.core.config import settings
  14. from app.core.logger import get_logger
  15. import json
  16. logger = get_logger(__name__)
  17. class DefectInferenceService:
  18. def _filter_max_prob_shape(self, json_data: dict, tag: str = "unknown") -> dict:
  19. """
  20. 通用过滤函数:只保留 shapes 中 probability 最大的那一个。
  21. """
  22. if not json_data or 'shapes' not in json_data or not json_data['shapes']:
  23. return json_data
  24. shapes = json_data['shapes']
  25. if len(shapes) <= 1:
  26. return json_data
  27. # 按 probability 降序排列,取第一个
  28. best_shape = max(shapes, key=lambda x: x.get('probability', 0))
  29. logger.info(f"[{tag}] 过滤多余框: 原有 {len(shapes)} 个, 保留最大置信度 {best_shape.get('probability'):.4f}")
  30. json_data['shapes'] = [best_shape]
  31. return json_data
  32. def defect_inference(self, inference_type: str, img_bgr: np.ndarray,
  33. is_draw_image=True) -> dict:
  34. """
  35. 执行卡片识别推理。
  36. Args:
  37. inference_type: 模型类型 (e.g., 'outer_box').
  38. img_bgr: 图像。
  39. Returns:
  40. 一个包含推理结果的字典。
  41. """
  42. simplifyPoints = SimplifyPoints()
  43. outside_filter = FilterOutsideDefects(expansion_pixel=30)
  44. # 面
  45. if (inference_type == "pokemon_back_face_coaxial_light_defect"
  46. or inference_type == "pokemon_front_face_reflect_coaxial_light_defect"
  47. or inference_type == "pokemon_front_face_no_reflect_coaxial_light_defect"):
  48. # 1. 获取对应的预测器实例
  49. predictor = get_predictor(inference_type)
  50. # 3. 调用我们新加的 predict_from_image 方法进行推理
  51. # result = predictor.predict_from_image(img_bgr)
  52. # 3. 实例化我们聚合器,传入预测器
  53. aggregator = CardDefectAggregator(
  54. predictor=predictor,
  55. tile_size=512,
  56. overlap_ratio=0.1, # 10% 重叠
  57. )
  58. json_data = aggregator.process_image(
  59. image=img_bgr,
  60. mode='face'
  61. )
  62. # 简化点数
  63. for shapes in json_data["shapes"]:
  64. points = shapes["points"]
  65. num1 = len(points)
  66. simplify_points = simplifyPoints.simplify_points(points)
  67. shapes["points"] = simplify_points
  68. new_num1 = len(simplify_points)
  69. logger.info(f"num: {num1}, new_num1: {new_num1}")
  70. logger.info("开始执行外框过滤...")
  71. try:
  72. # 1. 因为面原本没有外框推理,这里需要专门加载并推理一次
  73. predictor_outer = get_predictor("outer_box")
  74. outer_result = predictor_outer.predict_from_image(img_bgr)
  75. # 2. 执行过滤
  76. json_data = outside_filter.execute(json_data, outer_result)
  77. except Exception as e:
  78. logger.error(f"面流程外框过滤失败: {e}")
  79. merge_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-merge.json'
  80. with open(merge_json_path, 'w', encoding='utf-8') as f:
  81. json.dump(json_data, f, ensure_ascii=False, indent=4)
  82. logger.info(f"合并结束")
  83. processor = DefectProcessor(pixel_resolution=settings.PIXEL_RESOLUTION)
  84. area_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-face_result.json'
  85. if is_draw_image:
  86. drawing_params_with_rect = DrawingParams(draw_min_rect=True)
  87. drawn_image, area_json = processor.analyze_and_draw(img_bgr, json_data,
  88. drawing_params_with_rect)
  89. temp_img_path = settings.TEMP_WORK_DIR / f'{inference_type}-face_result.jpg'
  90. cv2.imwrite(temp_img_path, drawn_image)
  91. else:
  92. area_json = processor.analyze_from_json(json_data)
  93. face_json_result = formate_face_data(area_json)
  94. face_json_result = formate_add_edit_type(face_json_result)
  95. with open(area_json_path, 'w', encoding='utf-8') as f:
  96. json.dump(face_json_result, f, ensure_ascii=False, indent=2)
  97. logger.info("面的面积计算结束")
  98. return face_json_result
  99. # 存在边角判断的情况
  100. elif (inference_type == "pokemon_back_face_ring_light_defect"
  101. or inference_type == "pokemon_front_face_reflect_ring_light_defect"
  102. or inference_type == "pokemon_front_face_no_reflect_ring_light_defect"):
  103. predictor = get_predictor(inference_type)
  104. aggregator = CardDefectAggregator(
  105. predictor=predictor,
  106. tile_size=512,
  107. overlap_ratio=0.1, # 10% 重叠
  108. )
  109. json_data = aggregator.process_image(
  110. image=img_bgr,
  111. mode='face'
  112. )
  113. # 简化点数
  114. logger.info("开始进行点数简化")
  115. for shapes in json_data["shapes"]:
  116. points = shapes["points"]
  117. num1 = len(points)
  118. simplify_points = simplifyPoints.simplify_points(points)
  119. shapes["points"] = simplify_points
  120. new_num1 = len(simplify_points)
  121. # logger.info(f"num: {num1}, new_num1: {new_num1}")
  122. logger.info("点数简化结束")
  123. # merge_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-merge.json'
  124. # with open(merge_json_path, 'w', encoding='utf-8') as f:
  125. # json.dump(json_data, f, ensure_ascii=False, indent=4)
  126. # logger.info(f"合并结束")
  127. logger.info("开始执行外框过滤...")
  128. # 外框推理
  129. predictor_outer = get_predictor("outer_box")
  130. outer_result = predictor_outer.predict_from_image(img_bgr)
  131. # 过滤外框,只留一个
  132. outer_result = self._filter_max_prob_shape(outer_result, tag="Corner流程-外框")
  133. # 2. 执行过滤, 去掉外框外的缺陷
  134. json_data = outside_filter.execute(json_data, outer_result)
  135. processor = DefectProcessor(pixel_resolution=settings.PIXEL_RESOLUTION)
  136. area_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-corner_result.json'
  137. if is_draw_image:
  138. drawing_params_with_rect = DrawingParams(draw_min_rect=True)
  139. drawn_image, area_json = processor.analyze_and_draw(img_bgr, json_data,
  140. drawing_params_with_rect)
  141. temp_img_path = settings.TEMP_WORK_DIR / f'{inference_type}-corner_result.jpg'
  142. cv2.imwrite(temp_img_path, drawn_image)
  143. else:
  144. area_json: dict = processor.analyze_from_json(json_data)
  145. logger.info("边角缺陷面积计算结束")
  146. classifier = ClassifyEdgeCorner(settings.PIXEL_RESOLUTION,
  147. settings.CORNER_SIZE_MM,
  148. settings.EDGE_SIZE_MM)
  149. edge_corner_data = classifier.classify_defects_location(area_json, outer_result)
  150. edge_corner_data = formate_add_edit_type(edge_corner_data)
  151. with open(area_json_path, 'w', encoding='utf-8') as f:
  152. json.dump(edge_corner_data, f, ensure_ascii=False, indent=2)
  153. logger.info("边角面积计算结束")
  154. return edge_corner_data
  155. elif inference_type == "pokemon_front_card_center" \
  156. or inference_type == "pokemon_back_card_center":
  157. predictor_inner = get_predictor(settings.DEFECT_TYPE[inference_type]['inner_box'])
  158. predictor_outer = get_predictor(settings.DEFECT_TYPE[inference_type]['outer_box'])
  159. inner_result = predictor_inner.predict_from_image(img_bgr)
  160. outer_result = predictor_outer.predict_from_image(img_bgr)
  161. # 过滤内框和外框,只留最大置信度的框
  162. inner_result = self._filter_max_prob_shape(inner_result, tag="Center流程-内框")
  163. outer_result = self._filter_max_prob_shape(outer_result, tag="Center流程-外框")
  164. # temp_inner_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-inner_result.json'
  165. # temp_outer_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-outer_result.json'
  166. # with open(temp_inner_json_path, 'w', encoding='utf-8') as f:
  167. # json.dump(inner_result, f, ensure_ascii=False, indent=4)
  168. # with open(temp_outer_json_path, 'w', encoding='utf-8') as f:
  169. # json.dump(outer_result, f, ensure_ascii=False, indent=4)
  170. inner_points = inner_result['shapes'][0]['points']
  171. outer_points = outer_result['shapes'][0]['points']
  172. center_result, inner_rect_box, outer_rect_box = analyze_centering_rotated(inner_points, outer_points)
  173. # logger.info(f"inner_rect_box: {type(inner_rect_box)}, {inner_rect_box}")
  174. # logger.info(f"outer_rect_box: , {outer_rect_box}")
  175. logger.info("格式化居中数据")
  176. center_result = formate_center_data(center_result,
  177. inner_result, outer_result,
  178. inner_rect_box, outer_rect_box)
  179. draw_img = draw_boxes_and_center_info(img_bgr, center_result)
  180. temp_center_img_path = settings.TEMP_WORK_DIR / f'{inference_type}-center_result.jpg'
  181. cv2.imwrite(temp_center_img_path, draw_img)
  182. temp_center_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-center_result.json'
  183. with open(temp_center_json_path, 'w', encoding='utf-8') as f:
  184. json.dump(center_result, f, ensure_ascii=False, indent=2)
  185. return center_result
  186. else:
  187. return {}
  188. # inference_type: center, face, corner_edge
  189. def re_inference_from_json(self, card_light_type: str, center_json: dict, defect_json: dict) -> dict:
  190. light_type_list = ["center", "coaxial", "ring"]
  191. if card_light_type not in light_type_list:
  192. logger.error(f"inference_type 只能为{light_type_list}, 输入为{card_light_type}")
  193. raise ValueError(f"inference_type 只能为{light_type_list}, 输入为{card_light_type}")
  194. processor = DefectProcessor(pixel_resolution=settings.PIXEL_RESOLUTION)
  195. # 对于面的图, 不计算居中相关, 这里得到的center_json 应该为 {}
  196. if card_light_type == "coaxial":
  197. area_json = processor.re_analyze_from_json(defect_json)
  198. face_json_result = formate_face_data(area_json)
  199. logger.info("面缺陷面积计算结束")
  200. return face_json_result
  201. inner_result = center_json['box_result']['inner_box']
  202. outer_result = center_json['box_result']['outer_box']
  203. if card_light_type == "center":
  204. logger.info("居中重计算")
  205. inner_rect = inner_result['shapes'][0]['rect_box']
  206. outer_rect = outer_result['shapes'][0]['rect_box']
  207. center_result, inner_rect_box, outer_rect_box = analyze_centering_rect(inner_rect, outer_rect)
  208. center_result = formate_center_data(center_result,
  209. inner_result, outer_result,
  210. inner_rect_box, outer_rect_box)
  211. return center_result
  212. elif card_light_type == "ring":
  213. area_json: dict = processor.re_analyze_from_json(defect_json)
  214. logger.info("边角缺陷面积计算结束")
  215. # 根据外框区分边和角
  216. classifier = ClassifyEdgeCorner(settings.PIXEL_RESOLUTION,
  217. settings.CORNER_SIZE_MM,
  218. settings.EDGE_SIZE_MM)
  219. edge_corner_data = classifier.classify_defects_location(area_json, outer_result)
  220. logger.info("边角面积计算结束")
  221. return edge_corner_data
  222. else:
  223. return {}
  224. # 创建一个单例服务
  225. # defect_service = DefectInferenceService()