识别结果保存到对应目录下

This commit is contained in:
2025-09-09 16:30:12 +08:00
parent 0fe49bf829
commit 532a9e75e9
6 changed files with 375 additions and 325 deletions

View File

@ -6,203 +6,217 @@ import time
import threading
from PIL import Image
from insightface.app import FaceAnalysis
# 导入获取人脸信息的服务
# 假设service.face_service中get_all_face_name_with_eigenvalue可获取人脸数据
from service.face_service import get_all_face_name_with_eigenvalue
# 用于检查GPU状态
# GPU状态检查支持
try:
import pynvml
pynvml.nvmlInit()
_nvml_available = True
except ImportError:
print("警告: pynvml库未安装无法检测GPU状态、将默认使用0号GPU")
print("警告: pynvml库未安装无法检测GPU状态,默认尝试使用GPU")
_nvml_available = False
# 全局变量
# 全局人脸引擎与特征库
_face_app = None
_known_faces_embeddings = {} # 存储姓名到特征值的映射
_known_faces_names = [] # 存储所有已知姓名
_using_gpu = False # 标记是否使用GPU
_used_gpu_id = -1 # 记录当前使用的GPU ID
_known_faces_embeddings = {} # 姓名 -> 归一化特征值的映射
_known_faces_names = [] # 已知人脸姓名列表
# GPU使用状态标记
_using_gpu = False # 是否使用GPU
_used_gpu_id = -1 # 使用的GPU ID-1表示CPU
# 资源管理变量
_ref_count = 0
_last_used_time = 0
_lock = threading.Lock()
_release_timeout = 8 # 5秒无使用则释放
_is_releasing = False # 标记是否正在释放
_ref_count = 0 # 引擎引用计数(记录当前使用次数)
_last_used_time = 0 # 最后一次使用引擎的时间
_lock = threading.Lock() # 线程安全锁
_release_timeout = 8 # 闲置超时时间(秒)
_is_releasing = False # 资源释放中标记
_monitor_thread_running = False # 监控线程运行标记
# 调试计数器
# 调试计数器
_debug_counter = {
"created": 0,
"released": 0,
"detected": 0
"engine_created": 0, # 引擎创建次数
"engine_released": 0, # 引擎释放次数
"detection_calls": 0 # 检测函数调用次数
}
def check_gpu_availability(gpu_id, threshold=0.7):
"""检查指定GPU是否可用(内存使用率低于阈值)"""
def check_gpu_availability(gpu_id, memory_threshold=0.7):
"""检查指定GPU内存使用率是否低于阈值(判定为“可用”"""
if not _nvml_available:
return True # 无法检测时默认认为可用
return True
try:
handle = pynvml.nvmlDeviceGetHandleByIndex(gpu_id)
mem_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
usage = mem_info.used / mem_info.total
# 内存使用率低于阈值则认为可用
return usage < threshold
memory_usage = mem_info.used / mem_info.total
return memory_usage < memory_threshold
except Exception as e:
print(f"检查GPU {gpu_id} 状态时出错: {e}")
print(f"检查GPU {gpu_id} 状态失败: {e}")
return False
def select_best_gpu(preferred_gpus=[0, 1]):
"""选择最佳可用GPU、严格按照首选列表顺序检查、优先使用0号GPU"""
# 首先检查首选GPU列表
"""按优先级选择可用GPU优先0号均不可用则返回-1CPU"""
for gpu_id in preferred_gpus:
try:
# 检查GPU是否存在
# 验证GPU是否存在
if _nvml_available:
pynvml.nvmlDeviceGetHandleByIndex(gpu_id)
# 检查GPU是否可用
# 验证GPU内存是否充足
if check_gpu_availability(gpu_id):
print(f"GPU {gpu_id} 可用将使用该GPU")
print(f"GPU {gpu_id} 可用将使用该GPU")
return gpu_id
else:
if gpu_id == 0:
print(f"GPU 0 内存使用率过高(繁忙)、尝试切换到其他GPU")
print("GPU 0 内存使用率过高,尝试其他GPU")
except Exception as e:
print(f"GPU {gpu_id}存在或无法访问: {e}")
continue
# 如果所有首选GPU都不可用、返回-1表示使用CPU
print("所有指定的GPU都不可用、将使用CPU进行计算")
print(f"GPU {gpu_id}可用或访问失败: {e}")
print("所有指定GPU均不可用将使用CPU计算")
return -1
def _release_engine():
"""释放人脸识别引擎资源"""
def _release_engine_resources():
"""释放人脸引擎的所有资源模型、特征库、GPU缓存等"""
global _face_app, _is_releasing, _known_faces_embeddings, _known_faces_names
if not _face_app or _is_releasing:
return
try:
_is_releasing = True
# 释放InsightFace资源
if hasattr(_face_app, 'model'):
# 清除模型资源
_face_app.model = None
_face_app = None
print("开始释放人脸引擎资源...")
# 清空人脸数据
# 释放InsightFace模型资源
if hasattr(_face_app, "model"):
_face_app.model = None # 显式置空模型引用
_face_app = None # 释放引擎实例
# 清空人脸特征库
_known_faces_embeddings.clear()
_known_faces_names.clear()
_debug_counter["released"] += 1
print(f"Face recognition engine released. Stats: {_debug_counter}")
_debug_counter["engine_released"] += 1
print(f"人脸引擎已释放,调试统计: {_debug_counter}")
# 清理GPU缓存
# 强制垃圾回收
gc.collect()
# 清理各深度学习框架的GPU缓存
# Torch 缓存清理
try:
import torch
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
print("Torch GPU缓存已清理")
except ImportError:
pass
# TensorFlow 缓存清理
try:
import tensorflow as tf
tf.keras.backend.clear_session()
print("TensorFlow会话已清理")
except ImportError:
pass
# MXNet 缓存清理InsightFace底层常用MXNet
try:
import mxnet as mx
mx.nd.waitall() # 等待所有计算完成并释放资源
print("MXNet资源已等待释放")
except ImportError:
pass
except Exception as e:
print(f"释放资源过程中出错: {e}")
finally:
_is_releasing = False
def _monitor_thread():
"""监控线程、检查并释放超时未使用的资源"""
global _ref_count, _last_used_time, _face_app
while True:
time.sleep(5) # 每5秒检查一次
def _resource_monitor_thread():
"""后台监控线程:检测引擎闲置超时,触发资源释放"""
global _ref_count, _last_used_time, _face_app, _monitor_thread_running
_monitor_thread_running = True
while _monitor_thread_running:
time.sleep(2) # 缩短检查间隔,加快闲置检测响应
with _lock:
# 只有当引擎存在、没有引用且超时、才释放
# 当引擎存在 + 无引用 + 未在释放中”时,检查闲置时间
if _face_app and _ref_count == 0 and not _is_releasing:
elapsed = time.time() - _last_used_time
if elapsed > _release_timeout:
print(f"Idle timeout ({elapsed:.1f}s > {_release_timeout}s), releasing face engine")
_release_engine()
idle_time = time.time() - _last_used_time
if idle_time > _release_timeout:
print(f"引擎闲置超时({idle_time:.1f}s > {_release_timeout}s),释放资源")
_release_engine_resources()
def load_model(prefer_gpu=True, preferred_gpus=[0, 1]):
"""加载人脸识别模型及已知人脸特征库默认优先使用0号GPU"""
"""加载人脸识别引擎及已知人脸特征库默认优先用0号GPU"""
global _face_app, _known_faces_embeddings, _known_faces_names, _using_gpu, _used_gpu_id
# 确保监控线程只启动一次
if not any(t.name == "FaceMonitor" for t in threading.enumerate()):
threading.Thread(target=_monitor_thread, daemon=True, name="FaceMonitor").start()
print("Face monitor thread started")
# 启动后台监控线程(确保仅启动一次
if not _monitor_thread_running:
threading.Thread(
target=_resource_monitor_thread,
daemon=True,
name="FaceEngineMonitor"
).start()
print("人脸引擎监控线程已启动")
# 如果正在释放中、等待释放完成
# 正在释放资源,等待释放完成
while _is_releasing:
time.sleep(0.1)
# 如果已经初始化直接返回
# 若引擎已初始化直接返回
if _face_app:
return True
# 初始化InsightFace模型
# 初始化InsightFace引擎
try:
# 初始化InsightFace
print("正在初始化InsightFace人脸识别引擎...")
_face_app = FaceAnalysis(name='buffalo_l', root='~/.insightface')
_face_app = FaceAnalysis(name="buffalo_l", root=os.path.expanduser("~/.insightface"))
# 选择合适的GPU、默认优先使用0号
# 选择GPU优先用0号
ctx_id = 0
if prefer_gpu:
ctx_id = select_best_gpu(preferred_gpus)
_using_gpu = ctx_id != -1
_used_gpu_id = ctx_id if _using_gpu else -1
if _using_gpu:
print(f"成功初始化使用GPU {ctx_id} 进行计算")
else:
print("成功初始化使用CPU进行计算")
if _using_gpu:
print(f"引擎初始化成功,将使用GPU {ctx_id} 计算")
else:
print("引擎初始化成功,将使用CPU计算")
# 准备模型
# 准备模型(加载到指定设备)
_face_app.prepare(ctx_id=ctx_id, det_size=(640, 640))
print("InsightFace人脸识别引擎初始化成功。")
_debug_counter["created"] += 1
print(f"Face engine initialized. Stats: {_debug_counter}")
print("InsightFace引擎初始化")
_debug_counter["engine_created"] += 1
print(f"引擎调试统计: {_debug_counter}")
except Exception as e:
print(f"初始化失败: {e}")
print(f"引擎初始化失败: {e}")
return False
# 从服务获取所有人脸姓名和特征值
# 从服务加载已知人脸姓名和特征值
try:
face_data = get_all_face_name_with_eigenvalue()
# 处理获取到的人脸数据
for person_name, eigenvalue_data in face_data.items():
# 处理特征值数据 - 兼容数组和字符串两种格式
# 兼容“numpy数组”和“字符串”格式的特征值
if isinstance(eigenvalue_data, np.ndarray):
# 如果已经是numpy数组、直接使用
eigenvalue = eigenvalue_data.astype(np.float32)
elif isinstance(eigenvalue_data, str):
# 清理字符串: 移除方括号、换行符和多余空格
cleaned = eigenvalue_data.replace('[', '').replace(']', '').replace('\n', '').strip()
# 按空格或逗号分割(处理可能的不同分隔符)
values = [v for v in cleaned.split() if v]
# 转换为数组
# 清理字符串中的括号、换行等干扰符
cleaned = eigenvalue_data.replace("[", "").replace("]", "").replace("\n", "").strip()
# 分割并转换为浮点数数组
values = [v for v in cleaned.split() if v] # 兼容空格/逗号分隔
eigenvalue = np.array(list(map(float, values)), dtype=np.float32)
else:
# 不支持的类型
print(f"Unsupported eigenvalue type for {person_name}")
print(f"不支持的特征值类型({type(eigenvalue_data)}),跳过 {person_name}")
continue
# 归一化处理
# 特征值归一化(保证后续相似度计算的一致性)
norm = np.linalg.norm(eigenvalue)
if norm != 0:
eigenvalue = eigenvalue / norm
@ -210,100 +224,103 @@ def load_model(prefer_gpu=True, preferred_gpus=[0, 1]):
_known_faces_embeddings[person_name] = eigenvalue
_known_faces_names.append(person_name)
print(f"成功加载 {len(_known_faces_names)} 个人脸的特征库")
except Exception as e:
print(f"Error loading face data from service: {e}")
print(f"加载人脸特征库失败: {e}")
return True if _face_app else False
return _face_app is not None
def detect(frame, threshold=0.4):
"""检测并识别人脸、返回结果元组(是否匹配到已知人脸, 结果字符串)"""
global _face_app, _known_faces_embeddings, _known_faces_names, _using_gpu, _used_gpu_id
global _ref_count, _last_used_time
def detect(frame, similarity_threshold=0.4):
"""
检测并识别人脸
返回:(是否匹配到已知人脸, 结果描述字符串)
"""
global _face_app, _known_faces_embeddings, _known_faces_names, _ref_count, _last_used_time
# 验证前置条件
# 校验输入帧有效性
if frame is None or frame.size == 0:
return (False, "无效帧数据")
return (False, "无效的输入帧数据")
# 增加引用计数并获取引擎实例
# 加锁并更新引用计数、最后使用时间
engine = None
with _lock:
_ref_count += 1
_last_used_time = time.time()
_debug_counter["detected"] += 1
_debug_counter["detection_calls"] += 1
# 初始化引擎(如果未初始化且在释放中
# 若引擎未初始化且在释放中,尝试初始化
if not _face_app and not _is_releasing:
if not load_model(prefer_gpu=True):
_ref_count -= 1 # 恢复引用计数
return (False, "引擎初始化失败")
# 初始化失败,恢复引用计数
with _lock:
_ref_count = max(0, _ref_count - 1)
return (False, "人脸引擎初始化失败")
# 获取当前引擎引用
engine = _face_app
engine = _face_app # 获取引擎引用
# 检查引擎是否可用
if not engine or not _known_faces_names:
# 校验引擎可用
if not engine or len(_known_faces_names) == 0:
with _lock:
_ref_count = max(0, _ref_count - 1)
return (False, "人脸识别引擎不可用或未初始化")
return (False, "人脸引擎不可用或特征库为空")
try:
# 如果使用GPU、确保输入帧在处理前是连续的数组
if _using_gpu and not frame.flags.contiguous:
# GPU计算时确保帧数据是连续内存避免CUDA错误
if _using_gpu and engine is not None and not frame.flags.contiguous:
frame = np.ascontiguousarray(frame)
faces = _face_app.get(frame)
# 执行人脸检测与特征提取
faces = engine.get(frame)
except Exception as e:
print(f"Face detect error: {e}")
# 检测到错误时尝试重新选择GPU并重新初始化
print("尝试重新选择GPU并重新初始化...")
print(f"人脸检测过程出错: {e}")
# 出错时尝试重新初始化引擎可能是GPU状态变化导致
print("尝试重新初始化人脸引擎...")
with _lock:
_ref_count = max(0, _ref_count - 1)
load_model(prefer_gpu=True) # 重新初始化时保持默认GPU优先级
load_model(prefer_gpu=True)
return (False, f"检测错误: {str(e)}")
result_parts = []
has_matched = False # 标记是否有匹配到已知人脸
has_matched_known_face = False # 是否有任意人脸匹配到已知
for face in faces:
# 特征归一化
embedding = face.embedding.astype(np.float32)
norm = np.linalg.norm(embedding)
# 归一化当前检测到的人脸特征
face_embedding = face.embedding.astype(np.float32)
norm = np.linalg.norm(face_embedding)
if norm == 0:
continue
embedding = embedding / norm
face_embedding = face_embedding / norm
# 对比已知人脸
max_sim, best_name = -1.0, "Unknown"
# 已知人脸特征逐一比对
max_similarity, best_match_name = -1.0, "Unknown"
for name in _known_faces_names:
known_emb = _known_faces_embeddings[name]
sim = np.dot(embedding, known_emb)
if sim > max_sim:
max_sim = sim
best_name = name
similarity = np.dot(face_embedding, known_emb) # 余弦相似度
if similarity > max_similarity:
max_similarity = similarity
best_match_name = name
# 判断匹配结果
is_match = max_sim >= threshold
if is_match:
has_matched = True # 只要有一个匹配成功、就标记为True
# 判断是否匹配成功
is_matched = max_similarity >= similarity_threshold
if is_matched:
has_matched_known_face = True
bbox = face.bbox
# 记录该人脸的检测结果
bbox = face.bbox # 人脸边界框
result_parts.append(
f"{'匹配' if is_match else '匹配'}: {best_name} (相似度: {max_sim:.2f}, 边界框: {bbox})"
f"{'匹配' if is_matched else '匹配'}: {best_match_name} "
f"(相似度: {max_similarity:.2f}, 边界框: {bbox.astype(int).tolist()})"
)
# 构建结果字符串
if not result_parts:
result_str = "未检测到人脸"
else:
result_str = "; ".join(result_parts)
# 构建最终结果字符串
result_str = "未检测到人脸" if not result_parts else "; ".join(result_parts)
# 减少引用计数、确保线程安全
# 释放引用计数线程安全
with _lock:
_ref_count = max(0, _ref_count - 1)
# 持续使用时更新最后使用时间
if _ref_count > 0:
_last_used_time = time.time()
# 若仍有引用,更新最后使用时间若引用为0也立即标记加快闲置检测
_last_used_time = time.time()
# 第一个返回值为: 是否匹配到已知人脸
return (has_matched, result_str)
return (has_matched_known_face, result_str)