92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
简单示例:人脸特征提取
|
|
"""
|
|
|
|
import cv2
|
|
import sys
|
|
import os
|
|
|
|
# 添加父目录到路径
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from face_feature_extractor import FaceFeatureExtractor
|
|
import numpy as np
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
|
|
# 示例1: 基础使用
|
|
print("=" * 60)
|
|
print("示例1: 基础人脸特征提取")
|
|
print("=" * 60)
|
|
|
|
# 读取图像
|
|
image_path = r"E:\FaceFeatureExtractorAPI\muti.png" # 请替换为实际图像路径
|
|
|
|
if not os.path.exists(image_path):
|
|
print(f"错误: 图像文件不存在: {image_path}")
|
|
print("请提供有效的图像路径")
|
|
return
|
|
|
|
image = cv2.imread(image_path)
|
|
if image is None:
|
|
print(f"错误: 无法读取图像: {image_path}")
|
|
return
|
|
|
|
# 创建特征提取器
|
|
extractor = FaceFeatureExtractor()
|
|
|
|
# 提取特征
|
|
feature = extractor.extract_single_feature(image)
|
|
|
|
if feature is not None:
|
|
print(f"✓ 特征提取成功")
|
|
print(f" 特征维度: {feature.shape}")
|
|
print(f" 特征范数: {np.linalg.norm(feature):.6f}")
|
|
print(f" 特征类型: {feature.dtype}")
|
|
else:
|
|
print("✗ 特征提取失败(质量检查未通过或未检测到人脸)")
|
|
|
|
# 示例2: 详细信息提取
|
|
print("\n" + "=" * 60)
|
|
print("示例2: 获取详细信息")
|
|
print("=" * 60)
|
|
|
|
result = extractor.extract_features(image, return_all_faces=True, quality_filter=False)
|
|
|
|
print(f"处理时间: {result.processing_time:.3f}秒")
|
|
print(f"检测到 {len(result.faces)} 个人脸")
|
|
|
|
for i, face in enumerate(result.faces):
|
|
print(f"\n人脸 {i+1}:")
|
|
print(f" 位置: ({face.bbox[0]:.1f}, {face.bbox[1]:.1f}, {face.bbox[2]:.1f}, {face.bbox[3]:.1f})")
|
|
print(f" 置信度: {face.confidence:.3f}")
|
|
|
|
print(" 质量评估:")
|
|
for name, score in face.quality_scores.items():
|
|
if name != 'overall':
|
|
status = "✓" if score['passed'] else "✗"
|
|
print(f" {status} {score['description']}")
|
|
|
|
overall_status = "✓ 合格" if face.quality_scores['overall']['passed'] else "✗ 不合格"
|
|
print(f" 整体评估: {overall_status}")
|
|
|
|
# 示例3: 统计信息
|
|
print("\n" + "=" * 60)
|
|
print("示例3: 统计信息")
|
|
print("=" * 60)
|
|
|
|
stats = extractor.get_statistics()
|
|
print(f"总处理次数: {stats['total_extractions']}")
|
|
print(f"成功次数: {stats['successful_extractions']}")
|
|
print(f"质量过滤次数: {stats['quality_filtered']}")
|
|
print(f"成功率: {stats['success_rate']:.2%}")
|
|
print(f"平均处理时间: {stats['average_processing_time']:.3f}秒")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|