2024-09-12 17:14:01 +08:00
|
|
|
|
from fastapi import FastAPI, HTTPException
|
2024-07-31 15:32:50 +08:00
|
|
|
|
from pydantic import BaseModel
|
2024-09-12 17:14:01 +08:00
|
|
|
|
from process import FaceHelper, FileError, ErrorMsg
|
2024-07-31 15:32:50 +08:00
|
|
|
|
import threading
|
2024-09-12 17:14:01 +08:00
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
# 创建 FastAPI 实例
|
2024-07-31 15:32:50 +08:00
|
|
|
|
app = FastAPI()
|
2024-09-12 17:14:01 +08:00
|
|
|
|
|
|
|
|
|
|
# 创建线程锁,确保在多线程环境中对共享资源的访问是线程安全的
|
2024-07-31 15:32:50 +08:00
|
|
|
|
lock = threading.Lock()
|
|
|
|
|
|
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 全局变量 facehelper,稍后将用于管理人脸识别相关操作
|
|
|
|
|
|
facehelper = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_facehelper():
|
|
|
|
|
|
"""
|
|
|
|
|
|
懒加载模式初始化 facehelper 对象。
|
|
|
|
|
|
只有在第一次调用时才会创建 FaceHelper 实例。
|
|
|
|
|
|
使用线程锁以确保 facehelper 实例在多线程环境下的安全初始化。
|
|
|
|
|
|
"""
|
|
|
|
|
|
global facehelper
|
|
|
|
|
|
if facehelper is None:
|
|
|
|
|
|
with lock:
|
|
|
|
|
|
if facehelper is None:
|
|
|
|
|
|
facehelper = FaceHelper(db_dir="./dbface")
|
|
|
|
|
|
return facehelper
|
|
|
|
|
|
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 定义请求模型,用于接收前端传递的图像数据
|
|
|
|
|
|
class FaceRequest(BaseModel):
|
|
|
|
|
|
img: str
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 定义请求模型,用于接收前端传递的数据库操作数据
|
|
|
|
|
|
class DBFaceRequest(BaseModel):
|
|
|
|
|
|
img: str
|
|
|
|
|
|
optMode: str
|
|
|
|
|
|
uniqueKey: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 接两图片对比的参数
|
|
|
|
|
|
class ImageComparison(BaseModel):
|
|
|
|
|
|
firstImage: str
|
|
|
|
|
|
secondImage: str
|
|
|
|
|
|
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
|
|
|
|
|
@app.post("/refreshdb")
|
|
|
|
|
|
def refresh():
|
2024-09-12 17:14:01 +08:00
|
|
|
|
"""
|
|
|
|
|
|
刷新人脸数据库的接口。
|
|
|
|
|
|
该接口将调用 facehelper 的 updateDB 方法,刷新数据库内容。
|
|
|
|
|
|
"""
|
|
|
|
|
|
facehelper = get_facehelper()
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 加锁确保数据库更新操作的线程安全性
|
2024-07-31 15:32:50 +08:00
|
|
|
|
with lock:
|
|
|
|
|
|
facehelper.updateDB(None, None, None, Onlyrefresh=True)
|
2024-09-12 17:14:01 +08:00
|
|
|
|
except FileError as e:
|
|
|
|
|
|
# 处理文件错误,返回相应的错误代码和信息
|
|
|
|
|
|
return {'code': e.code, 'msg': str(e), 'data': None}
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 成功刷新数据库后返回相应的状态码和消息
|
|
|
|
|
|
return {'code': "30002", 'msg': ErrorMsg['30002'], 'data': None}
|
|
|
|
|
|
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
2024-08-02 12:04:38 +08:00
|
|
|
|
@app.post("/facerecognition")
|
2024-09-12 17:14:01 +08:00
|
|
|
|
def faceRecognition(input: FaceRequest):
|
|
|
|
|
|
"""
|
|
|
|
|
|
进行人脸识别的接口。
|
|
|
|
|
|
接收前端传递的图像数据,并使用 facehelper 进行人脸识别。
|
|
|
|
|
|
返回识别结果和运行时间。
|
|
|
|
|
|
"""
|
|
|
|
|
|
facehelper = get_facehelper()
|
|
|
|
|
|
start = time.time() # 记录开始时间
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 调用 facehelper 的 faceRecognition 方法进行人脸识别
|
2024-07-31 15:32:50 +08:00
|
|
|
|
ret_data = facehelper.faceRecognition(input.img)
|
|
|
|
|
|
except Exception as e:
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 处理异常,返回相应的错误代码和信息
|
|
|
|
|
|
raise HTTPException(status_code=400, detail={'code': e.code, 'msg': str(e), 'data': None})
|
|
|
|
|
|
finally:
|
|
|
|
|
|
end = time.time() # 记录结束时间
|
|
|
|
|
|
# 打印运行时间
|
|
|
|
|
|
print(f"Recognition finished. Runtime: {end - start:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
# 返回识别结果
|
|
|
|
|
|
return ret_data
|
|
|
|
|
|
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
2024-08-02 12:04:38 +08:00
|
|
|
|
@app.post("/featuredetect")
|
2024-09-12 17:14:01 +08:00
|
|
|
|
def featureDetect(input: FaceRequest):
|
|
|
|
|
|
"""
|
|
|
|
|
|
特征检测接口。
|
|
|
|
|
|
接收前端传递的图像数据,并使用 facehelper 进行特征检测。
|
|
|
|
|
|
返回检测结果和运行时间。
|
|
|
|
|
|
"""
|
|
|
|
|
|
facehelper = get_facehelper()
|
|
|
|
|
|
start = time.time() # 记录开始时间
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 调用 facehelper 的 featureDetect 方法进行特征检测
|
2024-08-02 12:04:38 +08:00
|
|
|
|
ret_data = facehelper.featureDetect(input.img)
|
|
|
|
|
|
except Exception as e:
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 处理异常,返回相应的错误代码和信息
|
|
|
|
|
|
raise HTTPException(status_code=400, detail={'code': e.code, 'msg': str(e), 'data': None})
|
|
|
|
|
|
finally:
|
|
|
|
|
|
end = time.time() # 记录结束时间
|
|
|
|
|
|
# 打印运行时间
|
|
|
|
|
|
print(f"Feature detection finished. Runtime: {end - start:.2f} seconds")
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 返回检测结果
|
|
|
|
|
|
return ret_data
|
2024-07-31 15:32:50 +08:00
|
|
|
|
|
2024-09-12 17:14:01 +08:00
|
|
|
|
|
|
|
|
|
|
@app.post("/updatedb")
|
|
|
|
|
|
def updateDB(input: DBFaceRequest):
|
|
|
|
|
|
"""
|
|
|
|
|
|
更新人脸数据库的接口。
|
|
|
|
|
|
接收前端传递的数据库操作数据,并使用 facehelper 更新数据库。
|
|
|
|
|
|
"""
|
|
|
|
|
|
facehelper = get_facehelper()
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 加锁确保数据库更新操作的线程安全性
|
2024-07-31 15:32:50 +08:00
|
|
|
|
with lock:
|
2024-08-02 12:04:38 +08:00
|
|
|
|
facehelper.updateDB(input.img, input.optMode, input.uniqueKey)
|
2024-07-31 15:32:50 +08:00
|
|
|
|
except Exception as e:
|
2024-09-12 17:14:01 +08:00
|
|
|
|
# 处理异常,返回相应的错误代码和信息
|
|
|
|
|
|
raise HTTPException(status_code=400, detail={'code': e.code, 'msg': str(e), 'data': None})
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 成功更新数据库后返回相应的状态码和消息
|
|
|
|
|
|
return {'code': "30002", 'msg': ErrorMsg['30002'], 'data': None}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/imageComparison")
|
|
|
|
|
|
def imageComparison(input: ImageComparison):
|
|
|
|
|
|
"""
|
|
|
|
|
|
更新人脸数据库的接口。
|
|
|
|
|
|
接收前端传递的数据库操作数据,并使用 facehelper 更新数据库。
|
|
|
|
|
|
"""
|
|
|
|
|
|
facehelper = get_facehelper()
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 加锁确保数据库更新操作的线程安全性
|
|
|
|
|
|
with lock:
|
|
|
|
|
|
state = facehelper.imageComparison(input.firstImage, input.secondImage)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
# 处理异常,返回相应的错误代码和信息
|
|
|
|
|
|
raise HTTPException(status_code=400, detail={'code': e.code, 'msg': str(e), 'data': None})
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 成功更新数据库后返回相应的状态码和消息
|
|
|
|
|
|
return {'code': "30002", 'msg': ErrorMsg['30002'], 'data': bool(state)}
|
|
|
|
|
|
|
|
|
|
|
|
|