| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- # -*- coding: utf-8 -*-
- import base64
- from fastapi import APIRouter, Form, UploadFile
- from fastapi.responses import JSONResponse
- from digitalHuman.server.reponse import Response
- from digitalHuman.server.header import HeaderInfo
- from digitalHuman.server.models import FaceDetectionOutput
- # 延迟导入,避免启动时触发 uniface 导入(可能导致 DLL 加载失败)
- # 只在 API 调用时才导入
- def _get_face_detection_infer():
- """延迟导入 face_detection_infer"""
- from digitalHuman.server.core.api_face_detection_v0_impl import face_detection_infer
- return face_detection_infer
- router = APIRouter(prefix="/face_detection/v0")
- # ========================= 人脸检测 ===========================
- @router.post("/detect", summary="Face Detection")
- async def api_face_detection(header: HeaderInfo, file: UploadFile):
- """
- 执行人脸检测
- 接收图片文件,返回检测到的人脸信息
- """
- response = Response()
- try:
- file_data = await file.read()
- face_detection_infer = _get_face_detection_infer()
- result = await face_detection_infer(header, file_data)
- response.data = result
- except Exception as e:
- response.data = {"hasFace": False, "faceCount": 0, "faces": []}
- response.error(str(e))
- return JSONResponse(content=response.validate(FaceDetectionOutput), status_code=200)
- # ========================= 人脸检测 (Base64) ===========================
- @router.post("/detect/base64", summary="Face Detection (Base64)")
- async def api_face_detection_base64(header: HeaderInfo, image_data: str = Form(...)):
- """
- 执行人脸检测 (Base64格式)
- 接收Base64编码的图片数据,返回检测到的人脸信息
- """
- response = Response()
- try:
- # 解码Base64图片
- if image_data.startswith('data:image'):
- # 处理 data:image/jpeg;base64,xxx 格式
- image_data = image_data.split(',')[1]
- file_data = base64.b64decode(image_data)
- face_detection_infer = _get_face_detection_infer()
- result = await face_detection_infer(header, file_data)
- response.data = result
- except Exception as e:
- response.data = {"hasFace": False, "faceCount": 0, "faces": []}
- response.error(str(e))
- return JSONResponse(content=response.validate(FaceDetectionOutput), status_code=200)
|