chawin.chen commited on
Commit
a4fadd2
·
1 Parent(s): e2f9566
Files changed (1) hide show
  1. api_routes.py +42 -0
api_routes.py CHANGED
@@ -1126,6 +1126,48 @@ async def analyze_face(
1126
  }
1127
 
1128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1129
  @api_router.post(path="/analyze")
1130
  @log_api_params
1131
  async def analyze_face(
 
1126
  }
1127
 
1128
 
1129
+ @api_router.post("/detect_faces", tags=["Face API"])
1130
+ @log_api_params
1131
+ async def detect_faces_endpoint(
1132
+ file: UploadFile = File(..., description="需要进行人脸检测的图片"),
1133
+ ):
1134
+ """
1135
+ 上传单张图片,调用 YOLO(_detect_faces)做人脸检测并返回耗时。
1136
+ """
1137
+ if not file or not file.content_type or not file.content_type.startswith("image/"):
1138
+ raise HTTPException(status_code=400, detail="请上传有效的图片文件")
1139
+
1140
+ image_bytes = await file.read()
1141
+ if not image_bytes:
1142
+ raise HTTPException(status_code=400, detail="图片内容为空")
1143
+
1144
+ np_arr = np.frombuffer(image_bytes, np.uint8)
1145
+ image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
1146
+ if image is None:
1147
+ raise HTTPException(status_code=400, detail="无法解析图片文件,请确认格式正确")
1148
+
1149
+ if analyzer is None:
1150
+ _ensure_analyzer()
1151
+ if analyzer is None:
1152
+ raise HTTPException(status_code=500, detail="人脸检测模型尚未就绪,请稍后再试")
1153
+
1154
+ detect_start = time.perf_counter()
1155
+ try:
1156
+ face_boxes = analyzer._detect_faces(image)
1157
+ except Exception as exc:
1158
+ logger.error(f"Face detection failed: {exc}")
1159
+ raise HTTPException(status_code=500, detail="调用人脸检测失败") from exc
1160
+ detect_duration = time.perf_counter() - detect_start
1161
+
1162
+ return {
1163
+ "success": True,
1164
+ "face_count": len(face_boxes),
1165
+ "boxes": face_boxes,
1166
+ "elapsed_ms": round(detect_duration * 1000, 3),
1167
+ "elapsed_seconds": round(detect_duration, 4),
1168
+ }
1169
+
1170
+
1171
  @api_router.post(path="/analyze")
1172
  @log_api_params
1173
  async def analyze_face(