This commit is contained in:
ZZX9599
2025-09-03 22:06:55 +08:00
parent 8a97bfa381
commit 14c70e3c76

View File

@ -234,3 +234,36 @@ async def delete_face(
raise Exception(f"删除人脸记录失败:{str(e)}") from e raise Exception(f"删除人脸记录失败:{str(e)}") from e
finally: finally:
db.close_connection(conn, cursor) db.close_connection(conn, cursor)
def get_all_face_name_with_eigenvalue() -> dict:
"""
获取所有人脸的名称及其对应的特征值,组成字典返回
key: 人脸名称name
value: 人脸特征值eigenvalue
过滤掉name为None的记录避免字典key为None的情况
"""
conn = None
cursor = None
try:
conn = db.get_connection()
cursor = conn.cursor(dictionary=True)
# 只查询需要的字段,提高效率
query = "SELECT name, eigenvalue FROM face WHERE name IS NOT NULL"
cursor.execute(query)
faces = cursor.fetchall()
# 构建name到eigenvalue的映射字典
face_dict = {
face["name"]: face["eigenvalue"]
for face in faces
}
return face_dict
except MySQLError as e:
raise Exception(f"获取人脸名称与特征值失败:{str(e)}") from e
finally:
# 确保资源释放
db.close_connection(conn, cursor)