2025-03-25 00:51:26 +08:00
# InspireFace Python API
2025-06-16 13:19:38 +08:00
InspireFace provides an easy-to-use Python API that wraps the underlying dynamic link library through ctypes. You can install the latest release version via pip or configure it using the project's self-compiled dynamic library.
2025-03-25 00:51:26 +08:00
2025-06-16 13:19:38 +08:00
## Quick Installation
2025-03-25 00:51:26 +08:00
2025-06-16 13:19:38 +08:00
### Install via pip (Recommended)
2025-03-25 00:51:26 +08:00
```bash
pip install inspireface
```
2025-06-16 13:19:38 +08:00
### Manual Installation
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
1. First install the necessary dependencies:
2025-06-15 01:41:04 +08:00
```bash
pip install loguru tqdm opencv-python
```
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
2. Copy the compiled dynamic library to the specified directory:
2025-06-15 01:41:04 +08:00
```bash
2025-06-16 13:19:38 +08:00
# Copy the compiled dynamic library to the corresponding system architecture directory
2025-03-25 00:51:26 +08:00
cp YOUR_BUILD_DIR/libInspireFace.so inspireface/modules/core/SYSTEM/CORE_ARCH/
```
2025-06-16 13:19:38 +08:00
3. Install the Python package:
2025-06-15 01:41:04 +08:00
```bash
2025-03-25 00:51:26 +08:00
python setup.py install
2024-05-02 01:27:29 +08:00
```
2025-06-16 13:19:38 +08:00
## Quick Start
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
Here's a simple example showing how to use InspireFace for face detection and landmark drawing:
2024-05-02 01:27:29 +08:00
2025-06-15 01:41:04 +08:00
```python
2024-05-02 01:27:29 +08:00
import cv2
2025-03-25 00:51:26 +08:00
import inspireface as isf
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
# Create session with required features enabled
2025-06-15 01:41:04 +08:00
session = isf.InspireFaceSession(
2025-06-16 13:19:38 +08:00
opt=isf.HF_ENABLE_NONE, # Optional features
detect_mode=isf.HF_DETECT_MODE_ALWAYS_DETECT # Detection mode
2025-06-15 01:41:04 +08:00
)
2025-06-16 13:19:38 +08:00
# Set detection confidence threshold
2025-03-25 00:51:26 +08:00
session.set_detection_confidence_threshold(0.5)
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
# Read image
2025-06-15 01:41:04 +08:00
image = cv2.imread("path/to/your/image.jpg")
2025-06-16 13:19:38 +08:00
assert image is not None, "Please check if the image path is correct"
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
# Perform face detection
2024-05-02 01:27:29 +08:00
faces = session.face_detection(image)
2025-06-16 13:19:38 +08:00
print(f"Detected {len(faces)} faces")
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
# Draw detection results on image
2024-05-02 01:27:29 +08:00
draw = image.copy()
for idx, face in enumerate(faces):
2025-06-16 13:19:38 +08:00
# Get face bounding box coordinates
2024-05-02 01:27:29 +08:00
x1, y1, x2, y2 = face.location
2025-06-15 01:41:04 +08:00
2025-06-16 13:19:38 +08:00
# Calculate rotated box parameters
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-06-16 13:19:38 +08:00
# Draw rotated box
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
2025-06-16 13:19:38 +08:00
# Draw landmarks
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)
2024-05-02 01:27:29 +08:00
```
2025-06-16 13:19:38 +08:00
## More Examples
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
The project provides multiple example files demonstrating different features:
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
- `sample_face_detection.py` : Basic face detection
- `sample_face_track_from_video.py` : Video face tracking
- `sample_face_recognition.py` : Face recognition
- `sample_face_comparison.py` : Face comparison
- `sample_feature_hub.py` : Feature extraction
- `sample_system_resource_statistics.py` : System resource statistics
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
## Running Tests
2024-05-02 01:27:29 +08:00
2025-06-16 13:19:38 +08:00
The project includes unit tests. You can adjust test content by modifying parameters in `test/test_settings.py` :
2024-05-02 01:27:29 +08:00
2025-06-15 01:41:04 +08:00
```bash
2024-05-02 01:27:29 +08:00
python -m unittest discover -s test
```
2025-06-16 13:19:38 +08:00
## Notes
2025-06-15 01:41:04 +08:00
2025-06-16 13:19:38 +08:00
1. Ensure that OpenCV and other necessary dependencies are installed on your system
2. Make sure the dynamic library is correctly installed before use
3. Python 3.7 or higher is recommended