rating_report.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. from fastapi import APIRouter, HTTPException, Depends, Query
  2. from mysql.connector.pooling import PooledMySQLConnection
  3. import io
  4. from app.core.minio_client import minio_client
  5. from typing import List, Dict, Any, Optional
  6. from PIL import Image
  7. from app.core.logger import get_logger
  8. from app.core.config import settings
  9. from app.core.database_loader import get_db_connection
  10. from app.crud import crud_card
  11. from app.utils.scheme import ImageType
  12. logger = get_logger(__name__)
  13. router = APIRouter()
  14. def _get_active_json(image_data: Any) -> Optional[Dict]:
  15. """获取有效的json数据,优先 modified_json"""
  16. if not image_data:
  17. return None
  18. # image_data 可能是 Pydantic 对象或 字典,做兼容处理
  19. if hasattr(image_data, "modified_json"):
  20. mj = image_data.modified_json
  21. dj = image_data.detection_json
  22. else:
  23. mj = image_data.get("modified_json")
  24. dj = image_data.get("detection_json")
  25. # 注意:根据 schema.py,这里读出来已经是 dict 了,不需要 json.loads
  26. # 如果数据库里存的是 null,读出来是 None
  27. if mj:
  28. return mj
  29. return dj
  30. def _crop_defect_image(original_image_path_str: str, min_rect: List, output_filename: str) -> str:
  31. """
  32. 通过 MinIO 切割缺陷图片为正方形并保存
  33. """
  34. try:
  35. # ★ 将进来的全路径 URL 剥离为相对路径 (如 /Data/xxx.jpg) 供 MinIO 读取
  36. rel_path = original_image_path_str.replace(settings.DATA_HOST_URL, "")
  37. rel_path = "/" + rel_path.lstrip('/\\')
  38. object_name = f"{settings.MINIO_BASE_PREFIX}{rel_path}"
  39. # 1. 从 MinIO 获取原图字节
  40. try:
  41. response = minio_client.get_object(settings.MINIO_BUCKET, object_name)
  42. image_bytes = response.read()
  43. response.close()
  44. response.release_conn()
  45. except Exception as e:
  46. logger.warning(f"从MinIO获取原图失败: {object_name} -> {e}")
  47. return ""
  48. # 2. 在内存中用 PIL 切图
  49. with Image.open(io.BytesIO(image_bytes)) as img:
  50. img_w, img_h = img.size
  51. center_x, center_y = min_rect[0]
  52. rect_w, rect_h = min_rect[1]
  53. side_length = max(max(rect_w, rect_h) * 1.5, 100)
  54. half_side = side_length / 2
  55. left, top = max(0, center_x - half_side), max(0, center_y - half_side)
  56. right, bottom = min(img_w, center_x + half_side), min(img_h, center_y + half_side)
  57. cropped_img = img.crop((left, top, right, bottom))
  58. # 3. 将切割后的图存入内存流,并上传到 MinIO
  59. out_bytes = io.BytesIO()
  60. cropped_img.save(out_bytes, format="JPEG", quality=95)
  61. out_bytes.seek(0)
  62. out_rel_path = f"/DefectImage/{output_filename}"
  63. out_object_name = f"{settings.MINIO_BASE_PREFIX}{out_rel_path}"
  64. minio_client.put_object(
  65. settings.MINIO_BUCKET,
  66. out_object_name,
  67. out_bytes,
  68. len(out_bytes.getvalue()),
  69. content_type="image/jpeg"
  70. )
  71. return settings.get_full_url(out_rel_path)
  72. except Exception as e:
  73. logger.error(f"切割并上传图片失败: {e}")
  74. return ""
  75. @router.get("/generate", status_code=200, summary="生成评级报告数据")
  76. def generate_rating_report(
  77. cardNo: str,
  78. db_conn: PooledMySQLConnection = Depends(get_db_connection)
  79. ):
  80. if not cardNo or not cardNo.strip():
  81. raise HTTPException(status_code=400, detail="cardNo 不能为空")
  82. # 根据cardNo 查询id
  83. try:
  84. with db_conn.cursor() as cursor:
  85. query_sql = f"SELECT id, cardNo FROM {settings.DB_CARD_TABLE_NAME} WHERE cardNo = %s"
  86. cursor.execute(query_sql, (cardNo,))
  87. card_id = cursor.fetchone()[0]
  88. if not card_id:
  89. raise HTTPException(
  90. status_code=404,
  91. detail=f"未找到卡号为 {cardNo} 的相关记录"
  92. )
  93. except Exception as e:
  94. logger.error(f"创建卡牌失败: {e}")
  95. raise HTTPException(status_code=500, detail="数据库查询失败。")
  96. top_n_defects = 3
  97. """
  98. 根据 Card ID 生成评级报告 JSON
  99. """
  100. # 1. 获取卡片详情 (复用 Crud 逻辑,确保能拿到所有图片)
  101. card_data = crud_card.get_card_with_details(db_conn, card_id)
  102. if not card_data:
  103. raise HTTPException(status_code=404, detail="未找到该卡片信息")
  104. # 初始化返回结构
  105. response_data = {
  106. "backImageUrl": "",
  107. "frontImageUrl": "",
  108. "cardNo": cardNo,
  109. "centerBack": "",
  110. "centerFront": "",
  111. "measureLength": 0.0,
  112. "measureWidth": 0.0,
  113. "cornerBackNum": 0,
  114. "sideBackNum": 0,
  115. "surfaceBackNum": 0,
  116. "cornerFrontNum": 0,
  117. "sideFrontNum": 0,
  118. "surfaceFrontNum": 0,
  119. "scoreThreshold": float(card_data.get("detection_score") or 0),
  120. "evaluateNo": str(card_data.get("id")),
  121. "defectDetailList": []
  122. }
  123. # 临时列表用于收集所有缺陷,最后排序取 Top N
  124. all_defects_collected = []
  125. # 遍历图片寻找 Front Ring 和 Back Ring
  126. images = card_data.get("images", [])
  127. # 辅助字典:defect_type 到 统计字段 的映射
  128. defect_map_keys = {
  129. "front_ring": {
  130. "corner": "cornerFrontNum",
  131. "edge": "sideFrontNum",
  132. "face": "surfaceFrontNum"
  133. },
  134. "back_ring": {
  135. "corner": "cornerBackNum",
  136. "edge": "sideBackNum",
  137. "face": "surfaceBackNum"
  138. }
  139. }
  140. for img in images:
  141. img_type = img.image_type
  142. # 只处理环光图
  143. if img_type not in ["front_ring", "back_ring"]:
  144. continue
  145. # 设置主图 URL
  146. if img_type == "front_ring":
  147. response_data["frontImageUrl"] = img.image_path
  148. elif img_type == "back_ring":
  149. response_data["backImageUrl"] = img.image_path
  150. # 获取有效 JSON
  151. json_data = _get_active_json(img)
  152. if not json_data or "result" not in json_data:
  153. continue
  154. result_node = json_data["result"]
  155. # 1. 处理居中 (Center)
  156. center_inf = result_node.get("center_result", {}).get("box_result", {}).get("center_inference", {})
  157. if center_inf:
  158. # 格式: L/R=47/53, T/B=51/49 (取整)
  159. # center_inference 包含 center_left, center_right, center_top, center_bottom
  160. c_str = (
  161. f"L/R={int(round(center_inf.get('center_left', 0)))}/{int(round(center_inf.get('center_right', 0)))}, "
  162. f"T/B={int(round(center_inf.get('center_top', 0)))}/{int(round(center_inf.get('center_bottom', 0)))}"
  163. )
  164. if img_type == "front_ring":
  165. response_data["centerFront"] = c_str
  166. # 2. 处理尺寸 (仅从正面取,或者只要有就取) - mm 转 cm,除以 10,保留2位
  167. rw_mm = center_inf.get("real_width_mm", 0)
  168. rh_mm = center_inf.get("real_height_mm", 0)
  169. response_data["measureWidth"] = round(rw_mm / 10.0, 2)
  170. response_data["measureLength"] = round(rh_mm / 10.0, 2)
  171. else:
  172. response_data["centerBack"] = c_str
  173. # 2. 处理缺陷 (Defects)
  174. defects = result_node.get("defect_result", {}).get("defects", [])
  175. for defect in defects:
  176. # 过滤 edit_type == 'del'
  177. if defect.get("edit_type") == "del":
  178. continue
  179. d_type = defect.get("defect_type", "") # corner, edge, face
  180. d_label = defect.get("label", "") # scratch, wear, etc.
  181. # 统计数量
  182. count_key = defect_map_keys.get(img_type, {}).get(d_type)
  183. if count_key:
  184. response_data[count_key] += 1
  185. # 收集详细信息用于 Top N 列表
  186. # 需要保存:缺陷对象本身,图片路径,正反面标识
  187. side_str = "FRONT" if img_type == "front_ring" else "BACK"
  188. all_defects_collected.append({
  189. "defect_data": defect,
  190. "image_path": img.image_path,
  191. "side": side_str,
  192. "area": defect.get("actual_area", 0)
  193. })
  194. # 3. 处理 defectDetailList (Top N 切图)
  195. # 按实际面积从大到小排序
  196. all_defects_collected.sort(key=lambda x: x["area"], reverse=True)
  197. top_defects = all_defects_collected[:top_n_defects]
  198. final_defect_list = []
  199. for idx, item in enumerate(top_defects, start=1):
  200. defect = item["defect_data"]
  201. side = item["side"]
  202. original_img_path = item["image_path"]
  203. # 构造 ID
  204. d_id = idx # 1, 2, 3
  205. # 构造文件名: {card_id}_{seq_id}.jpg
  206. filename = f"{card_id}_{d_id}.jpg"
  207. # 执行切图
  208. min_rect = defect.get("min_rect")
  209. defect_img_url = ""
  210. location_str = ""
  211. if min_rect and len(min_rect) == 3:
  212. # 切图并保存
  213. defect_img_url = _crop_defect_image(original_img_path, min_rect, filename)
  214. # 计算 Location (中心坐标)
  215. # min_rect[0] 是 [x, y]
  216. cx, cy = min_rect[0]
  217. location_str = f"{int(cx)},{int(cy)}"
  218. # 构造 Type 字符串: defect_type + label (大写)
  219. # 例如: defect_type="edge", label="wear" -> "EDGE WEAR"
  220. d_type_raw = defect.get("defect_type", "")
  221. d_label_raw = defect.get("label", "")
  222. type_str = f"{d_type_raw.upper()} {d_label_raw.upper()}".strip()
  223. final_defect_list.append({
  224. "id": d_id,
  225. "side": side,
  226. "location": location_str,
  227. "type": type_str,
  228. "defectImgUrl": defect_img_url
  229. })
  230. response_data["defectDetailList"] = final_defect_list
  231. return response_data