实现异步调用compreface人脸识别服务
This commit is contained in:
parent
13d59b3c4f
commit
c078850c8b
@ -1,6 +0,0 @@
|
||||
from utils.compreface_util import ComprefaceUtil
|
||||
import asyncio
|
||||
|
||||
image_path = "./haotian.jpg"
|
||||
|
||||
print(asyncio.run(ComprefaceUtil.face_addition(image_path, '刘昊天_访客')))
|
||||
30
ruoyi-fastapi-backend/004测试compreface方法.py
Normal file
30
ruoyi-fastapi-backend/004测试compreface方法.py
Normal file
@ -0,0 +1,30 @@
|
||||
from utils.compreface_util import ComprefaceUtil
|
||||
import asyncio
|
||||
import time
|
||||
import json
|
||||
|
||||
image_path = "./haotian1.jpg"
|
||||
|
||||
# # 测试上传人脸图片
|
||||
# print(asyncio.run(ComprefaceUtil.face_addition(image_path, '刘昊天_访客')))
|
||||
|
||||
# 测试人脸识别
|
||||
start_time = time.time()
|
||||
result = asyncio.run(
|
||||
ComprefaceUtil.face_recognition(
|
||||
image_path,
|
||||
options={
|
||||
"limit": 0,
|
||||
"det_prob_threshold": 0.8,
|
||||
"prediction_count": 5,
|
||||
# 可选参数 age,gender,landmarks,calculator
|
||||
"face_plugins": "age,gender,landmarks",
|
||||
"status": "false",
|
||||
},
|
||||
)
|
||||
)
|
||||
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)
|
||||
@ -2,10 +2,9 @@ from config.env import ComprefaceConfig
|
||||
from compreface import CompreFace
|
||||
from compreface.service import RecognitionService
|
||||
from compreface.collections import FaceCollection
|
||||
from compreface.common.typed_dict import AllOptionsDict, check_fields_by_name
|
||||
|
||||
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
||||
|
||||
import requests
|
||||
import httpx
|
||||
import json
|
||||
import os
|
||||
@ -25,7 +24,7 @@ VERIFICATION_API: str = '/api/v1/verification'
|
||||
|
||||
|
||||
|
||||
# compre_face: CompreFace = CompreFace(ComprefaceConfig.COMPREFACE_BASE_URL, ComprefaceConfig.)
|
||||
compre_face: CompreFace = CompreFace(ComprefaceConfig.COMPREFACE_BASE_URL, ComprefaceConfig.COMPERFACE_BASE_PORT)
|
||||
# recognition: RecognitionService = compre_face.init_face_recognition(ComprefaceConfig.COMPREFACE_API_KEY)
|
||||
# face_collection: FaceCollection = recognition.get_face_collection()
|
||||
|
||||
@ -50,6 +49,7 @@ class ComprefaceUtil:
|
||||
return response.json()
|
||||
return response
|
||||
|
||||
# 添加人脸到指定分组下, subject 为分组名称
|
||||
@classmethod
|
||||
async def face_addition(cls, image: str = '' or bytes, subject: str = '') -> dict:
|
||||
client_url: str = RECOGNIZE_CRUD_API
|
||||
@ -61,7 +61,33 @@ class ComprefaceUtil:
|
||||
, headers={'x-api-key': cls.api_key}
|
||||
, files=m)
|
||||
return result
|
||||
# 人脸识别
|
||||
@classmethod
|
||||
async def face_recognition(cls, image: str = '' or bytes, options: AllOptionsDict = {}) -> dict:
|
||||
# recognition: RecognitionService = compre_face.init_face_recognition(ComprefaceConfig.COMPREFACE_API_KEY)
|
||||
# print(recognition.recognize(image_path=image, options={
|
||||
# "limit": 0,
|
||||
# "det_prob_threshold": 0.8,
|
||||
# "prediction_count": 1,
|
||||
# "face_plugins": "calculator,age,gender,landmarks",
|
||||
# "status": "false"
|
||||
# }))
|
||||
|
||||
client_url: str = RECOGNIZE_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}
|
||||
, files=m)
|
||||
return result
|
||||
|
||||
|
||||
async def get_file(image: str = '' or bytes):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user