From b0186dfbb2de6378f375140c0facd1cd95e9bed2 Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Thu, 25 Sep 2025 10:36:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=BA=E8=84=B8=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-fastapi-backend/.env.dev | 1 + .../004测试compreface方法.py | 38 +++++++++++++++---- ruoyi-fastapi-backend/config/env.py | 1 + .../controller/compreface_controller.py | 17 +++++++++ .../utils/compreface_util.py | 19 +++++++++- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/ruoyi-fastapi-backend/.env.dev b/ruoyi-fastapi-backend/.env.dev index c01d3d6..7a87d0e 100644 --- a/ruoyi-fastapi-backend/.env.dev +++ b/ruoyi-fastapi-backend/.env.dev @@ -135,6 +135,7 @@ RAGFLOW_API_KEY = "ragflow-hlMjRmNzE2ODNiNTExZjA4ZTNlMDI0Mm" COMPREFACE_BASE_URL = "http://10.0.0.202" COMPERFACE_BASE_PORT = "8000" COMPREFACE_API_KEY = "ca1c8b20-a914-4e9e-8674-3cc3d04b3132" +COMPREFACE_API_KEY_DETECTION = "8437c714-43cd-42f6-8b18-a2114b024e57" COMPREFACE_FACE_FILE = "./image_face" COMPREFACE_SIMILARITY_THRESHOLD = 0.995 #-------------------compreface配置end------------------- \ No newline at end of file diff --git a/ruoyi-fastapi-backend/004测试compreface方法.py b/ruoyi-fastapi-backend/004测试compreface方法.py index eab3a47..41c9dc0 100644 --- a/ruoyi-fastapi-backend/004测试compreface方法.py +++ b/ruoyi-fastapi-backend/004测试compreface方法.py @@ -11,23 +11,47 @@ with open(image_path, "rb") as f: # # 测试上传人脸图片 # print(asyncio.run(ComprefaceUtil.face_addition(image_path, '刘昊天_访客'))) -# 测试人脸识别 +# #-----------------------------------------测试人脸识别-------------------------------------------- +# start_time = time.time() +# result = asyncio.run( +# ComprefaceUtil.face_recognition( +# image_bytes, +# options={ +# "limit": 0, +# "det_prob_threshold": 0.8, +# "prediction_count": 1, +# # 可选参数 age,gender,landmarks,calculator +# "face_plugins": "age,gender,landmarks", +# "status": "true", +# }, +# ) +# ) +# print(result) +# with open("compreface_face_recognition.json", "w", encoding="utf-8") as f: +# f.write(json.dumps(result, ensure_ascii=False, indent=4)) + +# print("spend time:", time.time() - start_time) + +# #-----------------------------------------测试人脸识别end------------------------------------------ + +# -----------------------------------------测试人脸检测-------------------------------------------- start_time = time.time() result = asyncio.run( - ComprefaceUtil.face_recognition( + ComprefaceUtil.face_detection( image_bytes, options={ "limit": 0, "det_prob_threshold": 0.8, "prediction_count": 1, # 可选参数 age,gender,landmarks,calculator - "face_plugins": "age,gender,landmarks", - "status": "true", - }, + "face_plugins": "pose", + "status": "false", + } ) - ) +) print(result) -with open("compreface_face_recognition.json", "w", encoding="utf-8") as f: +with open("compreface_face_detection.json", "w", encoding="utf-8") as f: f.write(json.dumps(result, ensure_ascii=False, indent=4)) print("spend time:", time.time() - start_time) +# -----------------------------------------测试人脸检测end----------------------------------------- diff --git a/ruoyi-fastapi-backend/config/env.py b/ruoyi-fastapi-backend/config/env.py index cdf05d2..8c802a4 100644 --- a/ruoyi-fastapi-backend/config/env.py +++ b/ruoyi-fastapi-backend/config/env.py @@ -156,6 +156,7 @@ class ComprefaceSettings: COMPREFACE_BASE_URL = "http://10.0.0.202" COMPERFACE_BASE_PORT = "8000" COMPREFACE_API_KEY = "ca1c8b20-a914-4e9e-8674-3cc3d04b3132" + COMPREFACE_API_KEY_DETECTION = "8437c714-43cd-42f6-8b18-a2114b024e57" COMPREFACE_FACE_FILE = "./image_face" COMPREFACE_SIMILARITY_THRESHOLD = 0.995 diff --git a/ruoyi-fastapi-backend/module_admin/controller/compreface_controller.py b/ruoyi-fastapi-backend/module_admin/controller/compreface_controller.py index 3f0a16f..8f08b7a 100644 --- a/ruoyi-fastapi-backend/module_admin/controller/compreface_controller.py +++ b/ruoyi-fastapi-backend/module_admin/controller/compreface_controller.py @@ -13,6 +13,23 @@ comprefaceController = APIRouter(prefix='/system/compreface' , dependencies=[Depends(LoginService.get_current_user)] ) +# 人脸检测 +@comprefaceController.post('/face_detection') +async def face_detection(request: Request, file: UploadFile = File(None)) -> dict: + """ + 人脸检测 + + """ + if file: # 说明是表单上传 + image = await file.read() + + else: # 尝试按字节流读取 + image = await request.body() + + result = await ComprefaceService.face_detection_service(image) + print(result) + return ResponseUtil.success(data=result) + # 人脸识别 @comprefaceController.post('/face_recognition') # @Log(title='人脸识别', business_type=BusinessType.OTHER) diff --git a/ruoyi-fastapi-backend/utils/compreface_util.py b/ruoyi-fastapi-backend/utils/compreface_util.py index f0d51a2..e604cc6 100644 --- a/ruoyi-fastapi-backend/utils/compreface_util.py +++ b/ruoyi-fastapi-backend/utils/compreface_util.py @@ -33,6 +33,7 @@ compre_face: CompreFace = CompreFace(ComprefaceConfig.COMPREFACE_BASE_URL, Compr class ComprefaceUtil: api_key: str = ComprefaceConfig.COMPREFACE_API_KEY + api_key_detection: str = ComprefaceConfig.COMPREFACE_API_KEY_DETECTION @classmethod async def send_request(cls, method: str, url: str, headers: dict, body: str | None=None, data=None, files=None ,json=True): @@ -80,7 +81,23 @@ class ComprefaceUtil: , headers={'x-api-key': cls.api_key} , files=m) return result - + + # 人脸检测 + @classmethod + async def face_detection(cls, image: str = '' or bytes, options: AllOptionsDict = {}) -> dict: + client_url: str = DETECTION_API + url: str = ComprefaceConfig.COMPREFACE_BASE_URL + ':' + ComprefaceConfig.COMPERFACE_BASE_PORT + client_url+"?" + for key in options.keys(): + # Checks fields with necessary rules. + # key - key field by options. + # check_fields_by_name(key, options[key]) + url += '&' + key + "=" + str(options[key]) + m = await multipart_constructor(image) + result = await cls.send_request("post" + , url + , headers={'x-api-key': cls.api_key_detection} + , files=m) + return result async def get_file(image: str = '' or bytes): if not os.path.isfile(image):