Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update track mode #149

Merged
merged 12 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
# Current version
set(INSPIRE_FACE_VERSION_MAJOR 1)
set(INSPIRE_FACE_VERSION_MINOR 1)
set(INSPIRE_FACE_VERSION_PATCH 11)
set(INSPIRE_FACE_VERSION_PATCH 12)

# Converts the version number to a string
string(CONCAT INSPIRE_FACE_VERSION_MAJOR_STR ${INSPIRE_FACE_VERSION_MAJOR})
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Please contact [[email protected]](mailto:[email protected]?subject=In

## Change Logs

**`2025-01-21`** Update all models to t3 and add tool to convert cosine similarity to percentage.

**`2025-01-08`** Support inference on Rockchip devices **RK3566/RK3568** NPU.

**`2024-12-25`** Add support for optional **RKRGA** image acceleration processing on Rockchip devices.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.insightface.sdk.inspireface.base.RGBLivenessConfidence;
import com.insightface.sdk.inspireface.base.SearchTopKResults;
import com.insightface.sdk.inspireface.base.Session;
import com.insightface.sdk.inspireface.base.SimilarityConverterConfig;
import com.insightface.sdk.inspireface.base.TypeDefine;
import com.insightface.sdk.inspireface.utils.SDKUtils;

Expand Down Expand Up @@ -214,6 +215,27 @@ public static CustomParameter CreateCustomParameter(boolean enableRecognition, b
*/
public static native void SetFaceDetectThreshold(Session session, float threshold);

/**
* Set track mode smooth ratio, default value is 0.025
* @param session Session object
* @param ratio Smooth ratio
*/
public static native void SetTrackModeSmoothRatio(Session session, float ratio);

/**
* Set track mode num smooth cache frame, default value is 15
* @param session Session object
* @param num Num smooth cache frame
*/
public static native void SetTrackModeNumSmoothCacheFrame(Session session, int num);

/**
* Set track mode detect interval, default value is 20
* @param session Session object
* @param interval Detect interval
*/
public static native void SetTrackModeDetectInterval(Session session, int interval);

/**
* Enable feature hub data
* @param configuration FeatureHubConfiguration object
Expand Down Expand Up @@ -288,6 +310,26 @@ public static CustomParameter CreateCustomParameter(boolean enableRecognition, b
*/
public static native int GetFeatureLength();


/**
* Get cosine similarity converter
* @return SimilarityConverterConfig object
*/
public static native SimilarityConverterConfig GetCosineSimilarityConverter();

/**
* Update cosine similarity converter
* @param config SimilarityConverterConfig object
*/
public static native void UpdateCosineSimilarityConverter(SimilarityConverterConfig config);

/**
* Convert cosine similarity to percentage
* @param similarity Cosine similarity
* @return Percentage similarity
*/
public static native float CosineSimilarityConvertToPercentage(float similarity);

/**
* Face comparison
* @param feature1 FaceFeature object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.insightface.sdk.inspireface.base;

public class SimilarityConverterConfig {
public float threshold;
public float middleScore;
public float steepness;
public float outputMin;
public float outputMax;
}
15 changes: 15 additions & 0 deletions cpp/inspireface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if (ISF_ENABLE_RKNN)
# For rknpu2 with armv7, we recommend linking static libraries by default
set(ISF_RKNN_API_LIB ${ISF_THIRD_PARTY_DIR}/inspireface-precompile-lite/rknn/${ISF_RKNPU_MAJOR}/runtime/${RK_PLATFORM}/librknn_api/${ISF_RK_COMPILER_TYPE}/librknnmrt.a)
set(RKNN_LINKED ${ISF_RKNN_API_LIB})
set(RKNN_USE_STATIC_LIBS TRUE)
endif()
endif()

Expand Down Expand Up @@ -206,3 +207,17 @@ endif ()
if (ISF_RK_COMPILER_TYPE STREQUAL "aarch64")
install(FILES ${ISF_RKNN_API_LIB}/librknnrt.so DESTINATION ${CMAKE_INSTALL_PREFIX}/InspireFace/lib)
endif()

if (NOT ISF_BUILD_SHARED_LIBS)
if(MNN_BUILD_SHARED_LIBS)
install(FILES ${CMAKE_BINARY_DIR}/3rdparty/MNN/libMNN.so DESTINATION ${CMAKE_INSTALL_PREFIX}/InspireFace/lib)
else()
install(FILES ${CMAKE_BINARY_DIR}/3rdparty/MNN/libMNN.a DESTINATION ${CMAKE_INSTALL_PREFIX}/InspireFace/lib)
endif()
if(RKNN_USE_STATIC_LIBS)
# To be added: The compilation of the RK series needs to be added
install(FILES ${ISF_RKNN_API_LIB} DESTINATION ${CMAKE_INSTALL_PREFIX}/InspireFace/lib)
endif()

endif()

155 changes: 155 additions & 0 deletions cpp/inspireface/c_api/inspireface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "feature_hub/feature_hub_db.h"
#include "initialization_module/launch.h"
#include "initialization_module/resource_manage.h"
#include "recognition_module/similarity_converter.h"

using namespace inspire;

Expand Down Expand Up @@ -66,6 +67,74 @@ HYPER_CAPI_EXPORT extern HResult HFCreateImageStream(PHFImageData data, HFImageS
return HSUCCEED;
}

HYPER_CAPI_EXPORT extern HResult HFCreateImageStreamEmpty(HFImageStream *handle) {
if (handle == nullptr) {
return HERR_INVALID_IMAGE_STREAM_HANDLE;
}
auto stream = new HF_CameraStream();
*handle = (HFImageStream)stream;
return HSUCCEED;
}

HYPER_CAPI_EXPORT extern HResult HFImageStreamSetBuffer(HFImageStream handle, HPUInt8 buffer, HInt32 width, HInt32 height) {
if (handle == nullptr) {
return HERR_INVALID_IMAGE_STREAM_HANDLE;
}
((HF_CameraStream *)handle)->impl.SetDataBuffer(buffer, width, height);
return HSUCCEED;
}

HYPER_CAPI_EXPORT extern HResult HFImageStreamSetRotation(HFImageStream handle, HFRotation rotation) {
if (handle == nullptr) {
return HERR_INVALID_IMAGE_STREAM_HANDLE;
}
switch (rotation) {
case HF_CAMERA_ROTATION_90:
((HF_CameraStream *)handle)->impl.SetRotationMode(inspirecv::ROTATION_90);
break;
case HF_CAMERA_ROTATION_180:
((HF_CameraStream *)handle)->impl.SetRotationMode(inspirecv::ROTATION_180);
break;
case HF_CAMERA_ROTATION_270:
((HF_CameraStream *)handle)->impl.SetRotationMode(inspirecv::ROTATION_270);
break;
default:
((HF_CameraStream *)handle)->impl.SetRotationMode(inspirecv::ROTATION_0);
break;
}
return HSUCCEED;
}

HYPER_CAPI_EXPORT extern HResult HFImageStreamSetFormat(HFImageStream handle, HFImageFormat format) {
if (handle == nullptr) {
return HERR_INVALID_IMAGE_STREAM_HANDLE;
}
switch (format) {
case HF_STREAM_RGB:
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::RGB);
break;
case HF_STREAM_BGR:
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::BGR);
break;
case HF_STREAM_RGBA:
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::RGBA);
break;
case HF_STREAM_BGRA:
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::BGRA);
break;
case HF_STREAM_YUV_NV12:
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::NV12);
break;
case HF_STREAM_YUV_NV21:
((HF_CameraStream *)handle)->impl.SetDataFormat(inspirecv::NV21);
break;
default:
return HERR_INVALID_IMAGE_STREAM_PARAM; // Assume there's a return code for unsupported
// formats
}
return HSUCCEED;
}

HYPER_CAPI_EXPORT extern HResult HFReleaseImageStream(HFImageStream streamHandle) {
if (streamHandle == nullptr) {
return HERR_INVALID_IMAGE_STREAM_HANDLE;
Expand Down Expand Up @@ -372,6 +441,16 @@ HResult HFQueryExpansiveHardwareRockchipDmaHeapPath(HString path) {
return HSUCCEED;
}

HResult HFSetExpansiveHardwareAppleCoreMLModelPath(HString path) {
// TODO: Implement this function
return HSUCCEED;
}

HResult HFQueryExpansiveHardwareAppleCoreMLModelPath(HString path) {
// TODO: Implement this function
return HSUCCEED;
}

HResult HFFeatureHubDataEnable(HFFeatureHubConfiguration configuration) {
inspire::DatabaseConfiguration param;
if (configuration.primaryKeyMode != HF_PK_AUTO_INCREMENT && configuration.primaryKeyMode != HF_PK_MANUAL_INPUT) {
Expand Down Expand Up @@ -447,6 +526,39 @@ HResult HFSessionSetFaceDetectThreshold(HFSession session, HFloat threshold) {
return ctx->impl.SetFaceDetectThreshold(threshold);
}

HResult HFSessionSetTrackModeSmoothRatio(HFSession session, HFloat ratio) {
if (session == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
}
HF_FaceAlgorithmSession *ctx = (HF_FaceAlgorithmSession *)session;
if (ctx == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
}
return ctx->impl.SetTrackModeSmoothRatio(ratio);
}

HResult HFSessionSetTrackModeNumSmoothCacheFrame(HFSession session, HInt32 num) {
if (session == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
}
HF_FaceAlgorithmSession *ctx = (HF_FaceAlgorithmSession *)session;
if (ctx == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
}
return ctx->impl.SetTrackModeNumSmoothCacheFrame(num);
}

HResult HFSessionSetTrackModeDetectInterval(HFSession session, HInt32 num) {
if (session == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
}
HF_FaceAlgorithmSession *ctx = (HF_FaceAlgorithmSession *)session;
if (ctx == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
}
return ctx->impl.SetTrackModeDetectInterval(num);
}

HResult HFExecuteFaceTrack(HFSession session, HFImageStream streamHandle, PHFMultipleFaceData results) {
if (session == nullptr) {
return HERR_INVALID_CONTEXT_HANDLE;
Expand Down Expand Up @@ -648,6 +760,49 @@ HResult HFFaceComparison(HFFaceFeature feature1, HFFaceFeature feature2, HPFloat
return ret;
}

HResult HFGetRecommendedCosineThreshold(HPFloat threshold) {
if (!INSPIRE_LAUNCH->isMLoad()) {
INSPIRE_LOGW("Inspireface is not launched, using default threshold 0.48");
}
*threshold = SIMILARITY_CONVERTER_GET_RECOMMENDED_COSINE_THRESHOLD();
return HSUCCEED;
}

HResult HFCosineSimilarityConvertToPercentage(HFloat similarity, HPFloat result) {
if (!INSPIRE_LAUNCH->isMLoad()) {
INSPIRE_LOGW("Inspireface is not launched.");
}
*result = SIMILARITY_CONVERTER_RUN(similarity);
return HSUCCEED;
}

HResult HFUpdateCosineSimilarityConverter(HFSimilarityConverterConfig config) {
if (!INSPIRE_LAUNCH->isMLoad()) {
INSPIRE_LOGW("Inspireface is not launched.");
}
inspire::SimilarityConverterConfig cfg;
cfg.threshold = config.threshold;
cfg.middleScore = config.middleScore;
cfg.steepness = config.steepness;
cfg.outputMin = config.outputMin;
cfg.outputMax = config.outputMax;
SIMILARITY_CONVERTER_UPDATE_CONFIG(cfg);
return HSUCCEED;
}

HResult HFGetCosineSimilarityConverter(PHFSimilarityConverterConfig config) {
if (!INSPIRE_LAUNCH->isMLoad()) {
INSPIRE_LOGW("Inspireface is not launched.");
}
inspire::SimilarityConverterConfig cfg = SIMILARITY_CONVERTER_GET_CONFIG();
config->threshold = cfg.threshold;
config->middleScore = cfg.middleScore;
config->steepness = cfg.steepness;
config->outputMin = cfg.outputMin;
config->outputMax = cfg.outputMax;
return HSUCCEED;
}

HResult HFGetFeatureLength(HPInt32 num) {
*num = 512;

Expand Down
Loading
Loading