Przeglądaj źródła

增加缺陷坐标点的简化功能

AnlaAnla 3 tygodni temu
rodzic
commit
6d4c211b99

+ 223 - 0
Test/点图像简化.py

@@ -0,0 +1,223 @@
+from shapely.geometry import Polygon, Point
+
+# 你提供的原始坐标点
+points_raw = [
+    [
+        484,
+        2686
+    ],
+    [
+        484,
+        2693
+    ],
+    [
+        485,
+        2694
+    ],
+    [
+        485,
+        2697
+    ],
+    [
+        486,
+        2698
+    ],
+    [
+        486,
+        2702
+    ],
+    [
+        487,
+        2703
+    ],
+    [
+        487,
+        2705
+    ],
+    [
+        488,
+        2706
+    ],
+    [
+        488,
+        2709
+    ],
+    [
+        489,
+        2710
+    ],
+    [
+        489,
+        2714
+    ],
+    [
+        490,
+        2715
+    ],
+    [
+        490,
+        2717
+    ],
+    [
+        491,
+        2718
+    ],
+    [
+        491,
+        2722
+    ],
+    [
+        492,
+        2723
+    ],
+    [
+        492,
+        2725
+    ],
+    [
+        499,
+        2732
+    ],
+    [
+        500,
+        2732
+    ],
+    [
+        500,
+        2731
+    ],
+    [
+        501,
+        2730
+    ],
+    [
+        501,
+        2729
+    ],
+    [
+        502,
+        2728
+    ],
+    [
+        502,
+        2711
+    ],
+    [
+        501,
+        2710
+    ],
+    [
+        501,
+        2709
+    ],
+    [
+        500,
+        2708
+    ],
+    [
+        500,
+        2707
+    ],
+    [
+        499,
+        2706
+    ],
+    [
+        499,
+        2705
+    ],
+    [
+        498,
+        2704
+    ],
+    [
+        498,
+        2703
+    ],
+    [
+        497,
+        2702
+    ],
+    [
+        497,
+        2699
+    ],
+    [
+        496,
+        2698
+    ],
+    [
+        496,
+        2696
+    ],
+    [
+        495,
+        2695
+    ],
+    [
+        495,
+        2693
+    ],
+    [
+        494,
+        2692
+    ],
+    [
+        494,
+        2691
+    ],
+    [
+        491,
+        2688
+    ],
+    [
+        488,
+        2688
+    ],
+    [
+        487,
+        2687
+    ],
+    [
+        485,
+        2687
+    ]
+]
+
+# 1. 创建一个 Shapely Polygon 对象
+original_polygon = Polygon(points_raw)
+
+print(f"原始点数量: {len(points_raw)}")
+print(f"原始多边形面积: {original_polygon.area:.2f}")
+print("-" * 30)
+
+# 2. 使用 .simplify() 方法进行简化
+# tolerance 参数就是 RDP 算法的阈值(epsilon)
+# 它决定了简化的程度,单位与你的坐标单位相同。
+# 你需要根据实际情况调整这个值。
+
+# 尝试一个较小的 tolerance
+tolerance_small = 0.8
+simplified_polygon_small = original_polygon.simplify(tolerance_small, preserve_topology=True)
+points_small = list(simplified_polygon_small.exterior.coords)
+
+print(f"使用 tolerance = {tolerance_small}")
+print(f"简化后点数量: {len(points_small)}")
+print(f"简化后多边形面积: {simplified_polygon_small.area:.2f}")
+# 将点坐标转换为整数列表
+points_small_int = [[int(x), int(y)] for x, y in points_small]
+print("简化后的点:", points_small_int)
+print("-" * 30)
+
+# 尝试一个较大的 tolerance
+tolerance_large = 2.0
+simplified_polygon_large = original_polygon.simplify(tolerance_large, preserve_topology=True)
+points_large = list(simplified_polygon_large.exterior.coords)
+
+print(f"使用 tolerance = {tolerance_large}")
+print(f"简化后点数量: {len(points_large)}")
+print(f"简化后多边形面积: {simplified_polygon_large.area:.2f}")
+# 将点坐标转换为整数列表
+points_large_int = [[int(x), int(y)] for x, y in points_large]
+print("简化后的点:", points_large_int)
+print("-" * 30)

+ 15 - 0
app/core/缺陷英文与中文对应.json

@@ -0,0 +1,15 @@
+{
+  "face": "面",
+  "wear": "磨损",
+  "scratch": "划痕",
+  "stain": "污渍",
+  "scuff": "磨痕",
+  "impact": "撞击痕迹",
+  "damaged": "损坏",
+  "wear_and_impact": "磨损和撞击痕迹",
+  "pit": "凹坑",
+  "corner": "角",
+  "wear_and_stain": "磨损和污渍",
+  "inner_box": "内框",
+  "outer_box": "外框"
+}

+ 24 - 4
app/services/defect_service.py

@@ -7,6 +7,7 @@ from app.utils.defect_inference.AnalyzeCenter import analyze_centering_rotated,
 from app.utils.defect_inference.DrawCenterInfo import draw_boxes_and_center_info
 from app.utils.defect_inference.ClassifyEdgeCorner import ClassifyEdgeCorner
 from app.utils.json_data_formate import formate_face_data, formate_add_edit_type
+from app.utils.simplify_points import SimplifyPoints
 from app.core.config import settings
 from app.core.logger import get_logger
 import json
@@ -27,6 +28,7 @@ class DefectInferenceService:
         Returns:
             一个包含推理结果的字典。
         """
+        simplifyPoints = SimplifyPoints()
 
         # 面
         if (inference_type == "pokemon_front_face_no_reflect_defect"
@@ -49,10 +51,20 @@ class DefectInferenceService:
                 image=img_bgr,
                 mode='face'
             )
-            # merge_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-merge.json'
-            # with open(merge_json_path, 'w', encoding='utf-8') as f:
-            #     json.dump(json_data, f, ensure_ascii=False, indent=4)
-            # logger.info(f"合并结束")
+
+            # 简化点数
+            for shapes in json_data["shapes"]:
+                points = shapes["points"]
+                num1 = len(points)
+                simplify_points = simplifyPoints.simplify_points(points)
+                shapes["points"] = simplify_points
+                new_num1 = len(simplify_points)
+                logger.info(f"num: {num1}, new_num1: {new_num1}")
+
+            merge_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-merge.json'
+            with open(merge_json_path, 'w', encoding='utf-8') as f:
+                json.dump(json_data, f, ensure_ascii=False, indent=4)
+            logger.info(f"合并结束")
 
             processor = DefectProcessor(pixel_resolution=settings.PIXEL_RESOLUTION)
             area_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-face_result.json'
@@ -91,6 +103,14 @@ class DefectInferenceService:
                 mode='edge'
             )
 
+            for shapes in json_data["shapes"]:
+                points = shapes["points"]
+                num1 = len(points)
+                simplify_points = simplifyPoints.simplify_points(points)
+                shapes["points"] = simplify_points
+                new_num1 = len(simplify_points)
+                logger.info(f"num: {num1}, new_num1: {new_num1}")
+
             # merge_json_path = settings.TEMP_WORK_DIR / f'{inference_type}-merge.json'
             # with open(merge_json_path, 'w', encoding='utf-8') as f:
             #     json.dump(json_data, f, ensure_ascii=False, indent=4)

+ 19 - 1
app/utils/score_inference/CardScorer.py

@@ -93,7 +93,25 @@ class CardScorer:
 
             # 将分数写入json
             if is_write_score:
-                defect['score'] = the_score
+                if "score" not in defect:
+                    # 新建的时候
+                    logger.info(f"新建分数score: {the_score}")
+                    defect['score'] = the_score
+                    defect["new_score"] = None
+                elif defect.get("new_score") is None:
+                    # 初次修改
+                    if defect["score"] != the_score:
+                        logger.info(f"初次修改 -> new_score: {the_score} (原score: {defect['score']})")
+                        defect["new_score"] = the_score
+
+                elif "score" in defect and defect["new_score"] is not None:
+                    # 多次修改
+                    if defect["new_score"] != the_score:
+                        defect["score"] = defect["new_score"]
+                        defect["new_score"] = the_score
+                else:
+                    defect['score'] = the_score
+                    defect["new_score"] = None
 
         # 2. 根据权重/系数计算最终扣分
         weights = aspect_config.get(f"{card_aspect}_weights") or aspect_config.get("coefficients")

+ 15 - 0
app/utils/simplify_points.py

@@ -0,0 +1,15 @@
+from shapely.geometry import Polygon
+
+
+class SimplifyPoints:
+    def __init__(self):
+        # tolerance 参数就是 RDP 算法的阈值(epsilon)
+        # 它决定了简化的程度,单位与你的坐标单位相同。
+        self.tolerance_small = 0.8
+
+    def simplify_points(self, points):
+        original_polygon = Polygon(points)
+        simplified_polygon_small = original_polygon.simplify(self.tolerance_small, preserve_topology=True)
+        points_small = list(simplified_polygon_small.exterior.coords)
+
+        return points_small