|
|
@@ -150,9 +150,12 @@ def _process_defects_for_json(
|
|
|
json_data: dict,
|
|
|
side: str,
|
|
|
all_images: list = None,
|
|
|
- generate_defect_img: bool = True,
|
|
|
generate_related_images: bool = True
|
|
|
):
|
|
|
+ """
|
|
|
+ 为每条缺陷生成同面其他类型图的位置截图列表 defectImgUrls。
|
|
|
+ 不再生成主缺陷截图 defectImgUrl,也不再聚合 defectDetailList。
|
|
|
+ """
|
|
|
start_time = perf_counter()
|
|
|
if not json_data or "result" not in json_data:
|
|
|
logger.info(
|
|
|
@@ -162,93 +165,46 @@ def _process_defects_for_json(
|
|
|
return
|
|
|
defect_result = json_data["result"].get("defect_result", {})
|
|
|
defects = defect_result.get("defects", [])
|
|
|
-
|
|
|
- is_fusion = side in ("front_fusion", "back_fusion")
|
|
|
+
|
|
|
side_prefix = "front_" if side.startswith("front_") else "back_"
|
|
|
-
|
|
|
- defect_detail_list = []
|
|
|
+
|
|
|
for idx, defect in enumerate(defects, start=1):
|
|
|
min_rect = defect.get("min_rect")
|
|
|
- defect_img_url = ""
|
|
|
- location_str = ""
|
|
|
defect_img_url_list = []
|
|
|
-
|
|
|
- if min_rect and len(min_rect) == 3:
|
|
|
- center_x, center_y = min_rect[0]
|
|
|
- location_str = f"{int(center_x)},{int(center_y)}"
|
|
|
-
|
|
|
- # 使用坐标哈希作为缓存文件名,避免重复裁剪
|
|
|
+
|
|
|
+ if min_rect and len(min_rect) == 3 and generate_related_images and all_images:
|
|
|
rect_str = str(min_rect)
|
|
|
rect_hash = hashlib.md5(rect_str.encode('utf-8')).hexdigest()[:8]
|
|
|
- if generate_defect_img:
|
|
|
- filename = f"xy_{card_id}_{img_id}_{idx}_{rect_hash}.jpg"
|
|
|
-
|
|
|
- out_rel_path = f"/DefectImage/{filename}"
|
|
|
- out_object_name = f"{settings.MINIO_BASE_PREFIX}{out_rel_path}"
|
|
|
+
|
|
|
+ same_side_images = [
|
|
|
+ img for img in all_images
|
|
|
+ if getattr(img, 'image_type', '').startswith(side_prefix) and getattr(img, 'id', None) != img_id
|
|
|
+ ]
|
|
|
+ for s_img in same_side_images:
|
|
|
+ s_img_type = getattr(s_img, 'image_type', '')
|
|
|
+ s_img_path = getattr(s_img, 'image_path', '')
|
|
|
+ s_img_id = getattr(s_img, 'id', 0)
|
|
|
+
|
|
|
+ s_filename = f"xy_{card_id}_{s_img_id}_{idx}_{rect_hash}.jpg"
|
|
|
+ s_out_rel_path = f"/DefectImage/{s_filename}"
|
|
|
+ s_out_object_name = f"{settings.MINIO_BASE_PREFIX}{s_out_rel_path}"
|
|
|
+
|
|
|
+ s_url = ""
|
|
|
try:
|
|
|
- # 检查 MinIO 中是否已有该截图,有则直接使用
|
|
|
- minio_client.stat_object(settings.MINIO_BUCKET, out_object_name)
|
|
|
- defect_img_url = settings.get_full_url(out_rel_path)
|
|
|
+ minio_client.stat_object(settings.MINIO_BUCKET, s_out_object_name)
|
|
|
+ s_url = settings.get_full_url(s_out_rel_path)
|
|
|
except Exception:
|
|
|
- # 不存在或异常,则执行裁剪并上传
|
|
|
- defect_img_url = crop_defect_image(img_path, min_rect, filename)
|
|
|
-
|
|
|
- # 把同面的其他类型图在同样位置截图(不论是不是融合图都截)
|
|
|
- if generate_related_images and all_images:
|
|
|
- same_side_images = [
|
|
|
- img for img in all_images
|
|
|
- if getattr(img, 'image_type', '').startswith(side_prefix) and getattr(img, 'id', None) != img_id
|
|
|
- ]
|
|
|
- for s_img in same_side_images:
|
|
|
- s_img_type = getattr(s_img, 'image_type', '')
|
|
|
- s_img_path = getattr(s_img, 'image_path', '')
|
|
|
- s_img_id = getattr(s_img, 'id', 0)
|
|
|
-
|
|
|
- s_filename = f"xy_{card_id}_{s_img_id}_{idx}_{rect_hash}.jpg"
|
|
|
- s_out_rel_path = f"/DefectImage/{s_filename}"
|
|
|
- s_out_object_name = f"{settings.MINIO_BASE_PREFIX}{s_out_rel_path}"
|
|
|
-
|
|
|
- s_url = ""
|
|
|
- try:
|
|
|
- minio_client.stat_object(settings.MINIO_BUCKET, s_out_object_name)
|
|
|
- s_url = settings.get_full_url(s_out_rel_path)
|
|
|
- except Exception:
|
|
|
- if s_img_path:
|
|
|
- s_url = crop_defect_image(s_img_path, min_rect, s_filename)
|
|
|
-
|
|
|
- if s_url:
|
|
|
- defect_img_url_list.append({
|
|
|
- "image_type": s_img_type,
|
|
|
- "url": s_url
|
|
|
- })
|
|
|
-
|
|
|
- # 1. 给每条缺陷带上 defectImgUrl
|
|
|
- defect["defectImgUrl"] = defect_img_url
|
|
|
+ if s_img_path:
|
|
|
+ s_url = crop_defect_image(s_img_path, min_rect, s_filename)
|
|
|
+
|
|
|
+ if s_url:
|
|
|
+ defect_img_url_list.append({
|
|
|
+ "image_type": s_img_type,
|
|
|
+ "url": s_url
|
|
|
+ })
|
|
|
+
|
|
|
defect["defectImgUrls"] = defect_img_url_list
|
|
|
-
|
|
|
- # 2. 组装 defectDetailList 元素
|
|
|
- raw_type = f"{defect.get('defect_type', '')}".upper().strip()
|
|
|
- type_str_map = {
|
|
|
- "CORNER": "CORNER",
|
|
|
- "EDGE": "SIDE",
|
|
|
- "FACE": "SURFACE"
|
|
|
- }
|
|
|
- type_str = type_str_map.get(raw_type, raw_type)
|
|
|
-
|
|
|
- detail_item = {
|
|
|
- "id": defect.get("id", idx),
|
|
|
- "side": side,
|
|
|
- "location": location_str,
|
|
|
- "type": type_str,
|
|
|
- "defectImgUrl": defect_img_url,
|
|
|
- "label": defect.get("label", ""),
|
|
|
- "actual_area": defect.get("actual_area", 0),
|
|
|
- "defectImgUrls": defect_img_url_list
|
|
|
- }
|
|
|
-
|
|
|
- defect_detail_list.append(detail_item)
|
|
|
-
|
|
|
- defect_result["defectDetailList"] = defect_detail_list
|
|
|
+
|
|
|
logger.info(
|
|
|
"耗时埋点 _process_defects_for_json: card_id=%s image_id=%s side=%s defects=%s elapsed_ms=%.2f",
|
|
|
card_id, img_id, side, len(defects), (perf_counter() - start_time) * 1000
|
|
|
@@ -257,7 +213,6 @@ def _process_defects_for_json(
|
|
|
|
|
|
def _process_images_to_xy_format(
|
|
|
card_data: dict,
|
|
|
- generate_defect_img: bool = True,
|
|
|
generate_related_images: bool = True
|
|
|
):
|
|
|
"""
|
|
|
@@ -276,7 +231,6 @@ def _process_images_to_xy_format(
|
|
|
if d_internal:
|
|
|
_process_defects_for_json(
|
|
|
card_id, img.id, img.image_path, d_internal, img.image_type, all_images,
|
|
|
- generate_defect_img=generate_defect_img,
|
|
|
generate_related_images=generate_related_images
|
|
|
)
|
|
|
img.detection_json = convert_internal_to_xy_format(d_internal)
|
|
|
@@ -290,7 +244,6 @@ def _process_images_to_xy_format(
|
|
|
if m_internal:
|
|
|
_process_defects_for_json(
|
|
|
card_id, img.id, img.image_path, m_internal, img.image_type, all_images,
|
|
|
- generate_defect_img=generate_defect_img,
|
|
|
generate_related_images=generate_related_images
|
|
|
)
|
|
|
img.modified_json = convert_internal_to_xy_format(m_internal)
|
|
|
@@ -369,7 +322,6 @@ def get_card_details(
|
|
|
# 4. 遍历图片,转换格式 (使用抽取出的辅助函数)
|
|
|
_process_images_to_xy_format(
|
|
|
card_data,
|
|
|
- generate_defect_img=False,
|
|
|
generate_related_images=True
|
|
|
)
|
|
|
|