Forráskód Böngészése

评级报告切图升级

AnlaAnla 5 napja
szülő
commit
ef6865b7ba
2 módosított fájl, 47 hozzáadás és 4 törlés
  1. 36 1
      Test/test01.py
  2. 11 3
      app/api/rating_report.py

+ 36 - 1
Test/test01.py

@@ -1 +1,36 @@
-print(1)
+from PIL import Image
+
+img = Image.open(r"C:\Code\ML\Image\Card\b2.jpg")
+
+min_rect = [
+    [
+        1616.630615234375,
+        2060.99365234375
+    ],
+    [
+        1752.767822265625,
+        1715.654296875
+    ],
+    -62.34006881713867
+]
+
+
+img_w, img_h = img.size
+(center_x, center_y), (rect_w, rect_h), angle = min_rect
+center_x = int(center_x)
+center_y = int(center_y)
+rect_w = int(rect_w)
+rect_h = int(rect_h)
+
+# 缩放比例, 根据倾斜角度来
+resize_scale = 1.5 - abs(abs(angle % 90) - 45) / 90
+side_length = max(max(rect_w, rect_h) * resize_scale, 100)
+half_side = side_length / 2
+
+print()
+left, top = max(0, center_x - half_side), max(0, center_y - half_side)
+right, bottom = min(img_w, center_x + half_side), min(img_h, center_y + half_side)
+
+cropped_img = img.crop((left, top, right, bottom))
+
+print()

+ 11 - 3
app/api/rating_report.py

@@ -3,6 +3,8 @@ from mysql.connector.pooling import PooledMySQLConnection
 
 import io
 import json
+import cv2
+import numpy as np
 from datetime import datetime
 from app.core.minio_client import minio_client
 from typing import List, Dict, Any, Optional
@@ -12,6 +14,7 @@ from app.core.logger import get_logger
 from app.core.config import settings
 from app.core.database_loader import get_db_connection
 from app.crud import crud_card
+
 from app.utils.scheme import ImageType
 
 logger = get_logger(__name__)
@@ -106,9 +109,14 @@ def _crop_defect_image(original_image_path_str: str, min_rect: List, output_file
         # 2. 在内存中用 PIL 切图
         with Image.open(io.BytesIO(image_bytes)) as img:
             img_w, img_h = img.size
-            center_x, center_y = min_rect[0]
-            rect_w, rect_h = min_rect[1]
-            side_length = max(max(rect_w, rect_h) * 1.5, 100)
+            (center_x, center_y), (rect_w, rect_h), angle = min_rect
+            center_x = int(center_x)
+            center_y = int(center_y)
+            rect_w = int(rect_w)
+            rect_h = int(rect_h)
+
+            resize_scale = 1.5 - abs(abs(angle % 90) - 45) / 90
+            side_length = max(max(rect_w, rect_h) * resize_scale, 100)
             half_side = side_length / 2
 
             left, top = max(0, center_x - half_side), max(0, center_y - half_side)