소스 검색

居中外框计算卡面真实大小

AnlaAnla 2 주 전
부모
커밋
6fbd93cfe5
1개의 변경된 파일25개의 추가작업 그리고 11개의 파일을 삭제
  1. 25 11
      app/utils/defect_inference/AnalyzeCenter.py

+ 25 - 11
app/utils/defect_inference/AnalyzeCenter.py

@@ -5,15 +5,18 @@ import matplotlib.pyplot as plt
 import math
 from typing import Union, List
 from app.core.logger import get_logger
+from app.core.config import settings
 
 logger = get_logger(__name__)
 
+
 def get_points_from_file(json_file):
     """辅助函数,从JSON文件加载点"""
     with open(json_file, 'r') as f:
         data = json.load(f)
     return data['shapes'][0]['points']
 
+
 def get_rect_from_file(json_file):
     """辅助函数,从JSON文件加载点"""
     with open(json_file, 'r') as f:
@@ -214,6 +217,18 @@ def analyze_centering_rotated(inner_points: Union[str, List], outer_points: Unio
             angle_diff), inner_box_corners_int, outer_box_corners_int
 
 
+def get_int_bounding_rect(points):
+    # np.min 返回的是 numpy.float32,必须转为 python float
+    x_min = int(np.min(points[:, 0]))
+    y_min = int(np.min(points[:, 1]))
+    x_max = int(np.max(points[:, 0]))
+    y_max = int(np.max(points[:, 1]))
+
+    w = x_max - x_min
+    h = y_max - y_min
+    return x_min, y_min, w, h
+
+
 def analyze_centering_rect(inner_points: Union[str, List], outer_points: Union[str, List]):
     """
     使用横平竖直的矩形 (Axis-Aligned Bounding Box) 进行居中分析。
@@ -228,17 +243,6 @@ def analyze_centering_rect(inner_points: Union[str, List], outer_points: Union[s
     inner_contour = np.array(inner_points, dtype=np.int32)
     outer_contour = np.array(outer_points, dtype=np.int32)
 
-    def get_int_bounding_rect(points):
-        # np.min 返回的是 numpy.float32,必须转为 python float
-        x_min = int(np.min(points[:, 0]))
-        y_min = int(np.min(points[:, 1]))
-        x_max = int(np.max(points[:, 0]))
-        y_max = int(np.max(points[:, 1]))
-
-        w = x_max - x_min
-        h = y_max - y_min
-        return x_min, y_min, w, h
-
     # --- 1. 获取横平竖直的矩形参数 (x, y, w, h) ---
     # 即使传入的是4个点,用 boundingRect 也能确保取出准确的边界
     ix, iy, iw, ih = get_int_bounding_rect(inner_contour)
@@ -301,9 +305,19 @@ def analyze_centering_rect(inner_points: Union[str, List], outer_points: Union[s
 def formate_center_data(center_result,
                         inner_data: dict, outer_data: dict,
                         inner_rect_points: List, outer_rect_points: List):
+    pixel_to_mm = settings.PIXEL_RESOLUTION / 1000.0
+    # 转换为 numpy 数组
+    outer_contour = np.array(outer_rect_points, dtype=np.int32)
+    ox, oy, ow, oh = get_int_bounding_rect(outer_contour)
+
+    real_width = ow * pixel_to_mm
+    real_height = oh * pixel_to_mm
+
     data = {
         "box_result": {
             "center_inference": {
+                "real_width_mm": real_width,
+                "real_height_mm": real_height,
                 "angel_diff": center_result[2],
                 "center_left": center_result[0][0],
                 "center_right": center_result[0][1],