添加人脸检测接口

This commit is contained in:
haotian 2025-09-25 10:36:20 +08:00
parent a28f0b1dff
commit b0186dfbb2
5 changed files with 68 additions and 8 deletions

View File

@ -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-------------------

View File

@ -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-----------------------------------------

View File

@ -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

View File

@ -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)

View File

@ -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):
@ -81,6 +82,22 @@ class ComprefaceUtil:
, 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):