Files
insightface/cpp-package/inspireface/python/README.md

103 lines
2.7 KiB
Markdown
Raw Normal View History

2025-03-25 00:51:26 +08:00
# InspireFace Python API
2025-06-15 01:41:04 +08:00
InspireFace 提供了简单易用的 Python API通过 ctypes 封装底层动态链接库实现。您可以通过 pip 安装最新发布版本,或使用项目自行编译的动态库进行配置。
2025-03-25 00:51:26 +08:00
2025-06-15 01:41:04 +08:00
## 快速安装
2025-03-25 00:51:26 +08:00
2025-06-15 01:41:04 +08:00
### 通过 pip 安装(推荐)
2025-03-25 00:51:26 +08:00
```bash
pip install inspireface
```
2025-06-15 01:41:04 +08:00
### 手动安装
2025-06-15 01:41:04 +08:00
1. 首先安装必要的依赖:
```bash
pip install loguru tqdm opencv-python
```
2025-06-15 01:41:04 +08:00
2. 将编译好的动态库复制到指定目录:
```bash
# 将编译好的动态库复制到对应系统架构目录
2025-03-25 00:51:26 +08:00
cp YOUR_BUILD_DIR/libInspireFace.so inspireface/modules/core/SYSTEM/CORE_ARCH/
```
2025-06-15 01:41:04 +08:00
3. 安装 Python 包:
```bash
2025-03-25 00:51:26 +08:00
python setup.py install
```
2025-06-15 01:41:04 +08:00
## 快速开始
2025-06-15 01:41:04 +08:00
以下是一个简单的示例,展示如何使用 InspireFace 进行人脸检测和关键点绘制:
2025-06-15 01:41:04 +08:00
```python
import cv2
2025-03-25 00:51:26 +08:00
import inspireface as isf
2025-06-15 01:41:04 +08:00
# 创建会话,启用所需功能
session = isf.InspireFaceSession(
opt=isf.HF_ENABLE_NONE, # 可选功能
detect_mode=isf.HF_DETECT_MODE_ALWAYS_DETECT # 检测模式
)
# 设置检测置信度阈值
2025-03-25 00:51:26 +08:00
session.set_detection_confidence_threshold(0.5)
2025-06-15 01:41:04 +08:00
# 读取图像
image = cv2.imread("path/to/your/image.jpg")
assert image is not None, "请检查图像路径是否正确"
2025-06-15 01:41:04 +08:00
# 执行人脸检测
faces = session.face_detection(image)
2025-06-15 01:41:04 +08:00
print(f"检测到 {len(faces)} 个人脸")
2025-06-15 01:41:04 +08:00
# 在图像上绘制检测结果
draw = image.copy()
for idx, face in enumerate(faces):
2025-06-15 01:41:04 +08:00
# 获取人脸框位置
x1, y1, x2, y2 = face.location
2025-06-15 01:41:04 +08:00
# 计算旋转框参数
2025-03-25 00:51:26 +08:00
center = ((x1 + x2) / 2, (y1 + y2) / 2)
size = (x2 - x1, y2 - y1)
angle = face.roll
2025-06-15 01:41:04 +08:00
# 绘制旋转框
2025-03-25 00:51:26 +08:00
rect = ((center[0], center[1]), (size[0], size[1]), angle)
box = cv2.boxPoints(rect)
box = box.astype(int)
cv2.drawContours(draw, [box], 0, (100, 180, 29), 2)
2025-06-15 01:41:04 +08:00
# 绘制关键点
landmarks = session.get_face_dense_landmark(face)
for x, y in landmarks.astype(int):
2025-03-25 00:51:26 +08:00
cv2.circle(draw, (x, y), 0, (220, 100, 0), 2)
```
2025-06-15 01:41:04 +08:00
## 更多示例
2025-06-15 01:41:04 +08:00
项目提供了多个示例文件,展示了不同的功能:
2025-06-15 01:41:04 +08:00
- `sample_face_detection.py`: 基础人脸检测
- `sample_face_track_from_video.py`: 视频人脸跟踪
- `sample_face_recognition.py`: 人脸识别
- `sample_face_comparison.py`: 人脸比对
- `sample_feature_hub.py`: 特征提取
- `sample_system_resource_statistics.py`: 系统资源统计
2025-06-15 01:41:04 +08:00
## 运行测试
2025-06-15 01:41:04 +08:00
项目包含单元测试,您可以通过修改 `test/test_settings.py` 中的参数来调整测试内容:
2025-06-15 01:41:04 +08:00
```bash
python -m unittest discover -s test
```
2025-06-15 01:41:04 +08:00
## 注意事项
1. 确保系统已安装 OpenCV 和其他必要依赖
2. 使用前请确保动态库已正确安装
3. 建议使用 Python 3.7 或更高版本