Files
insightface/cpp-package/inspireface/cpp/sample/api/sample_face_track_benchmark.c

146 lines
4.8 KiB
C
Raw Normal View History

2025-05-22 16:07:26 +08:00
/*
2025-03-25 00:51:26 +08:00
* Created by Jingyu Yan
* @date 2024-10-01
*/
2025-05-22 16:07:26 +08:00
#include <stdio.h>
#include <stdlib.h>
2025-03-25 00:51:26 +08:00
#include <inspireface.h>
int main(int argc, char* argv[]) {
2025-05-22 16:07:26 +08:00
/* Check whether the number of parameters is correct */
2025-03-25 00:51:26 +08:00
if (argc < 3 || argc > 4) {
HFLogPrint(HF_LOG_ERROR, "Usage: %s <pack_path> <source_path> [rotation]", argv[0]);
return 1;
}
2025-05-22 16:07:26 +08:00
const char* packPath = argv[1];
const char* sourcePath = argv[2];
2025-03-25 00:51:26 +08:00
int rotation = 0;
2025-05-22 16:07:26 +08:00
/* If rotation is provided, check and set the value */
2025-03-25 00:51:26 +08:00
if (argc == 4) {
2025-05-22 16:07:26 +08:00
rotation = atoi(argv[3]);
2025-03-25 00:51:26 +08:00
if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270) {
HFLogPrint(HF_LOG_ERROR, "Invalid rotation value. Allowed values are 0, 90, 180, 270.");
return 1;
}
}
2025-05-22 16:07:26 +08:00
2025-03-25 00:51:26 +08:00
HFRotation rotation_enum;
2025-05-22 16:07:26 +08:00
/* Set rotation based on input parameter */
2025-03-25 00:51:26 +08:00
switch (rotation) {
case 90:
rotation_enum = HF_CAMERA_ROTATION_90;
break;
case 180:
rotation_enum = HF_CAMERA_ROTATION_180;
break;
case 270:
rotation_enum = HF_CAMERA_ROTATION_270;
break;
case 0:
default:
rotation_enum = HF_CAMERA_ROTATION_0;
break;
}
HFLogPrint(HF_LOG_INFO, "Pack file Path: %s", packPath);
HFLogPrint(HF_LOG_INFO, "Source file Path: %s", sourcePath);
HFLogPrint(HF_LOG_INFO, "Rotation: %d", rotation);
HFSetLogLevel(HF_LOG_INFO);
HResult ret;
2025-05-22 16:07:26 +08:00
/* The resource file must be loaded before it can be used */
2025-03-25 00:51:26 +08:00
ret = HFLaunchInspireFace(packPath);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Load Resource error: %d", ret);
return ret;
}
2025-05-22 16:07:26 +08:00
/* Enable the functions in the pipeline: mask detection, live detection, and face quality
* detection */
HOption option = HF_ENABLE_QUALITY | HF_ENABLE_MASK_DETECT | HF_ENABLE_LIVENESS;
/* Non-video or frame sequence mode uses IMAGE-MODE, which is always face detection without
* tracking */
2025-06-16 13:19:38 +08:00
HFDetectMode detMode = HF_DETECT_MODE_LIGHT_TRACK;
2025-05-22 16:07:26 +08:00
/* Maximum number of faces detected */
2025-03-25 00:51:26 +08:00
HInt32 maxDetectNum = 20;
2025-05-22 16:07:26 +08:00
/* Face detection image input level */
2025-03-25 00:51:26 +08:00
HInt32 detectPixelLevel = 160;
2025-05-22 16:07:26 +08:00
/* Handle of the current face SDK algorithm context */
2025-03-25 00:51:26 +08:00
HFSession session = {0};
ret = HFCreateInspireFaceSessionOptional(option, detMode, maxDetectNum, detectPixelLevel, -1, &session);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Create FaceContext error: %d", ret);
return ret;
}
HFSessionSetTrackPreviewSize(session, detectPixelLevel);
HFSessionSetFilterMinimumFacePixelSize(session, 4);
2025-05-22 16:07:26 +08:00
/* Load a image */
2025-03-25 00:51:26 +08:00
HFImageBitmap image;
ret = HFCreateImageBitmapFromFilePath(sourcePath, 3, &image);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "The source entered is not a picture or read error.");
return ret;
}
2025-05-22 16:07:26 +08:00
/* Prepare an image parameter structure for configuration */
2025-03-25 00:51:26 +08:00
HFImageStream imageHandle = {0};
ret = HFCreateImageStreamFromImageBitmap(image, rotation_enum, &imageHandle);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Create ImageStream error: %d", ret);
return ret;
}
int loop = 100;
2025-05-22 16:07:26 +08:00
/* Enable the cost spend */
2025-03-25 00:51:26 +08:00
HFSessionSetEnableTrackCostSpend(session, 1);
2025-05-22 16:07:26 +08:00
int i;
/* Execute HF_FaceContextRunFaceTrack captures face information in an image */
2025-03-25 00:51:26 +08:00
HFMultipleFaceData multipleFaceData = {0};
2025-05-22 16:07:26 +08:00
for (i = 0; i < loop; i++) {
2025-03-25 00:51:26 +08:00
ret = HFExecuteFaceTrack(session, imageHandle, &multipleFaceData);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Execute HFExecuteFaceTrack error: %d", ret);
return ret;
}
}
HFLogPrint(HF_LOG_INFO, "Number of Detection: %d", multipleFaceData.detectedNum);
HFSessionPrintTrackCostSpend(session);
2025-06-16 13:19:38 +08:00
if (multipleFaceData.detectedNum > 0) {
HFLogPrint(HF_LOG_INFO, "========================================");
for (i = 0; i < multipleFaceData.detectedNum; i++) {
HFLogPrint(HF_LOG_INFO, "TrackId: %d", multipleFaceData.trackIds[i]);
HFLogPrint(HF_LOG_INFO, "TrackCount: %d", multipleFaceData.trackCounts[i]);
}
} else {
HFLogPrint(HF_LOG_WARN, "The face cannot be detected, and the tracking test results may be invalid!");
}
2025-03-25 00:51:26 +08:00
ret = HFReleaseImageStream(imageHandle);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Release image stream error: %d", ret);
}
2025-06-16 13:19:38 +08:00
2025-03-25 00:51:26 +08:00
ret = HFReleaseImageBitmap(image);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Release image bitmap error: %d", ret);
return ret;
2025-06-16 13:19:38 +08:00
}
/* The memory must be freed at the end of the program */
ret = HFReleaseInspireFaceSession(session);
if (ret != HSUCCEED) {
HFLogPrint(HF_LOG_ERROR, "Release session error: %d", ret);
return ret;
2025-03-25 00:51:26 +08:00
}
return 0;
}