Kaynağa Gözat

试图计算2个总分

AnlaAnla 1 ay önce
ebeveyn
işleme
819eb4660f
4 değiştirilmiş dosya ile 30 ekleme ve 22 silme
  1. 3 1
      Test/img_data_insert.py
  2. 2 2
      Test/test01.py
  3. 23 18
      app/api/cards.py
  4. 2 1
      app/utils/scheme.py

+ 3 - 1
Test/img_data_insert.py

@@ -46,7 +46,9 @@ def upload_image_with_json(
 
 
 def send4img():
-    send_url = "http://127.0.0.1:7745/api/images/insert/7"
+    send_url = "http://192.168.31.243:7745/api/images/insert/1"
+
+
     front_face_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面面\rectify_center_img.jpg"
     front_face_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面面\front_face_score.json"
 

+ 2 - 2
Test/test01.py

@@ -1,7 +1,7 @@
 import requests
 
-base_url = 'http://127.0.0.1:7745/api/cards/query/7'
-# base_url = 'http://192.168.31.243:7745/api/cards/query/4'
+# base_url = 'http://127.0.0.1:7745/api/cards/query/7'
+base_url = 'http://192.168.31.243:7745/api/cards/query/1'
 
 
 def send(url):

+ 23 - 18
app/api/cards.py

@@ -67,31 +67,36 @@ def get_card_details(id: int, db_conn: PooledMySQLConnection = db_dependency):
         images = [CardImageResponse.model_validate(row) for row in image_records]
 
         # 计算总分数(只有当图片数量为 4 时才计算)
-        card_data['score'] = None
+        # 初始化
+        card_data["detection_score"] = None
+        card_data["modified_score"] = None
         if len(images) == 4:
             try:
-                final_score = 10
+                # ---------- detection_score ----------
+                detection_score = 10.0
                 for img in images:
-                    # 优先使用 modified_json,否则使用 detection_json
-                    src = img.modified_json if img.modified_json is not None else img.detection_json
-
-                    # src 可能为 None 或者不是 dict;做稳健检查
-                    if not isinstance(src, dict):
-                        logger.warning(f"图片 {img.id} 的 JSON 数据格式异常: {type(src)}")
-                        add_score = 0
-                    else:
-                        # 取嵌套键值:result -> _used_compute_deduct_score
-                        add_score = 0
+                    try:
+                        add_val = img.detection_json.get("result", {}).get("_used_compute_deduct_score", 0)
+                        detection_score += float(add_val or 0)
+                    except Exception as e:
+                        logger.warning(f"解析 detection_json 分数失败 (image_id={img.id}): {e}")
+                card_data["detection_score"] = detection_score
+
+                # ---------- modified_score ----------
+                all_modified_none = all(img.modified_json is None for img in images)
+                if not all_modified_none:
+                    modified_score = 10.0
+                    for img in images:
+                        src = img.modified_json if img.modified_json is not None else img.detection_json
                         try:
                             add_val = src.get("result", {}).get("_used_compute_deduct_score", 0)
-                            add_score = float(add_val) if add_val is not None else 0
+                            modified_score += float(add_val or 0)
                         except Exception as e:
-                            logger.warning(f"从图片 {img.id} 的 JSON 中解析分数失败: {e}")
-                            add_score = 0
+                            logger.warning(f"解析 modified_json 分数失败 (image_id={img.id}): {e}")
+                    card_data["modified_score"] = modified_score
+                else:
+                    card_data["modified_score"] = None
 
-                    final_score += add_score
-
-                card_data['score'] = final_score
             except Exception as e:
                 logger.error(f"计算分数过程异常: {e}")
 

+ 2 - 1
app/utils/scheme.py

@@ -41,7 +41,8 @@ class CardDetailResponse(BaseModel):
     card_name: Optional[str] = None
     created_at: datetime
     updated_at: datetime
-    score: Optional[float] = None
+    detection_score: Optional[float] = None
+    modified_score: Optional[float] = None
     images: List[CardImageResponse] = []
 
     class Config: