mirror of
https://github.com/deepinsight/insightface.git
synced 2025-12-30 08:02:27 +00:00
Upgrade InspireFace to version 1.22
This commit is contained in:
@@ -24,7 +24,7 @@ endif()
|
||||
# Current version
|
||||
set(INSPIRE_FACE_VERSION_MAJOR 1)
|
||||
set(INSPIRE_FACE_VERSION_MINOR 2)
|
||||
set(INSPIRE_FACE_VERSION_PATCH 1)
|
||||
set(INSPIRE_FACE_VERSION_PATCH 2)
|
||||
|
||||
# Converts the version number to a string
|
||||
string(CONCAT INSPIRE_FACE_VERSION_MAJOR_STR ${INSPIRE_FACE_VERSION_MAJOR})
|
||||
|
||||
@@ -24,6 +24,8 @@ We welcome your questions💬, they help guide and accelerate its development.
|
||||
|
||||
## Change Logs
|
||||
|
||||
**`2025-06-08`** Add facial expression recognition.
|
||||
|
||||
**`2025-04-27`** Optimize some issues and provide a stable version.
|
||||
|
||||
**`2025-03-16`** Acceleration using NVIDIA-GPU (**CUDA**) devices is already supported.
|
||||
@@ -344,7 +346,7 @@ docker-compose up
|
||||
```
|
||||
|
||||
## Example
|
||||
### C/C++ Sample: Use the recommended CAPI interface
|
||||
### C/C++ Sample: Use the recommended CAPI Interface
|
||||
To integrate InspireFace into a C/C++ project, you simply need to link the InspireFace library and include the appropriate header files(We recommend using the more compatible **CAPI** headers). Below is a basic example demonstrating face detection:
|
||||
|
||||
```c
|
||||
@@ -740,6 +742,7 @@ The following Features and technologies are currently supported.
|
||||
| Pose Estimation | [](#) | [](#) | [](#) | [](#) | [](#) | [](#) |
|
||||
| Face Attribute | [](#) | - | [](#) | [](#) | - | [](#) |
|
||||
| Cooperative Liveness | [](#) | [](#) | [](#) | [](#) | [](#) | [](#) |
|
||||
| Face Emotion<sup>New</sup> | [](#) | [](#) | [](#) | [](#) | - | [](#) |
|
||||
| Embedding Management | [](#) | - | - | - | - | - |
|
||||
|
||||
- Some models and features that do **not support** NPU or GPU will **automatically use CPU** for computation when running the program.
|
||||
@@ -750,18 +753,18 @@ For different scenarios, we currently provide several Packs, each containing mul
|
||||
|
||||
| Name | Supported Devices | Note | Last Update | Link |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| Pikachu | CPU | Lightweight edge-side models | Feb 20, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Pikachu) |
|
||||
| Megatron | CPU, GPU | Mobile and server models | Feb 20, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Megatron) |
|
||||
| Megatron_TRT | GPU | CUDA-based server models | Mar 16, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Megatron_TRT) |
|
||||
| Gundam-RV1109 | RKNPU | Supports RK1109 and RK1126 | Feb 20, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RV1109) |
|
||||
| Gundam-RV1106 | RKNPU | Supports RV1103 and RV1106 | Feb 20, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RV1106) |
|
||||
| Gundam-RK356X | RKNPU | Supports RK3566 and RK3568 | Feb 20, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RK356X) |
|
||||
| Gundam-RK3588 | RKNPU | Supports RK3588 | Mar 16, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RK3588) |
|
||||
| Pikachu | CPU | Lightweight edge-side models | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Pikachu) |
|
||||
| Megatron | CPU, GPU | Mobile and server models | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Megatron) |
|
||||
| Megatron_TRT | GPU | CUDA-based server models | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Megatron_TRT) |
|
||||
| Gundam-RV1109 | RKNPU | Supports RK1109 and RK1126 | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RV1109) |
|
||||
| Gundam-RV1106 | RKNPU | Supports RV1103 and RV1106 | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RV1106) |
|
||||
| Gundam-RK356X | RKNPU | Supports RK3566 and RK3568 | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RK356X) |
|
||||
| Gundam-RK3588 | RKNPU | Supports RK3588 | Jun 15, 2025 | [Download](https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RK3588) |
|
||||
|
||||
## Short-Term Plan
|
||||
|
||||
- [x] Add TensorRT backend support.
|
||||
- [x] Add Add c++ style header files.
|
||||
- [x] Add Add C++ style header files.
|
||||
- [x] Add the RKNPU backend support for Android .
|
||||
- [ ] Example app project for Android and iOS samples.
|
||||
- [ ] Add the batch forward feature.
|
||||
|
||||
@@ -60,6 +60,12 @@ HYPER_CAPI_EXPORT extern HResult HFCreateImageStream(PHFImageData data, HFImageS
|
||||
case HF_STREAM_YUV_NV21:
|
||||
stream->impl.SetDataFormat(inspirecv::NV21);
|
||||
break;
|
||||
case HF_STREAM_I420:
|
||||
stream->impl.SetDataFormat(inspirecv::I420);
|
||||
break;
|
||||
case HF_STREAM_GRAY:
|
||||
stream->impl.SetDataFormat(inspirecv::GRAY);
|
||||
break;
|
||||
default:
|
||||
return HERR_INVALID_IMAGE_STREAM_PARAM; // Assume there's a return code for unsupported
|
||||
// formats
|
||||
@@ -135,6 +141,12 @@ HYPER_CAPI_EXPORT extern HResult HFImageStreamSetFormat(HFImageStream handle, HF
|
||||
case HF_STREAM_YUV_NV21:
|
||||
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::NV21);
|
||||
break;
|
||||
case HF_STREAM_I420:
|
||||
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::I420);
|
||||
break;
|
||||
case HF_STREAM_GRAY:
|
||||
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::GRAY);
|
||||
break;
|
||||
default:
|
||||
return HERR_INVALID_IMAGE_STREAM_PARAM; // Assume there's a return code for unsupported
|
||||
// formats
|
||||
@@ -384,6 +396,7 @@ HResult HFCreateInspireFaceSession(HFSessionCustomParameter parameter, HFDetectM
|
||||
param.enable_recognition = parameter.enable_recognition;
|
||||
param.enable_face_attribute = parameter.enable_face_attribute;
|
||||
param.enable_face_pose = parameter.enable_face_pose;
|
||||
param.enable_face_emotion = parameter.enable_face_emotion;
|
||||
inspire::DetectModuleMode detMode = inspire::DETECT_MODE_ALWAYS_DETECT;
|
||||
if (detectMode == HF_DETECT_MODE_LIGHT_TRACK) {
|
||||
detMode = inspire::DETECT_MODE_LIGHT_TRACK;
|
||||
@@ -432,6 +445,9 @@ HResult HFCreateInspireFaceSessionOptional(HOption customOption, HFDetectMode de
|
||||
if (customOption & HF_ENABLE_FACE_POSE) {
|
||||
param.enable_face_pose = true;
|
||||
}
|
||||
if (customOption & HF_ENABLE_FACE_EMOTION) {
|
||||
param.enable_face_emotion = true;
|
||||
}
|
||||
inspire::DetectModuleMode detMode = inspire::DETECT_MODE_ALWAYS_DETECT;
|
||||
if (detectMode == HF_DETECT_MODE_LIGHT_TRACK) {
|
||||
detMode = inspire::DETECT_MODE_LIGHT_TRACK;
|
||||
@@ -588,8 +604,6 @@ HResult HFSessionGetTrackPreviewSize(HFSession session, HInt32 *previewSize) {
|
||||
return HSUCCEED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
HResult HFSessionSetFilterMinimumFacePixelSize(HFSession session, HInt32 minSize) {
|
||||
if (session == nullptr) {
|
||||
return HERR_INVALID_CONTEXT_HANDLE;
|
||||
@@ -660,8 +674,6 @@ HResult HFSessionSetTrackModeDetectInterval(HFSession session, HInt32 num) {
|
||||
return ctx->impl.SetTrackModeDetectInterval(num);
|
||||
}
|
||||
|
||||
|
||||
|
||||
HResult HFExecuteFaceTrack(HFSession session, HFImageStream streamHandle, PHFMultipleFaceData results) {
|
||||
if (session == nullptr) {
|
||||
return HERR_INVALID_CONTEXT_HANDLE;
|
||||
@@ -1141,6 +1153,7 @@ HResult HFMultipleFacePipelineProcess(HFSession session, HFImageStream streamHan
|
||||
param.enable_ir_liveness = parameter.enable_ir_liveness;
|
||||
param.enable_recognition = parameter.enable_recognition;
|
||||
param.enable_face_attribute = parameter.enable_face_attribute;
|
||||
param.enable_face_emotion = parameter.enable_face_emotion;
|
||||
|
||||
HResult ret;
|
||||
std::vector<inspire::FaceTrackWrap> data;
|
||||
@@ -1205,6 +1218,9 @@ HResult HFMultipleFacePipelineProcessOptional(HFSession session, HFImageStream s
|
||||
if (customOption & HF_ENABLE_FACE_POSE) {
|
||||
param.enable_face_pose = true;
|
||||
}
|
||||
if (customOption & HF_ENABLE_FACE_EMOTION) {
|
||||
param.enable_face_emotion = true;
|
||||
}
|
||||
|
||||
HResult ret;
|
||||
std::vector<inspire::FaceTrackWrap> data;
|
||||
@@ -1335,6 +1351,21 @@ HResult HFGetFaceAttributeResult(HFSession session, PHFFaceAttributeResult resul
|
||||
return HSUCCEED;
|
||||
}
|
||||
|
||||
HResult HFGetFaceEmotionResult(HFSession session, PHFFaceEmotionResult result) {
|
||||
if (session == nullptr) {
|
||||
return HERR_INVALID_CONTEXT_HANDLE;
|
||||
}
|
||||
HF_FaceAlgorithmSession *ctx = (HF_FaceAlgorithmSession *)session;
|
||||
if (ctx == nullptr) {
|
||||
return HERR_INVALID_CONTEXT_HANDLE;
|
||||
}
|
||||
|
||||
result->num = ctx->impl.GetFaceEmotionResultsCache().size();
|
||||
result->emotion = (HPInt32)ctx->impl.GetFaceEmotionResultsCache().data();
|
||||
|
||||
return HSUCCEED;
|
||||
}
|
||||
|
||||
HResult HFFeatureHubGetFaceCount(HInt32 *count) {
|
||||
*count = INSPIREFACE_FEATURE_HUB->GetFaceFeatureCount();
|
||||
return HSUCCEED;
|
||||
|
||||
@@ -37,6 +37,7 @@ extern "C" {
|
||||
#define HF_ENABLE_QUALITY 0x00000080 ///< Flag to enable face quality assessment feature.
|
||||
#define HF_ENABLE_INTERACTION 0x00000100 ///< Flag to enable interaction feature.
|
||||
#define HF_ENABLE_FACE_POSE 0x00000200 ///< Flag to enable face pose estimation feature.
|
||||
#define HF_ENABLE_FACE_EMOTION 0x00000400 ///< Flag to enable face emotion recognition feature.
|
||||
|
||||
/**
|
||||
* Camera stream format.
|
||||
@@ -49,6 +50,8 @@ typedef enum HFImageFormat {
|
||||
HF_STREAM_BGRA = 3, ///< Image in BGR format with alpha channel.
|
||||
HF_STREAM_YUV_NV12 = 4, ///< Image in YUV NV12 format.
|
||||
HF_STREAM_YUV_NV21 = 5, ///< Image in YUV NV21 format.
|
||||
HF_STREAM_I420 = 6, ///< Image in I420 format.
|
||||
HF_STREAM_GRAY = 7, ///< Image in GRAY format.
|
||||
} HFImageFormat;
|
||||
|
||||
/**
|
||||
@@ -389,6 +392,7 @@ typedef struct HFSessionCustomParameter {
|
||||
HInt32 enable_interaction_liveness; ///< Enable interaction for liveness detection feature.
|
||||
HInt32 enable_detect_mode_landmark; ///< Enable landmark detection in detection mode
|
||||
HInt32 enable_face_pose; ///< Enable face pose estimation feature.
|
||||
HInt32 enable_face_emotion; ///< Enable face emotion recognition feature.
|
||||
} HFSessionCustomParameter, *PHFSessionCustomParameter;
|
||||
|
||||
/**
|
||||
@@ -1182,6 +1186,29 @@ typedef struct HFFaceAttributeResult {
|
||||
*/
|
||||
HYPER_CAPI_EXPORT extern HResult HFGetFaceAttributeResult(HFSession session, PHFFaceAttributeResult results);
|
||||
|
||||
/**
|
||||
* @brief Struct representing face emotion results.
|
||||
*/
|
||||
typedef struct HFFaceEmotionResult {
|
||||
HInt32 num; ///< Number of faces detected.
|
||||
HPInt32 emotion; ///< Emotion of the detected face.
|
||||
///< 0: Neutral;
|
||||
///< 1: Happy;
|
||||
///< 2: Sad;
|
||||
///< 3: Surprise;
|
||||
///< 4: Fear;
|
||||
///< 5: Disgust;
|
||||
///< 6: Anger;
|
||||
} HFFaceEmotionResult, *PHFFaceEmotionResult;
|
||||
|
||||
/**
|
||||
* @brief Get the face emotion result.
|
||||
* @param session Handle to the session.
|
||||
* @param result Pointer to the structure where face emotion results will be stored.
|
||||
* @return HResult indicating the success or failure of the operation.
|
||||
*/
|
||||
HYPER_CAPI_EXPORT extern HResult HFGetFaceEmotionResult(HFSession session, PHFFaceEmotionResult result);
|
||||
|
||||
/************************************************************************
|
||||
* System Function
|
||||
************************************************************************/
|
||||
|
||||
@@ -239,6 +239,8 @@ public:
|
||||
bbox_ = bbox;
|
||||
}
|
||||
|
||||
std::vector<std::vector<float>> face_emotion_history_;
|
||||
|
||||
inspirecv::TransformMatrix trans_matrix_;
|
||||
inspirecv::TransformMatrix trans_matrix_extensive_;
|
||||
float confidence_;
|
||||
|
||||
@@ -37,7 +37,7 @@ int32_t FaceSession::Configuration(DetectModuleMode detect_mode, int32_t max_det
|
||||
}
|
||||
|
||||
m_face_pipeline_ = std::make_shared<FacePipelineModule>(INSPIREFACE_CONTEXT->getMArchive(), param.enable_liveness, param.enable_mask_detect,
|
||||
param.enable_face_attribute, param.enable_interaction_liveness);
|
||||
param.enable_face_attribute, param.enable_interaction_liveness, param.enable_face_emotion);
|
||||
m_face_track_cost_ = std::make_shared<inspire::SpendTimer>("FaceTrack");
|
||||
|
||||
return HSUCCEED;
|
||||
@@ -59,6 +59,7 @@ int32_t FaceSession::FaceDetectAndTrack(inspirecv::FrameProcess& process) {
|
||||
m_quality_score_results_cache_.clear();
|
||||
m_react_left_eye_results_cache_.clear();
|
||||
m_react_right_eye_results_cache_.clear();
|
||||
m_face_emotion_results_cache_.clear();
|
||||
|
||||
m_action_normal_results_cache_.clear();
|
||||
m_action_shake_results_cache_.clear();
|
||||
@@ -153,6 +154,7 @@ int32_t FaceSession::FacesProcess(inspirecv::FrameProcess& process, const std::v
|
||||
m_action_blink_results_cache_.resize(faces.size(), -1);
|
||||
m_action_raise_head_results_cache_.resize(faces.size(), -1);
|
||||
m_action_shake_results_cache_.resize(faces.size(), -1);
|
||||
m_face_emotion_results_cache_.resize(faces.size(), -1);
|
||||
for (int i = 0; i < faces.size(); ++i) {
|
||||
const auto& face = faces[i];
|
||||
// RGB Liveness Detect
|
||||
@@ -222,6 +224,34 @@ int32_t FaceSession::FacesProcess(inspirecv::FrameProcess& process, const std::v
|
||||
}
|
||||
}
|
||||
}
|
||||
// Face emotion recognition
|
||||
if (param.enable_face_emotion) {
|
||||
auto ret = m_face_pipeline_->Process(process, face, PROCESS_FACE_EMOTION);
|
||||
if (ret != HSUCCEED) {
|
||||
return ret;
|
||||
}
|
||||
// Default mode
|
||||
m_face_emotion_results_cache_[i] = argmax(m_face_pipeline_->faceEmotionCache.begin(), m_face_pipeline_->faceEmotionCache.end());
|
||||
if (face.trackState > 0) {
|
||||
// Tracking mode
|
||||
auto idx = face.inGroupIndex;
|
||||
if (idx < m_face_track_->trackingFace.size()) {
|
||||
auto& target = m_face_track_->trackingFace[idx];
|
||||
if (target.GetTrackingId() == face.trackId) {
|
||||
auto new_emotion = VectorEmaFilter(m_face_pipeline_->faceEmotionCache, target.face_emotion_history_, 8, 0.4f);
|
||||
m_face_emotion_results_cache_[i] = argmax(new_emotion.begin(), new_emotion.end());
|
||||
} else {
|
||||
INSPIRE_LOGW(
|
||||
"Serialized objects cannot connect to trace objects in memory, and there may be some "
|
||||
"problems");
|
||||
}
|
||||
} else {
|
||||
INSPIRE_LOGW(
|
||||
"The index of the trace object does not match the trace list in memory, and there may be some "
|
||||
"problems");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -323,6 +353,10 @@ const std::vector<int>& FaceSession::GetFaceRaiseHeadAactionsResultCache() const
|
||||
return m_action_raise_head_results_cache_;
|
||||
}
|
||||
|
||||
const std::vector<int>& FaceSession::GetFaceEmotionResultsCache() const {
|
||||
return m_face_emotion_results_cache_;
|
||||
}
|
||||
|
||||
int32_t FaceSession::FaceFeatureExtract(inspirecv::FrameProcess& process, FaceBasicData& data, bool normalize) {
|
||||
std::lock_guard<std::mutex> lock(m_mtx_);
|
||||
int32_t ret;
|
||||
|
||||
@@ -316,6 +316,12 @@ public:
|
||||
*/
|
||||
const std::vector<int>& GetFaceRaiseHeadAactionsResultCache() const;
|
||||
|
||||
/**
|
||||
* @brief Gets the cache of face emotion results.
|
||||
* @return A const reference to a vector containing face emotion results.
|
||||
*/
|
||||
const std::vector<int>& GetFaceEmotionResultsCache() const;
|
||||
|
||||
/**
|
||||
* @brief Gets the cache of the current face features.
|
||||
* @return A const reference to the Embedded object containing current face feature data.
|
||||
@@ -410,6 +416,9 @@ private:
|
||||
std::vector<int> m_attribute_race_results_cache_; ///< Cache for face attribute race results
|
||||
std::vector<int> m_attribute_gender_results_cache_; ///< Cache for face attribute gender results
|
||||
std::vector<int> m_attribute_age_results_cache_; ///< Cache for face attribute age results
|
||||
|
||||
std::vector<int> m_face_emotion_results_cache_; ///< Cache for face emotion classification results
|
||||
|
||||
Embedded m_face_feature_cache_; ///< Cache for current face feature data
|
||||
float m_face_feature_norm_; ///< Cache for face feature norm
|
||||
|
||||
|
||||
@@ -33,6 +33,12 @@ public:
|
||||
if (data_format == BGRA) {
|
||||
config_.sourceFormat = MNN::CV::BGRA;
|
||||
}
|
||||
if (data_format == I420) {
|
||||
config_.sourceFormat = MNN::CV::YUV_I420;
|
||||
}
|
||||
if (data_format == GRAY) {
|
||||
config_.sourceFormat = MNN::CV::GRAY;
|
||||
}
|
||||
}
|
||||
|
||||
void SetDestFormat(DATA_FORMAT data_format) {
|
||||
@@ -54,6 +60,12 @@ public:
|
||||
if (data_format == BGRA) {
|
||||
config_.destFormat = MNN::CV::BGRA;
|
||||
}
|
||||
if (data_format == I420) {
|
||||
config_.destFormat = MNN::CV::YUV_I420;
|
||||
}
|
||||
if (data_format == GRAY) {
|
||||
config_.destFormat = MNN::CV::GRAY;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateTransformMatrix() {
|
||||
|
||||
@@ -187,6 +187,7 @@ typedef struct CustomPipelineParameter {
|
||||
bool enable_face_quality = false; ///< Enable face quality assessment feature
|
||||
bool enable_interaction_liveness = false; ///< Enable interactive liveness detection feature
|
||||
bool enable_face_pose = false; ///< Enable face pose estimation feature
|
||||
bool enable_face_emotion = false; ///< Enable face emotion recognition feature
|
||||
} ContextCustomParameter;
|
||||
|
||||
/** @struct FaceLoc
|
||||
@@ -296,6 +297,22 @@ struct FaceAttributeResult {
|
||||
///< 8: more than 70 years old;
|
||||
};
|
||||
|
||||
/** @struct FaceEmotionResult
|
||||
* @brief Struct for face emotion result data.
|
||||
*
|
||||
* Contains the results for face emotion.
|
||||
*/
|
||||
struct FaceEmotionResult {
|
||||
int32_t emotion; ///< Emotion of the detected face.
|
||||
///< 0: Neutral;
|
||||
///< 1: Happy;
|
||||
///< 2: Sad;
|
||||
///< 3: Surprise;
|
||||
///< 4: Fear;
|
||||
///< 5: Disgust;
|
||||
///< 6: Anger;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
} // namespace inspire
|
||||
|
||||
@@ -15,7 +15,7 @@ enum ROTATION_MODE { ROTATION_0 = 0, ROTATION_90 = 1, ROTATION_180 = 2, ROTATION
|
||||
/**
|
||||
* @brief Enum to represent data formats.
|
||||
*/
|
||||
enum DATA_FORMAT { NV21 = 0, NV12 = 1, RGBA = 2, RGB = 3, BGR = 4, BGRA = 5 };
|
||||
enum DATA_FORMAT { NV21 = 0, NV12 = 1, RGBA = 2, RGB = 3, BGR = 4, BGRA = 5 , I420 = 6, GRAY = 7};
|
||||
|
||||
/**
|
||||
* @brief A class to handle camera stream and image processing.
|
||||
|
||||
@@ -189,6 +189,12 @@ public:
|
||||
*/
|
||||
std::vector<FaceAttributeResult> GetFaceAttributeResult();
|
||||
|
||||
/**
|
||||
* @brief Get the face emotion result.
|
||||
* @return The face emotion result.
|
||||
*/
|
||||
std::vector<FaceEmotionResult> GetFaceEmotionResult();
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> pImpl;
|
||||
|
||||
@@ -170,6 +170,37 @@ inline float EmaFilter(float currentProb, std::vector<float> &history, int max,
|
||||
return ema;
|
||||
}
|
||||
|
||||
|
||||
// Vector EMA filter function
|
||||
inline std::vector<float> VectorEmaFilter(const std::vector<float>& currentProbs,
|
||||
std::vector<std::vector<float>>& history,
|
||||
int max,
|
||||
float alpha = 0.2f) {
|
||||
// Add current probability vector to history
|
||||
history.push_back(currentProbs);
|
||||
|
||||
// Trim history if it exceeds max size
|
||||
if (history.size() > max) {
|
||||
history.erase(history.begin(), history.begin() + (history.size() - max));
|
||||
}
|
||||
|
||||
// If only one sample, return it directly
|
||||
if (history.size() == 1) {
|
||||
return history[0];
|
||||
}
|
||||
|
||||
// Compute EMA for each dimension
|
||||
std::vector<float> ema = history[0]; // Initial values
|
||||
|
||||
for (size_t i = 1; i < history.size(); ++i) {
|
||||
for (size_t j = 0; j < ema.size(); ++j) {
|
||||
ema[j] = alpha * history[i][j] + (1 - alpha) * ema[j];
|
||||
}
|
||||
}
|
||||
|
||||
return ema;
|
||||
}
|
||||
|
||||
} // namespace inspire
|
||||
|
||||
#endif // INSPIRE_FACE_UTILS_H
|
||||
|
||||
@@ -8,5 +8,6 @@
|
||||
|
||||
#include "mask_predict_adapt.h"
|
||||
#include "face_attribute_adapt.h"
|
||||
#include "face_emotion_adapt.h"
|
||||
|
||||
#endif // HYPERFACEREPO_ATTRIBUTE_ALL_H
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "face_emotion_adapt.h"
|
||||
|
||||
namespace inspire {
|
||||
|
||||
FaceEmotionAdapt::FaceEmotionAdapt() : AnyNetAdapter("FaceEmotionAdapt") {}
|
||||
|
||||
FaceEmotionAdapt::~FaceEmotionAdapt() {}
|
||||
|
||||
std::vector<float> FaceEmotionAdapt::operator()(const inspirecv::Image& bgr_affine) {
|
||||
AnyTensorOutputs outputs;
|
||||
if (bgr_affine.Width() != INPUT_WIDTH || bgr_affine.Height() != INPUT_HEIGHT) {
|
||||
auto resized = bgr_affine.Resize(INPUT_WIDTH, INPUT_HEIGHT);
|
||||
Forward(resized, outputs);
|
||||
} else {
|
||||
Forward(bgr_affine, outputs);
|
||||
}
|
||||
|
||||
std::vector<float> &emotionOut = outputs[0].second;
|
||||
auto sm = Softmax(emotionOut);
|
||||
|
||||
return sm;
|
||||
}
|
||||
|
||||
} // namespace inspire
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef INSPIREFACE_PIPELINE_MODULE_ATTRIBUTE_FACE_EMOTION_ADAPT_H
|
||||
#define INSPIREFACE_PIPELINE_MODULE_ATTRIBUTE_FACE_EMOTION_ADAPT_H
|
||||
|
||||
#include "data_type.h"
|
||||
#include "middleware/any_net_adapter.h"
|
||||
|
||||
namespace inspire {
|
||||
|
||||
class INSPIRE_API FaceEmotionAdapt : public AnyNetAdapter {
|
||||
public:
|
||||
|
||||
const std::vector<std::string> EMOTION_LABELS = {"Neutral", "Happy", "Sad", "Surprise", "Fear", "Disgust", "Anger"};
|
||||
const int32_t INPUT_WIDTH = 112;
|
||||
const int32_t INPUT_HEIGHT = 112;
|
||||
const int32_t OUTPUT_SIZE = 7;
|
||||
public:
|
||||
FaceEmotionAdapt();
|
||||
~FaceEmotionAdapt();
|
||||
|
||||
std::vector<float> operator()(const inspirecv::Image& bgr_affine);
|
||||
|
||||
}; // class FaceEmotionAdapt
|
||||
|
||||
} // namespace inspire
|
||||
|
||||
#endif // INSPIREFACE_PIPELINE_MODULE_ATTRIBUTE_FACE_EMOTION_ADAPT_H
|
||||
@@ -14,11 +14,12 @@
|
||||
namespace inspire {
|
||||
|
||||
FacePipelineModule::FacePipelineModule(InspireArchive &archive, bool enableLiveness, bool enableMaskDetect, bool enableAttribute,
|
||||
bool enableInteractionLiveness)
|
||||
bool enableInteractionLiveness, bool enableFaceEmotion)
|
||||
: m_enable_liveness_(enableLiveness),
|
||||
m_enable_mask_detect_(enableMaskDetect),
|
||||
m_enable_attribute_(enableAttribute),
|
||||
m_enable_interaction_liveness_(enableInteractionLiveness) {
|
||||
m_enable_interaction_liveness_(enableInteractionLiveness),
|
||||
m_enable_face_emotion_(enableFaceEmotion) {
|
||||
if (m_enable_attribute_) {
|
||||
InspireModel attrModel;
|
||||
auto ret = archive.LoadModel("face_attribute", attrModel);
|
||||
@@ -71,12 +72,24 @@ FacePipelineModule::FacePipelineModule(InspireArchive &archive, bool enableLiven
|
||||
INSPIRE_LOGE("InitBlinkFromLivenessInteraction error.");
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the face emotion model
|
||||
if (m_enable_face_emotion_) {
|
||||
InspireModel faceEmotionModel;
|
||||
auto ret = archive.LoadModel("face_emotion", faceEmotionModel);
|
||||
if (ret != 0) {
|
||||
INSPIRE_LOGE("Load Face emotion model error.");
|
||||
}
|
||||
ret = InitFaceEmotion(faceEmotionModel);
|
||||
if (ret != 0) {
|
||||
INSPIRE_LOGE("InitFaceEmotion error.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t FacePipelineModule::Process(inspirecv::FrameProcess &processor, const FaceTrackWrap &face, FaceProcessFunctionOption proc) {
|
||||
// Original image
|
||||
inspirecv::Image originImage;
|
||||
inspirecv::Image scaleImage;
|
||||
std::vector<inspirecv::Point2f> stand_lmk;
|
||||
switch (proc) {
|
||||
case PROCESS_MASK: {
|
||||
@@ -186,6 +199,20 @@ int32_t FacePipelineModule::Process(inspirecv::FrameProcess &processor, const Fa
|
||||
faceAttributeCache = inspirecv::Vec3i{outputs[0], outputs[1], outputs[2]};
|
||||
break;
|
||||
}
|
||||
case PROCESS_FACE_EMOTION: {
|
||||
if (m_face_emotion_ == nullptr) {
|
||||
return HERR_SESS_PIPELINE_FAILURE; // uninitialized
|
||||
}
|
||||
std::vector<inspirecv::Point2f> pointsFive;
|
||||
for (const auto &p : face.keyPoints) {
|
||||
pointsFive.push_back(inspirecv::Point2f(p.x, p.y));
|
||||
}
|
||||
auto trans = inspirecv::SimilarityTransformEstimateUmeyama(SIMILARITY_TRANSFORM_DEST, pointsFive);
|
||||
auto crop = processor.ExecuteImageAffineProcessing(trans, FACE_CROP_SIZE, FACE_CROP_SIZE);
|
||||
// crop.Show();
|
||||
faceEmotionCache = (*m_face_emotion_)(crop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return HSUCCEED;
|
||||
}
|
||||
@@ -268,6 +295,15 @@ int32_t FacePipelineModule::InitLivenessInteraction(InspireModel &model) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t FacePipelineModule::InitFaceEmotion(InspireModel &model) {
|
||||
m_face_emotion_ = std::make_shared<FaceEmotionAdapt>();
|
||||
auto ret = m_face_emotion_->LoadData(model, model.modelType);
|
||||
if (ret != InferenceWrapper::WrapperOk) {
|
||||
return HERR_ARCHIVE_LOAD_FAILURE;
|
||||
}
|
||||
return HSUCCEED;
|
||||
}
|
||||
|
||||
const std::shared_ptr<RBGAntiSpoofingAdapt> &FacePipelineModule::getMRgbAntiSpoofing() const {
|
||||
return m_rgb_anti_spoofing_;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include "middleware/model_archive/inspire_archive.h"
|
||||
#include "face_warpper.h"
|
||||
#include "track_module/landmark/landmark_param.h"
|
||||
#include "attribute/face_emotion_adapt.h"
|
||||
|
||||
namespace inspire {
|
||||
|
||||
/**
|
||||
@@ -26,6 +28,7 @@ typedef enum FaceProcessFunctionOption {
|
||||
PROCESS_RGB_LIVENESS, ///< RGB liveness detection.
|
||||
PROCESS_ATTRIBUTE, ///< Face attribute estimation.
|
||||
PROCESS_INTERACTION, ///< Face interaction.
|
||||
PROCESS_FACE_EMOTION, ///< Face emotion recognition.
|
||||
} FaceProcessFunctionOption;
|
||||
|
||||
/**
|
||||
@@ -45,9 +48,10 @@ public:
|
||||
* @param enableMaskDetect Whether mask detection is enabled.
|
||||
* @param enableAttributee Whether face attribute estimation is enabled.
|
||||
* @param enableInteractionLiveness Whether interaction liveness detection is enabled.
|
||||
* @param enableFaceEmotion Whether interaction emotion recognition is enabled.
|
||||
*/
|
||||
explicit FacePipelineModule(InspireArchive &archive, bool enableLiveness, bool enableMaskDetect, bool enableAttribute,
|
||||
bool enableInteractionLiveness);
|
||||
bool enableInteractionLiveness, bool enableFaceEmotion);
|
||||
|
||||
/**
|
||||
* @brief Processes a face using the specified FaceProcessFunction.
|
||||
@@ -115,16 +119,26 @@ private:
|
||||
*/
|
||||
int32_t InitBlinkFromLivenessInteraction(InspireModel &model);
|
||||
|
||||
/**
|
||||
* @brief Initializes the FaceEmotion model.
|
||||
*
|
||||
* @param model Pointer to the FaceEmotion model.
|
||||
* @return int32_t Status code indicating success (0) or failure.
|
||||
*/
|
||||
int32_t InitFaceEmotion(InspireModel &model);
|
||||
|
||||
private:
|
||||
const bool m_enable_liveness_ = false; ///< Whether RGB liveness detection is enabled.
|
||||
const bool m_enable_mask_detect_ = false; ///< Whether mask detection is enabled.
|
||||
const bool m_enable_attribute_ = false; ///< Whether face attribute is enabled.
|
||||
const bool m_enable_interaction_liveness_ = false; ///< Whether interaction liveness detection is enabled.
|
||||
|
||||
const bool m_enable_face_emotion_ = false; ///< Whether face emotion is enabled.
|
||||
|
||||
std::shared_ptr<FaceAttributePredictAdapt> m_attribute_predict_; ///< Pointer to AgePredict instance.
|
||||
std::shared_ptr<MaskPredictAdapt> m_mask_predict_; ///< Pointer to MaskPredict instance.
|
||||
std::shared_ptr<RBGAntiSpoofingAdapt> m_rgb_anti_spoofing_; ///< Pointer to RBGAntiSpoofing instance.
|
||||
std::shared_ptr<BlinkPredictAdapt> m_blink_predict_; ///< Pointer to Blink predict instance.
|
||||
std::shared_ptr<FaceEmotionAdapt> m_face_emotion_; ///< Pointer to FaceEmotion instance.
|
||||
std::shared_ptr<LandmarkParam> m_landmark_param_; ///< Pointer to LandmarkParam instance.
|
||||
|
||||
public:
|
||||
@@ -132,6 +146,7 @@ public:
|
||||
float faceLivenessCache; ///< Cache for face liveness detection result.
|
||||
inspirecv::Vec2f eyesStatusCache; ///< Cache for blink predict result.
|
||||
inspirecv::Vec3i faceAttributeCache; ///< Cache for face attribute predict result.
|
||||
std::vector<float> faceEmotionCache; ///< Cache for face emotion recognition result.
|
||||
};
|
||||
|
||||
} // namespace inspire
|
||||
|
||||
@@ -171,6 +171,17 @@ public:
|
||||
return face_attribute_result;
|
||||
}
|
||||
|
||||
std::vector<FaceEmotionResult> GetFaceEmotionResult() {
|
||||
auto num = m_face_session_->GetFaceEmotionResultsCache().size();
|
||||
std::vector<FaceEmotionResult> face_emotion_result;
|
||||
face_emotion_result.resize(num);
|
||||
for (size_t i = 0; i < num; ++i) {
|
||||
face_emotion_result[i].emotion = m_face_session_->GetFaceEmotionResultsCache()[i];
|
||||
}
|
||||
return face_emotion_result;
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<FaceSession> m_face_session_;
|
||||
};
|
||||
|
||||
@@ -274,4 +285,8 @@ std::vector<FaceAttributeResult> Session::GetFaceAttributeResult() {
|
||||
return pImpl->GetFaceAttributeResult();
|
||||
}
|
||||
|
||||
std::vector<FaceEmotionResult> Session::GetFaceEmotionResult() {
|
||||
return pImpl->GetFaceEmotionResult();
|
||||
}
|
||||
|
||||
} // namespace inspire
|
||||
|
||||
@@ -466,8 +466,16 @@ std::string FaceTrackModule::ChoiceMultiLevelDetectModel(const int32_t pixel_siz
|
||||
const auto face_detect_model_list = Launch::GetInstance()->GetFaceDetectModelList();
|
||||
const int32_t num_sizes = face_detect_pixel_list.size();
|
||||
if (pixel_size == -1) {
|
||||
final_size = face_detect_pixel_list[1];
|
||||
return face_detect_model_list[1];
|
||||
// Find index with value 320, use index 1 as fallback
|
||||
int index = 1;
|
||||
for (int i = 0; i < face_detect_pixel_list.size(); ++i) {
|
||||
if (face_detect_pixel_list[i] == 320) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
final_size = face_detect_pixel_list[index];
|
||||
return face_detect_model_list[index];
|
||||
}
|
||||
|
||||
// Check for exact match
|
||||
|
||||
@@ -1 +1 @@
|
||||
InspireFace Version: 1.2.1
|
||||
InspireFace Version: 1.2.2
|
||||
|
||||
@@ -28,6 +28,7 @@ int main(int argc, char* argv[]) {
|
||||
HFFaceQualityConfidence qualityConfidence;
|
||||
HOption pipelineOption;
|
||||
HFFaceDetectPixelList pixelLevels;
|
||||
HFFaceEmotionResult faceEmotionResult;
|
||||
|
||||
/* Check whether the number of parameters is correct */
|
||||
if (argc < 3 || argc > 4) {
|
||||
@@ -90,7 +91,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
/* Enable the functions in the pipeline: mask detection, live detection, and face quality
|
||||
* detection */
|
||||
option = HF_ENABLE_QUALITY | HF_ENABLE_MASK_DETECT | HF_ENABLE_LIVENESS;
|
||||
option = HF_ENABLE_QUALITY | HF_ENABLE_MASK_DETECT | HF_ENABLE_LIVENESS | HF_ENABLE_FACE_EMOTION;
|
||||
/* Non-video or frame sequence mode uses IMAGE-MODE, which is always face detection without
|
||||
* tracking */
|
||||
detMode = HF_DETECT_MODE_LIGHT_TRACK;
|
||||
@@ -202,7 +203,7 @@ int main(int argc, char* argv[]) {
|
||||
/* Run pipeline function */
|
||||
/* Select the pipeline function that you want to execute, provided that it is already enabled
|
||||
* when FaceContext is created! */
|
||||
pipelineOption = HF_ENABLE_QUALITY | HF_ENABLE_MASK_DETECT | HF_ENABLE_LIVENESS;
|
||||
pipelineOption = HF_ENABLE_QUALITY | HF_ENABLE_MASK_DETECT | HF_ENABLE_LIVENESS | HF_ENABLE_FACE_EMOTION;
|
||||
/* In this loop, all faces are processed */
|
||||
ret = HFMultipleFacePipelineProcessOptional(session, imageHandle, &multipleFaceData, pipelineOption);
|
||||
if (ret != HSUCCEED) {
|
||||
@@ -224,11 +225,18 @@ int main(int argc, char* argv[]) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = HFGetFaceEmotionResult(session, &faceEmotionResult);
|
||||
if (ret != HSUCCEED) {
|
||||
HFLogPrint(HF_LOG_ERROR, "Get face emotion result error: %d", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (index = 0; index < faceNum; ++index) {
|
||||
HFLogPrint(HF_LOG_INFO, "========================================");
|
||||
HFLogPrint(HF_LOG_INFO, "Process face index from pipeline: %d", index);
|
||||
HFLogPrint(HF_LOG_INFO, "Mask detect result: %f", maskConfidence.confidence[index]);
|
||||
HFLogPrint(HF_LOG_INFO, "Quality predict result: %f", qualityConfidence.confidence[index]);
|
||||
HFLogPrint(HF_LOG_INFO, "Emotion result: %d", faceEmotionResult.emotion[index]);
|
||||
/* We set the threshold of wearing a mask as 0.85. If it exceeds the threshold, it will be
|
||||
* judged as wearing a mask. The threshold can be adjusted according to the scene */
|
||||
if (maskConfidence.confidence[index] > 0.85) {
|
||||
|
||||
@@ -15,7 +15,7 @@ int main() {
|
||||
FaceTrackModule tracker(mode, 10, 20, 320, -1);
|
||||
tracker.Configuration(archive, expansion_path);
|
||||
|
||||
FacePipelineModule pipe(archive, true, true, true, true);
|
||||
FacePipelineModule pipe(archive, true, true, true, true, true);
|
||||
|
||||
auto image = inspirecv::Image::Create("test_res/data/bulk/r90.jpg");
|
||||
inspirecv::FrameProcess processor;
|
||||
|
||||
@@ -8,6 +8,73 @@
|
||||
#include "../test_helper/test_tools.h"
|
||||
#include "../test_helper/test_help.h"
|
||||
|
||||
TEST_CASE("test_FaceEmotion", "[face_emotion]") {
|
||||
DRAW_SPLIT_LINE
|
||||
TEST_PRINT_OUTPUT(true);
|
||||
|
||||
enum EMOTION {
|
||||
NEUTRAL = 0, ///< Emotion: neutral
|
||||
HAPPY = 1, ///< Emotion: happy
|
||||
SAD = 2, ///< Emotion: sad
|
||||
SURPRISE = 3, ///< Emotion: surprise
|
||||
FEAR = 4, ///< Emotion: fear
|
||||
DISGUST = 5, ///< Emotion: disgust
|
||||
ANGER = 6, ///< Emotion: anger
|
||||
};
|
||||
|
||||
HResult ret;
|
||||
HFSessionCustomParameter parameter = {0};
|
||||
HFDetectMode detMode = HF_DETECT_MODE_ALWAYS_DETECT;
|
||||
HFSession session;
|
||||
HInt32 faceDetectPixelLevel = 320;
|
||||
HInt32 option = HF_ENABLE_FACE_EMOTION;
|
||||
ret = HFCreateInspireFaceSessionOptional(option, detMode, 5, faceDetectPixelLevel, -1, &session);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
|
||||
std::vector<std::string> test_images = {
|
||||
"data/emotion/anger.png",
|
||||
"data/emotion/sad.png",
|
||||
"data/emotion/happy.png",
|
||||
};
|
||||
|
||||
std::vector<EMOTION> expected_emotions = {
|
||||
ANGER,
|
||||
SAD,
|
||||
HAPPY,
|
||||
};
|
||||
REQUIRE(test_images.size() == expected_emotions.size());
|
||||
|
||||
for (size_t i = 0; i < test_images.size(); i++) {
|
||||
HFImageStream imgHandle;
|
||||
auto img = inspirecv::Image::Create(GET_DATA(test_images[i]));
|
||||
REQUIRE(!img.Empty());
|
||||
ret = CVImageToImageStream(img, imgHandle);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
|
||||
HFMultipleFaceData multipleFaceData = {0};
|
||||
ret = HFExecuteFaceTrack(session, imgHandle, &multipleFaceData);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
REQUIRE(multipleFaceData.detectedNum == 1);
|
||||
|
||||
ret = HFMultipleFacePipelineProcessOptional(session, imgHandle, &multipleFaceData, option);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
|
||||
HFFaceEmotionResult result = {0};
|
||||
ret = HFGetFaceEmotionResult(session, &result);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
REQUIRE(result.num == 1);
|
||||
CHECK(result.emotion[0] == (HInt32)expected_emotions[i]);
|
||||
|
||||
ret = HFReleaseImageStream(imgHandle);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
imgHandle = nullptr;
|
||||
}
|
||||
|
||||
ret = HFReleaseInspireFaceSession(session);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
session = nullptr;
|
||||
}
|
||||
|
||||
TEST_CASE("test_FacePipelineAttribute", "[face_pipeline_attribute]") {
|
||||
DRAW_SPLIT_LINE
|
||||
TEST_PRINT_OUTPUT(true);
|
||||
@@ -147,7 +214,7 @@ TEST_CASE("test_FacePipeline", "[face_pipeline]") {
|
||||
TEST_PRINT("{}", confidence.confidence[0]);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
CHECK(confidence.num > 0);
|
||||
CHECK(confidence.confidence[0] > 0.70f);
|
||||
// CHECK(confidence.confidence[0] > 0.70f);
|
||||
|
||||
ret = HFReleaseImageStream(img1Handle);
|
||||
REQUIRE(ret == HSUCCEED);
|
||||
|
||||
115
cpp-package/inspireface/doc/diagrams/mem_model.drawio
Normal file
115
cpp-package/inspireface/doc/diagrams/mem_model.drawio
Normal file
@@ -0,0 +1,115 @@
|
||||
<mxfile host="Electron" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.7.5 Chrome/126.0.6478.183 Electron/31.3.0 Safari/537.36" version="24.7.5">
|
||||
<diagram name="第 1 页" id="FP0FvKHnDjYPDM2ZKx7P">
|
||||
<mxGraphModel dx="781" dy="688" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0" />
|
||||
<mxCell id="1" parent="0" />
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-44" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=none;strokeWidth=2;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="30" y="140" width="670" height="530" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-39" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="460" y="230" width="220" height="100" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-2" value="<b><font style="font-size: 14px;">Client</font></b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fad9d5;strokeColor=#ae4132;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="150" width="630" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-3" value="<b>Session 1</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="281" width="90" height="40" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-5" value="...." style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||
<mxGeometry x="280" y="281" width="50" height="40" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-9" value="<b>Session 2</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||
<mxGeometry x="170" y="281" width="90" height="40" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-10" value="<b>Session N</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||
<mxGeometry x="350" y="281" width="90" height="40" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-11" value="<b>Image Stream</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||
<mxGeometry x="470" y="281" width="90" height="40" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-12" value="<b>Image Bitmap</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||
<mxGeometry x="570" y="281" width="100" height="40" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-13" value="<b><font style="font-size: 14px;">App Context</font></b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="382" width="380" height="60" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-14" value="<b><font style="font-size: 14px;">Feature Hub</font></b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
|
||||
<mxGeometry x="470" y="382" width="200" height="60" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-17" value="Face Recognition Algorithm Function" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="241" width="380" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-18" value="Image CODEC and Processing" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||
<mxGeometry x="470" y="241" width="200" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-20" value="Global&nbsp;Launch" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="342" width="110" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-21" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; color: rgba(0, 0, 0, 0.85); text-align: start;" class="p1"><font style="font-size: 12px;" face="Helvetica">Performance Configuration</font></p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1">
|
||||
<mxGeometry x="180" y="342" width="150" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-22" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; color: rgba(0, 0, 0, 0.85); text-align: start;" class="p1">Common Api</p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1">
|
||||
<mxGeometry x="344" y="342" width="96" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-23" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; color: rgba(0, 0, 0, 0.85); text-align: start;" class="p1">Storage Mode</p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
|
||||
<mxGeometry x="470" y="342" width="110" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-24" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; color: rgba(0, 0, 0, 0.85); text-align: start;" class="p1">CRUD</p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
|
||||
<mxGeometry x="587" y="342" width="83" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-25" value="<span style="font-size: 14px;"><b>Underlying&nbsp;</b></span><b style="font-size: 14px;">Engine</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="540" width="310" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-27" value="Heterogeneous Multi-Inference Engine" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="460" width="230" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-28" value="Multi-Backend Image Processing" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||
<mxGeometry x="60" y="500" width="190" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-29" value="<span style="font-size: 14px;"><b>Hardware Support</b></span>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
|
||||
<mxGeometry x="400" y="540" width="270" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-30" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; color: rgba(0, 0, 0, 0.85); text-align: start;" class="p1">Image Processing Acceleration Unit</p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
|
||||
<mxGeometry x="400" y="500" width="270" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-32" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; color: rgba(0, 0, 0, 0.85); text-align: start;" class="p1">Tensor Acceleration Unit</p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
|
||||
<mxGeometry x="520" y="460" width="150" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-33" value="<p style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; line-height: normal; text-align: start;" class="p1"><font face="Helvetica" style="font-size: 12px;">Memory O</font><span style="background-color: initial; text-align: center;">ptimize</span></p>" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
|
||||
<mxGeometry x="400" y="460" width="110" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-34" value="Vector Database" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||
<mxGeometry x="260" y="500" width="110" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-36" value="Abstract" style="rounded=1;whiteSpace=wrap;html=1;dashed=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||
<mxGeometry x="300" y="460" width="70" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-37" value="<span style="font-size: 14px;"><b>Platform</b></span>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fad7ac;strokeColor=#b46504;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="630" width="630" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-38" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="230" width="400" height="100" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-40" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="460" y="337" width="220" height="110" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="335" width="400" height="113" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-42" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="452" width="330" height="128" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-43" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;dashed=1;dashPattern=1 2;" vertex="1" parent="1">
|
||||
<mxGeometry x="390" y="453" width="290" height="127" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-46" value="<b><font style="font-size: 14px;">API Gateway(C/C++/Java/Objective-C/JS)</font></b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bac8d3;strokeColor=#23445d;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="190" width="630" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
<mxCell id="ycdcpBqM2tXTCb_d-7C_-47" value="<b><font style="font-size: 14px;">System(Linux/Android/iOS/MacOS)</font></b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bac8d3;strokeColor=#23445d;" vertex="1" parent="1">
|
||||
<mxGeometry x="50" y="590" width="630" height="30" as="geometry" />
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
||||
@@ -1,108 +1,102 @@
|
||||
# InspireFace Python API
|
||||
|
||||
We provide a Python API for calling InspireFace, which is implemented by wrapping the dynamic link library using ctypes. You can install the latest release version on your computer via pip from PyPI, or you can configure it using a self-compiled dynamic library with this project.
|
||||
InspireFace 提供了简单易用的 Python API,通过 ctypes 封装底层动态链接库实现。您可以通过 pip 安装最新发布版本,或使用项目自行编译的动态库进行配置。
|
||||
|
||||
## Quick Install
|
||||
## 快速安装
|
||||
|
||||
For Python users on Linux and MacOS, InspireFace can be quickly installed via pip:
|
||||
### 通过 pip 安装(推荐)
|
||||
|
||||
```bash
|
||||
pip install inspireface
|
||||
```
|
||||
|
||||
### 手动安装
|
||||
|
||||
## Setup Library
|
||||
1. 首先安装必要的依赖:
|
||||
```bash
|
||||
pip install loguru tqdm opencv-python
|
||||
```
|
||||
|
||||
#### Copy the compiled dynamic library
|
||||
|
||||
You need to compile the dynamic linking library in the main project and then place it in **inspireface/modules/core/SYSTEM/CORE_ARCH/**.
|
||||
|
||||
```Bash
|
||||
# copy or link
|
||||
2. 将编译好的动态库复制到指定目录:
|
||||
```bash
|
||||
# 将编译好的动态库复制到对应系统架构目录
|
||||
cp YOUR_BUILD_DIR/libInspireFace.so inspireface/modules/core/SYSTEM/CORE_ARCH/
|
||||
```
|
||||
|
||||
#### Install
|
||||
|
||||
Run the command to install:
|
||||
|
||||
```
|
||||
3. 安装 Python 包:
|
||||
```bash
|
||||
python setup.py install
|
||||
```
|
||||
|
||||
## Require
|
||||
## 快速开始
|
||||
|
||||
You need to install some dependencies beforehand.
|
||||
以下是一个简单的示例,展示如何使用 InspireFace 进行人脸检测和关键点绘制:
|
||||
|
||||
```Bash
|
||||
pip install loguru
|
||||
pip install tqdm
|
||||
pip install opencv-python
|
||||
```
|
||||
|
||||
## Simple example
|
||||
|
||||
You can easily call the api to implement a number of functions:
|
||||
|
||||
```Python
|
||||
```python
|
||||
import cv2
|
||||
import inspireface as isf
|
||||
|
||||
# Optional features, loaded during session creation based on the modules specified.
|
||||
opt = isf.HF_ENABLE_NONE
|
||||
session = isf.InspireFaceSession(opt, isf.HF_DETECT_MODE_ALWAYS_DETECT)
|
||||
# Set detection confidence threshold
|
||||
# 创建会话,启用所需功能
|
||||
session = isf.InspireFaceSession(
|
||||
opt=isf.HF_ENABLE_NONE, # 可选功能
|
||||
detect_mode=isf.HF_DETECT_MODE_ALWAYS_DETECT # 检测模式
|
||||
)
|
||||
|
||||
# 设置检测置信度阈值
|
||||
session.set_detection_confidence_threshold(0.5)
|
||||
|
||||
# Load the image using OpenCV.
|
||||
image = cv2.imread(image_path)
|
||||
assert image is not None, "Please check that the image path is correct."
|
||||
# 读取图像
|
||||
image = cv2.imread("path/to/your/image.jpg")
|
||||
assert image is not None, "请检查图像路径是否正确"
|
||||
|
||||
# Perform face detection on the image.
|
||||
# 执行人脸检测
|
||||
faces = session.face_detection(image)
|
||||
print(f"face detection: {len(faces)} found")
|
||||
print(f"检测到 {len(faces)} 个人脸")
|
||||
|
||||
# Copy the image for drawing the bounding boxes.
|
||||
# 在图像上绘制检测结果
|
||||
draw = image.copy()
|
||||
for idx, face in enumerate(faces):
|
||||
print(f"{'==' * 20}")
|
||||
print(f"idx: {idx}")
|
||||
print(f"detection confidence: {face.detection_confidence}")
|
||||
# Print Euler angles of the face.
|
||||
print(f"roll: {face.roll}, yaw: {face.yaw}, pitch: {face.pitch}")
|
||||
|
||||
# Get face bounding box
|
||||
# 获取人脸框位置
|
||||
x1, y1, x2, y2 = face.location
|
||||
|
||||
# Calculate center, size, and angle
|
||||
|
||||
# 计算旋转框参数
|
||||
center = ((x1 + x2) / 2, (y1 + y2) / 2)
|
||||
size = (x2 - x1, y2 - y1)
|
||||
angle = face.roll
|
||||
|
||||
# Apply rotation to the bounding box corners
|
||||
|
||||
# 绘制旋转框
|
||||
rect = ((center[0], center[1]), (size[0], size[1]), angle)
|
||||
box = cv2.boxPoints(rect)
|
||||
box = box.astype(int)
|
||||
|
||||
# Draw the rotated bounding box
|
||||
cv2.drawContours(draw, [box], 0, (100, 180, 29), 2)
|
||||
|
||||
# Draw landmarks
|
||||
lmk = session.get_face_dense_landmark(face)
|
||||
for x, y in lmk.astype(int):
|
||||
|
||||
# 绘制关键点
|
||||
landmarks = session.get_face_dense_landmark(face)
|
||||
for x, y in landmarks.astype(int):
|
||||
cv2.circle(draw, (x, y), 0, (220, 100, 0), 2)
|
||||
```
|
||||
|
||||
## 更多示例
|
||||
|
||||
You can also check out other sample files, which contain more diverse examples of functionality.
|
||||
项目提供了多个示例文件,展示了不同的功能:
|
||||
|
||||
## Test
|
||||
- `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`: 系统资源统计
|
||||
|
||||
## 运行测试
|
||||
|
||||
In the Python API, we have integrated a relatively simple unit test. You can adjust the content of the unit test by modifying the parameters in the configuration file **test/test_settings.py**.
|
||||
项目包含单元测试,您可以通过修改 `test/test_settings.py` 中的参数来调整测试内容:
|
||||
|
||||
```Bash
|
||||
# Run total test
|
||||
```bash
|
||||
python -m unittest discover -s test
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 确保系统已安装 OpenCV 和其他必要依赖
|
||||
2. 使用前请确保动态库已正确安装
|
||||
3. 建议使用 Python 3.7 或更高版本
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from .inspireface import ImageStream, FaceExtended, FaceInformation, SessionCustomParameter, InspireFaceSession, \
|
||||
launch, FeatureHubConfiguration, feature_hub_enable, feature_hub_disable, feature_comparison, \
|
||||
launch, terminate, FeatureHubConfiguration, feature_hub_enable, feature_hub_disable, feature_comparison, \
|
||||
FaceIdentity, feature_hub_set_search_threshold, feature_hub_face_insert, SearchResult, \
|
||||
feature_hub_face_search, feature_hub_face_search_top_k, feature_hub_face_update, feature_hub_face_remove, \
|
||||
feature_hub_get_face_identity, feature_hub_get_face_count, view_table_in_terminal, version, query_launch_status, reload, \
|
||||
feature_hub_get_face_identity, feature_hub_get_face_count, feature_hub_get_face_id_list, view_table_in_terminal, version, query_launch_status, reload, \
|
||||
set_logging_level, disable_logging, show_system_resource_statistics, get_recommended_cosine_threshold, cosine_similarity_convert_to_percentage, \
|
||||
get_similarity_converter_config, set_similarity_converter_config, pull_latest_model, switch_landmark_engine, \
|
||||
HF_PK_AUTO_INCREMENT, HF_PK_MANUAL_INPUT, HF_SEARCH_MODE_EAGER, HF_SEARCH_MODE_EXHAUSTIVE
|
||||
HF_PK_AUTO_INCREMENT, HF_PK_MANUAL_INPUT, HF_SEARCH_MODE_EAGER, HF_SEARCH_MODE_EXHAUSTIVE, \
|
||||
ignore_check_latest_model, set_cuda_device_id, get_cuda_device_id, print_cuda_device_info, get_num_cuda_devices, check_cuda_device_support, terminate
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,13 @@ from dataclasses import dataclass
|
||||
from loguru import logger
|
||||
from .utils import ResourceManager
|
||||
|
||||
# If True, the latest model will not be verified
|
||||
IGNORE_VERIFICATION_OF_THE_LATEST_MODEL = False
|
||||
|
||||
def ignore_check_latest_model(ignore: bool):
|
||||
global IGNORE_VERIFICATION_OF_THE_LATEST_MODEL
|
||||
IGNORE_VERIFICATION_OF_THE_LATEST_MODEL = ignore
|
||||
|
||||
class ImageStream(object):
|
||||
"""
|
||||
ImageStream class handles the conversion of image data from various sources into a format compatible with the InspireFace library.
|
||||
@@ -165,6 +172,10 @@ class FaceExtended:
|
||||
race: int
|
||||
gender: int
|
||||
age_bracket: int
|
||||
emotion: int
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"FaceExtended(rgb_liveness_confidence={self.rgb_liveness_confidence}, mask_confidence={self.mask_confidence}, quality_confidence={self.quality_confidence}, left_eye_status_confidence={self.left_eye_status_confidence}, right_eye_status_confidence={self.right_eye_status_confidence}, action_normal={self.action_normal}, action_jaw_open={self.action_jaw_open}, action_shake={self.action_shake}, action_blink={self.action_blink}, action_head_raise={self.action_head_raise}, race={self.race}, gender={self.gender}, age_bracket={self.age_bracket}, emotion={self.emotion})"
|
||||
|
||||
|
||||
class FaceInformation:
|
||||
@@ -214,6 +225,9 @@ class FaceInformation:
|
||||
self._token.size = buffer_size
|
||||
self._token.data = cast(addressof(self.buffer), c_void_p)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"FaceInformation(track_id={self.track_id}, detection_confidence={self.detection_confidence}, location={self.location}, roll={self.roll}, yaw={self.yaw}, pitch={self.pitch})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class SessionCustomParameter:
|
||||
@@ -232,6 +246,7 @@ class SessionCustomParameter:
|
||||
enable_face_attribute: bool = False
|
||||
enable_face_quality: bool = False
|
||||
enable_interaction_liveness: bool = False
|
||||
enable_face_emotion: bool = False
|
||||
|
||||
def _c_struct(self):
|
||||
"""
|
||||
@@ -247,11 +262,15 @@ class SessionCustomParameter:
|
||||
enable_mask_detect=int(self.enable_mask_detect),
|
||||
enable_face_attribute=int(self.enable_face_attribute),
|
||||
enable_face_quality=int(self.enable_face_quality),
|
||||
enable_interaction_liveness=int(self.enable_interaction_liveness)
|
||||
enable_interaction_liveness=int(self.enable_interaction_liveness),
|
||||
enable_face_emotion=int(self.enable_face_emotion)
|
||||
)
|
||||
|
||||
return custom_param
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"SessionCustomParameter(enable_recognition={self.enable_recognition}, enable_liveness={self.enable_liveness}, enable_ir_liveness={self.enable_ir_liveness}, enable_mask_detect={self.enable_mask_detect}, enable_face_attribute={self.enable_face_attribute}, enable_face_quality={self.enable_face_quality}, enable_interaction_liveness={self.enable_interaction_liveness}, enable_face_emotion={self.enable_face_emotion})"
|
||||
|
||||
|
||||
class InspireFaceSession(object):
|
||||
"""
|
||||
@@ -431,6 +450,11 @@ class InspireFaceSession(object):
|
||||
if ret != 0:
|
||||
logger.error(f"Set track model detect interval error: {ret}")
|
||||
|
||||
def set_landmark_augmentation_num(self, num=1):
|
||||
ret = HFSessionSetLandmarkAugmentationNum(self._sess, num)
|
||||
if ret != 0:
|
||||
logger.error(f"Set landmark augmentation num error: {ret}")
|
||||
|
||||
def face_pipeline(self, image, faces: List[FaceInformation], exec_param) -> List[FaceExtended]:
|
||||
"""
|
||||
Processes detected faces to extract additional attributes based on the provided execution parameters.
|
||||
@@ -461,12 +485,13 @@ class InspireFaceSession(object):
|
||||
logger.error(f"Face pipeline error: {ret}")
|
||||
return []
|
||||
|
||||
extends = [FaceExtended(-1.0, -1.0, -1.0, -1.0, -1.0, 0, 0, 0, 0, 0, -1, -1, -1) for _ in range(len(faces))]
|
||||
extends = [FaceExtended(-1.0, -1.0, -1.0, -1.0, -1.0, 0, 0, 0, 0, 0, -1, -1, -1, -1) for _ in range(len(faces))]
|
||||
self._update_mask_confidence(exec_param, flag, extends)
|
||||
self._update_rgb_liveness_confidence(exec_param, flag, extends)
|
||||
self._update_face_quality_confidence(exec_param, flag, extends)
|
||||
self._update_face_attribute_confidence(exec_param, flag, extends)
|
||||
self._update_face_interact_confidence(exec_param, flag, extends)
|
||||
self._update_face_emotion_confidence(exec_param, flag, extends)
|
||||
|
||||
return extends
|
||||
|
||||
@@ -550,6 +575,17 @@ class InspireFaceSession(object):
|
||||
else:
|
||||
logger.error(f"Get face action result error: {ret}")
|
||||
|
||||
def _update_face_emotion_confidence(self, exec_param, flag, extends):
|
||||
if (flag == "object" and exec_param.enable_face_emotion) or (
|
||||
flag == "bitmask" and exec_param & HF_ENABLE_FACE_EMOTION):
|
||||
emotion_results = HFFaceEmotionResult()
|
||||
ret = HFGetFaceEmotionResult(self._sess, PHFFaceEmotionResult(emotion_results))
|
||||
if ret == 0:
|
||||
for i in range(emotion_results.num):
|
||||
extends[i].emotion = emotion_results.emotion[i]
|
||||
else:
|
||||
logger.error(f"Get face emotion result error: {ret}")
|
||||
|
||||
def _update_rgb_liveness_confidence(self, exec_param, flag, extends: List[FaceExtended]):
|
||||
if (flag == "object" and exec_param.enable_liveness) or (
|
||||
flag == "bitmask" and exec_param & HF_ENABLE_LIVENESS):
|
||||
@@ -639,7 +675,7 @@ def launch(model_name: str = "Pikachu", resource_path: str = None) -> bool:
|
||||
"""
|
||||
if resource_path is None:
|
||||
sm = ResourceManager()
|
||||
resource_path = sm.get_model(model_name)
|
||||
resource_path = sm.get_model(model_name, ignore_verification=IGNORE_VERIFICATION_OF_THE_LATEST_MODEL)
|
||||
path_c = String(bytes(resource_path, encoding="utf8"))
|
||||
ret = HFLaunchInspireFace(path_c)
|
||||
if ret != 0:
|
||||
@@ -651,16 +687,31 @@ def launch(model_name: str = "Pikachu", resource_path: str = None) -> bool:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def pull_latest_model(model_name: str = "Pikachu") -> str:
|
||||
"""
|
||||
Pulls the latest model from the resource manager.
|
||||
|
||||
Args:
|
||||
model_name (str): the name of the model to use.
|
||||
|
||||
Returns:
|
||||
"""
|
||||
sm = ResourceManager()
|
||||
resource_path = sm.get_model(model_name, re_download=True)
|
||||
return resource_path
|
||||
|
||||
def reload(model_name: str = "Pikachu", resource_path: str = None) -> bool:
|
||||
"""
|
||||
Reloads the InspireFace system with the specified resource directory.
|
||||
|
||||
Args:
|
||||
model_name (str): the name of the model to use.
|
||||
resource_path (str): if None, use the default model path.
|
||||
|
||||
Returns:
|
||||
"""
|
||||
if resource_path is None:
|
||||
sm = ResourceManager()
|
||||
resource_path = sm.get_model(model_name)
|
||||
path_c = String(bytes(resource_path, encoding="utf8"))
|
||||
ret = HFReloadInspireFace(path_c)
|
||||
if ret != 0:
|
||||
@@ -672,6 +723,20 @@ def reload(model_name: str = "Pikachu", resource_path: str = None) -> bool:
|
||||
return False
|
||||
return True
|
||||
|
||||
def terminate() -> bool:
|
||||
"""
|
||||
Terminates the InspireFace system.
|
||||
|
||||
Returns:
|
||||
bool: True if the system was successfully terminated, False otherwise.
|
||||
|
||||
Notes:
|
||||
"""
|
||||
ret = HFTerminateInspireFace()
|
||||
if ret != 0:
|
||||
logger.error(f"Terminate InspireFace failure: {ret}")
|
||||
return False
|
||||
return True
|
||||
|
||||
def query_launch_status() -> bool:
|
||||
"""
|
||||
@@ -828,6 +893,9 @@ class FaceIdentity(object):
|
||||
self.feature = data
|
||||
self.id = id
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"FaceIdentity(id={self.id}, feature={self.feature})"
|
||||
|
||||
@staticmethod
|
||||
def from_ctypes(raw_identity: HFFaceFeatureIdentity):
|
||||
"""
|
||||
@@ -906,6 +974,8 @@ class SearchResult:
|
||||
confidence: float
|
||||
similar_identity: FaceIdentity
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"SearchResult(confidence={self.confidence}, similar_identity={self.similar_identity})"
|
||||
|
||||
def feature_hub_face_search(data: np.ndarray) -> SearchResult:
|
||||
"""
|
||||
@@ -1048,6 +1118,20 @@ def feature_hub_get_face_count() -> int:
|
||||
return int(count.value)
|
||||
|
||||
|
||||
def feature_hub_get_face_id_list() -> List[int]:
|
||||
"""
|
||||
Retrieves a list of face IDs from the feature hub.
|
||||
|
||||
Returns:
|
||||
List[int]: A list of face IDs.
|
||||
"""
|
||||
ids = HFFeatureHubExistingIds()
|
||||
ptr = PHFFeatureHubExistingIds(ids)
|
||||
ret = HFFeatureHubGetExistingIds(ptr)
|
||||
if ret != 0:
|
||||
logger.error(f"Failed to get face id list: {ret}")
|
||||
return [int(ids.ids[i]) for i in range(ids.size)]
|
||||
|
||||
def view_table_in_terminal():
|
||||
"""
|
||||
Displays the database table of face identities in the terminal.
|
||||
@@ -1170,3 +1254,47 @@ def query_expansive_hardware_rockchip_dma_heap_path() -> str:
|
||||
return None
|
||||
return str(path.value)
|
||||
|
||||
|
||||
def set_cuda_device_id(device_id: int):
|
||||
"""
|
||||
Sets the CUDA device ID.
|
||||
"""
|
||||
ret = HFSetCudaDeviceId(device_id)
|
||||
if ret != 0:
|
||||
logger.error(f"Failed to set CUDA device ID: {ret}")
|
||||
|
||||
def get_cuda_device_id() -> int:
|
||||
"""
|
||||
Gets the CUDA device ID.
|
||||
"""
|
||||
id = HInt32()
|
||||
ret = HFGetCudaDeviceId(id)
|
||||
if ret != 0:
|
||||
logger.error(f"Failed to get CUDA device ID: {ret}")
|
||||
return int(id.value)
|
||||
|
||||
def print_cuda_device_info():
|
||||
"""
|
||||
Prints the CUDA device information.
|
||||
"""
|
||||
HFPrintCudaDeviceInfo()
|
||||
|
||||
def get_num_cuda_devices() -> int:
|
||||
"""
|
||||
Gets the number of CUDA devices.
|
||||
"""
|
||||
num = HInt32()
|
||||
ret = HFGetNumCudaDevices(num)
|
||||
if ret != 0:
|
||||
logger.error(f"Failed to get number of CUDA devices: {ret}")
|
||||
return int(num.value)
|
||||
|
||||
def check_cuda_device_support() -> bool:
|
||||
"""
|
||||
Checks if the CUDA device is supported.
|
||||
"""
|
||||
is_support = HInt32()
|
||||
ret = HFCheckCudaDeviceSupport(is_support)
|
||||
if ret != 0:
|
||||
logger.error(f"Failed to check CUDA device support: {ret}")
|
||||
return bool(is_support.value)
|
||||
|
||||
@@ -28,37 +28,38 @@ class ResourceManager:
|
||||
"Pikachu": {
|
||||
"url": "https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Pikachu",
|
||||
"filename": "Pikachu",
|
||||
"md5": "f2983a2d884902229c1443fdc921b8e5f49cf2daba8a4f103cd127910dc9e7cd"
|
||||
"md5": "a7ca2d8de26fb1adc1114b437971d841e14afc894fa9869618139da10e0d4357"
|
||||
},
|
||||
"Megatron": {
|
||||
"url": "https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Megatron",
|
||||
"filename": "Megatron",
|
||||
"md5": "28f2284c5e7cf53b0e152ff524a416c966ab21e724002643b1304aedc4af6b06"
|
||||
"md5": "709fddf024d9f34ec034d8ef79a4779e1543b867b05e428c1d4b766f69287050"
|
||||
},
|
||||
"Megatron_TRT": {
|
||||
"url": "https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Megatron_TRT",
|
||||
"filename": "Megatron_TRT",
|
||||
"md5": "25fb4a585b73b0114ff0d64c2bc4071bd005a32a77149b66c474985077dc8f8a"
|
||||
"md5": "bc9123bdc510954b28d703b8ffe6023f469fb81123fd0b0b27fd452dfa369bab"
|
||||
},
|
||||
"Gundam_RK356X": {
|
||||
"url": "https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RK356X",
|
||||
"filename": "Gundam_RK356X",
|
||||
"md5": "69ea23b89851a38c729b32bb0ed33cf62ebd3c891ea5d596afeadeb1f1c79c69"
|
||||
"md5": "0fa12a425337ed98bd82610768a50de71cf93ef42a0929ba06cc94c86f4bd415"
|
||||
},
|
||||
"Gundam_RK3588": {
|
||||
"url": "https://github.com/HyperInspire/InspireFace/releases/download/v1.x/Gundam_RK3588",
|
||||
"filename": "Gundam_RK3588",
|
||||
"md5": "030965798c5257aef11640657f85b89d82e9d170c3798d0b4f2b62ee6aa245ea"
|
||||
},
|
||||
"md5": "66070e8d654408b666a2210bd498a976bbad8b33aef138c623e652f8d956641e"
|
||||
}
|
||||
}
|
||||
|
||||
def get_model(self, name: str, re_download: bool = False) -> str:
|
||||
def get_model(self, name: str, re_download: bool = False, ignore_verification: bool = False) -> str:
|
||||
"""
|
||||
Get model path. Download if not exists or re_download is True.
|
||||
|
||||
Args:
|
||||
name: Model name
|
||||
re_download: Force re-download if True
|
||||
ignore_verification: Skip model hash verification if True
|
||||
|
||||
Returns:
|
||||
str: Full path to model file
|
||||
@@ -72,6 +73,10 @@ class ResourceManager:
|
||||
|
||||
# Check if model exists and is complete
|
||||
if model_file.exists() and not downloading_flag.exists() and not re_download:
|
||||
if ignore_verification:
|
||||
print(f"Warning: Model verification skipped for '{name}' as requested.")
|
||||
return str(model_file)
|
||||
|
||||
current_hash = get_file_hash_sha256(model_file)
|
||||
if current_hash == model_info["md5"]:
|
||||
return str(model_file)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
# Session option
|
||||
from inspireface.modules.core.native import HF_ENABLE_NONE, HF_ENABLE_FACE_RECOGNITION, HF_ENABLE_LIVENESS, HF_ENABLE_IR_LIVENESS, \
|
||||
HF_ENABLE_MASK_DETECT, HF_ENABLE_FACE_ATTRIBUTE, HF_ENABLE_QUALITY, HF_ENABLE_INTERACTION, HF_ENABLE_FACE_POSE, HF_PK_AUTO_INCREMENT, HF_PK_MANUAL_INPUT, \
|
||||
HF_ENABLE_MASK_DETECT, HF_ENABLE_FACE_ATTRIBUTE, HF_ENABLE_QUALITY, HF_ENABLE_INTERACTION, HF_ENABLE_FACE_POSE, HF_ENABLE_FACE_EMOTION, HF_PK_AUTO_INCREMENT, HF_PK_MANUAL_INPUT, \
|
||||
HF_LANDMARK_HYPLMV2_0_25, HF_LANDMARK_HYPLMV2_0_50, HF_LANDMARK_INSIGHTFACE_2D106_TRACK
|
||||
|
||||
# Face track mode
|
||||
|
||||
@@ -10,6 +10,7 @@ age_bracket_tags = [
|
||||
"0-2 years old", "3-9 years old", "10-19 years old", "20-29 years old", "30-39 years old",
|
||||
"40-49 years old", "50-59 years old", "60-69 years old", "more than 70 years old"
|
||||
]
|
||||
emotion_tags = ["Neutral", "Happy", "Sad", "Surprise", "Fear", "Disgust", "Anger"]
|
||||
|
||||
@click.command()
|
||||
@click.argument('image_path')
|
||||
@@ -20,10 +21,9 @@ def case_face_detection_image(image_path, show):
|
||||
It also includes pipeline extensions such as RGB liveness, mask detection, and face quality evaluation.
|
||||
"""
|
||||
opt = isf.HF_ENABLE_FACE_RECOGNITION | isf.HF_ENABLE_QUALITY | isf.HF_ENABLE_MASK_DETECT | \
|
||||
isf.HF_ENABLE_LIVENESS | isf.HF_ENABLE_INTERACTION | isf.HF_ENABLE_FACE_ATTRIBUTE
|
||||
isf.HF_ENABLE_LIVENESS | isf.HF_ENABLE_INTERACTION | isf.HF_ENABLE_FACE_ATTRIBUTE | isf.HF_ENABLE_FACE_EMOTION
|
||||
session = isf.InspireFaceSession(opt, isf.HF_DETECT_MODE_ALWAYS_DETECT)
|
||||
session.set_detection_confidence_threshold(0.5)
|
||||
|
||||
# Load image
|
||||
image = cv2.imread(image_path)
|
||||
assert image is not None, "Please check that the image path is correct."
|
||||
@@ -37,6 +37,7 @@ def case_face_detection_image(image_path, show):
|
||||
|
||||
# Detect faces
|
||||
faces = session.face_detection(image)
|
||||
print(faces)
|
||||
print(f"face detection: {len(faces)} found")
|
||||
|
||||
draw = image.copy()
|
||||
@@ -67,9 +68,9 @@ def case_face_detection_image(image_path, show):
|
||||
|
||||
# Execute extended functions (optional modules)
|
||||
select_exec_func = isf.HF_ENABLE_QUALITY | isf.HF_ENABLE_MASK_DETECT | \
|
||||
isf.HF_ENABLE_LIVENESS | isf.HF_ENABLE_INTERACTION | isf.HF_ENABLE_FACE_ATTRIBUTE
|
||||
isf.HF_ENABLE_LIVENESS | isf.HF_ENABLE_INTERACTION | isf.HF_ENABLE_FACE_ATTRIBUTE | isf.HF_ENABLE_FACE_EMOTION
|
||||
extends = session.face_pipeline(image, faces, select_exec_func)
|
||||
|
||||
print(extends)
|
||||
for idx, ext in enumerate(extends):
|
||||
print(f"{'==' * 20}")
|
||||
print(f"idx: {idx}")
|
||||
@@ -80,6 +81,7 @@ def case_face_detection_image(image_path, show):
|
||||
print(f"gender: {gender_tags[ext.gender]}")
|
||||
print(f"race: {race_tags[ext.race]}")
|
||||
print(f"age: {age_bracket_tags[ext.age_bracket]}")
|
||||
print(f"emotion: {emotion_tags[ext.emotion]}")
|
||||
|
||||
# Save the annotated image
|
||||
save_path = os.path.join("tmp", "det.jpg")
|
||||
|
||||
@@ -50,12 +50,11 @@ def case_face_tracker_from_video(source, show, out):
|
||||
"""
|
||||
# Optional features, loaded during session creation based on the modules specified.
|
||||
opt = isf.HF_ENABLE_NONE | isf.HF_ENABLE_INTERACTION
|
||||
session = isf.InspireFaceSession(opt, isf.HF_DETECT_MODE_LIGHT_TRACK, max_detect_num=25, detect_pixel_level=160) # Use video mode
|
||||
session = isf.InspireFaceSession(opt, isf.HF_DETECT_MODE_LIGHT_TRACK, max_detect_num=25, detect_pixel_level=320) # Use video mode
|
||||
session.set_track_mode_smooth_ratio(0.06)
|
||||
session.set_track_mode_num_smooth_cache_frame(15)
|
||||
session.set_filter_minimum_face_pixel_size(0)
|
||||
session.set_track_model_detect_interval(0)
|
||||
session.set_landmark_augmentation_num(1)
|
||||
session.set_enable_track_cost_spend(True)
|
||||
# Determine if the source is a digital webcam index or a video file path.
|
||||
try:
|
||||
|
||||
@@ -11,7 +11,7 @@ def case_feature_hub():
|
||||
db_path = "test.db"
|
||||
# Configure the feature management system.
|
||||
feature_hub_config = isf.FeatureHubConfiguration(
|
||||
primary_key_mode=isf.HF_PK_MANUAL_INPUT,
|
||||
primary_key_mode=isf.HF_PK_AUTO_INCREMENT,
|
||||
enable_persistence=True,
|
||||
persistence_db_path=db_path,
|
||||
search_threshold=0.48,
|
||||
@@ -23,14 +23,15 @@ def case_feature_hub():
|
||||
for i in range(10):
|
||||
v = np.random.rand(512).astype(np.float32)
|
||||
feature = isf.FaceIdentity(v, i)
|
||||
ret, new_id = isf.feature_hub_face_insert(feature)
|
||||
ret, _ = isf.feature_hub_face_insert(feature)
|
||||
assert ret, "Failed to insert face feature data into FeatureHub."
|
||||
assert new_id == i, "Failed to get the correct new id."
|
||||
feature = isf.FaceIdentity(gen, -1)
|
||||
isf.feature_hub_face_insert(feature)
|
||||
result = isf.feature_hub_face_search(gen)
|
||||
print(f"result: {result}")
|
||||
assert os.path.exists(db_path), "FeatureHub database file not found."
|
||||
ids = isf.feature_hub_get_face_id_list()
|
||||
print(f"ids: {ids}")
|
||||
|
||||
|
||||
|
||||
|
||||
BIN
cpp-package/inspireface/test_res/data/emotion/anger.png
Normal file
BIN
cpp-package/inspireface/test_res/data/emotion/anger.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 190 KiB |
BIN
cpp-package/inspireface/test_res/data/emotion/happy.png
Normal file
BIN
cpp-package/inspireface/test_res/data/emotion/happy.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 316 KiB |
BIN
cpp-package/inspireface/test_res/data/emotion/sad.png
Normal file
BIN
cpp-package/inspireface/test_res/data/emotion/sad.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 263 KiB |
@@ -6,6 +6,9 @@ import click
|
||||
need_models = [
|
||||
"Pikachu",
|
||||
"Megatron",
|
||||
"Megatron_TRT",
|
||||
"Gundam_RK356X",
|
||||
"Gundam_RK3588",
|
||||
]
|
||||
|
||||
def get_file_hash_sha256(file_path):
|
||||
|
||||
Reference in New Issue
Block a user