优化代码风格

This commit is contained in:
ZZX9599
2025-09-08 17:34:23 +08:00
parent 9b3d20511a
commit 8ceb92c572
20 changed files with 223 additions and 192 deletions

View File

@ -27,7 +27,7 @@ def init_insightface():
def add_binary_data(binary_data):
"""
接收单张图片的二进制数据提取特征并保存
接收单张图片的二进制数据提取特征并保存
参数:
binary_data: 图片的二进制数据bytes类型
@ -39,11 +39,11 @@ def add_binary_data(binary_data):
global _insightface_app, _feature_list
if not _insightface_app:
print("引擎未初始化无法处理")
print("引擎未初始化无法处理")
return False, None
try:
# 直接处理二进制数据转换为图像格式
# 直接处理二进制数据: 转换为图像格式
img = Image.open(BytesIO(binary_data))
frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
@ -70,15 +70,15 @@ def get_average_feature(features=None):
计算多个特征向量的平均值
参数:
features: 可选特征值列表。如果未提供则使用全局存储的_feature_list
features: 可选特征值列表。如果未提供则使用全局存储的_feature_list
每个元素可以是字符串格式或numpy数组
返回:
单一平均特征向量的numpy数组若无可计算数据则返回None
单一平均特征向量的numpy数组若无可计算数据则返回None
"""
global _feature_list
# 如果未提供features参数则使用全局特征列表
# 如果未提供features参数则使用全局特征列表
if features is None:
features = _feature_list
@ -105,7 +105,7 @@ def get_average_feature(features=None):
processed_features.append(embedding_np)
print(f"已添加第 {i + 1} 个特征值用于计算平均值")
else:
print(f"跳过第 {i + 1} 个特征值不是一维数组")
print(f"跳过第 {i + 1} 个特征值不是一维数组")
except Exception as e:
print(f"处理第 {i + 1} 个特征值时出错: {e}")
@ -118,12 +118,12 @@ def get_average_feature(features=None):
# 检查所有特征向量维度是否相同
dims = {feat.shape[0] for feat in processed_features}
if len(dims) > 1:
print(f"特征值维度不一致无法计算平均值。检测到的维度: {dims}")
print(f"特征值维度不一致无法计算平均值。检测到的维度: {dims}")
return None
# 计算平均值
avg_feature = np.mean(processed_features, axis=0)
print(f"成功计算 {len(processed_features)} 个特征值的平均特征向量维度: {avg_feature.shape[0]}")
print(f"成功计算 {len(processed_features)} 个特征值的平均特征向量维度: {avg_feature.shape[0]}")
return avg_feature