All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.facelib.mtfsdk.V2AndroidBridge Maven / Gradle / Ivy

package net.facelib.mtfsdk;

import net.facelib.jni.BaseJniBridge;
import net.facelib.jni.FacelibRuntimeParam;
import net.facelib.jni.SdkInitException;
import net.facelib.jni.SdkRuntimeException;
import net.gdface.image.MatType;
import net.gdface.utils.SimpleLog;

import static net.facelib.mtfsdk.MtfsdkConstant.*;

import java.util.Arrays;

import static net.facelib.jni.BridgeLog.BRIDEG_LOGGER;

import net.facelib.akcore.LicenseManager;
import net.facelib.jni.SdkStatus;
import net.facelib.sdk.ImageSupport;

/**
 * MTFSDK for android/arm JNI 接口
* 此类为非线程安全的,不可多线程共享调用,需要在外部进一步封装实现线程安全 * * @author guyadong * */ public class V2AndroidBridge extends BaseJniBridge { public static final String SDK_VERSION = "MTFSDK512V2"; static final String V2_PRODUCT_ID = "MTFSDK512V2_android"; private final long[] featureHandle = new long[]{-1}; /** * 初始化状态 */ private volatile SdkStatus status = SdkStatus.SDK_UNINITIALIZED; public static V2LicenseManager V2_LICENSE_MANAGER = new V2LicenseManager(); static { try { BRIDEG_LOGGER.log("loadLibrary FS_AndroidFeaV2SDK"); // 加载算法动态库 System.loadLibrary("FS_AndroidFeaV2SDK"); } catch (Exception e) { BRIDEG_LOGGER.log(e.getMessage()); throw new ExceptionInInitializerError(e); } } public V2AndroidBridge() { } @Override public SdkStatus getStatus() { return status; } @Override protected void sdkInit(String licenseFile, String licenseKey, String licenseCode) throws SdkInitException { if (SdkStatus.SDK_INIT_OK == status) { return; } try { int ffFlag = FFInitV2( licenseCode.getBytes(), licenseKey.getBytes(), licenseFile.getBytes(), "\0", featureHandle); BRIDEG_LOGGER.log("FFInitV2 featureHandle: [{}] ffFlag={}",featureHandle[0],ffFlag); status = SdkStatus.jniCode(ffFlag); BRIDEG_LOGGER.log("feature module: {}", this); if (status != SdkStatus.SDK_INIT_OK) { throw new SdkInitException(status); } } finally { // 如果没有初始化成功,则destroy已经初始化模块 if (status != SdkStatus.SDK_INIT_OK) { BRIDEG_LOGGER.log("FFDestroyV2 caused by init fail {}",this); FFDestroyV2(featureHandle[0]); } } } @Override protected void sdkUninit() { if (featureHandle[0] >= 0) { BRIDEG_LOGGER.log("FFDestroyV2 {}",this); status = SdkStatus.SDK_UNINITIALIZED; FFDestroyV2(featureHandle[0]); featureHandle[0] = -1; } } @Override protected LicenseManager licenseManager() { return BaseJniBridge.getLicenseManager(V2_PRODUCT_ID, V2AndroidBridge.V2_LICENSE_MANAGER); } public byte[] feaExtractByte(MatType matType, byte[] matData, int width, int height, double[] rect) { byte[] buffer = new byte[FEATURE_BYTES]; //BRIDEG_LOGGER.log("before FFFeaExtractByteV2 {} width={},height={}",this,width, height); byte[] bgr = ImageSupport.toBGR(matType, matData, width, height); int ret = FFFeaExtractByteV2(featureHandle[0], bgr, width, height, buffer, rect); if (ret < 0) { SdkStatus code = SdkStatus.jniCode(ret); BRIDEG_LOGGER.log("FFFeaExtractByteV2 code={} {}",code,this); throw new SdkRuntimeException(code); } return buffer; } @Override public V2AndroidBridge setRuntimeParam(String name, Object value) throws SdkRuntimeException { FacelibRuntimeParam r = FacelibRuntimeParam.valueOf(name); switch (r) { case featureThreadNumber: int featureThreadNumber = value != null ? (Integer) value : MtfAndroidConfigProvider.DEFAULT_FEATURE_THREAD_NUMBER; if (featureThreadNumber > 0) { int ret = FFSetThreadsNumberV2(featureHandle[0], featureThreadNumber); if (ret != 0) { SdkStatus code = SdkStatus.jniCode(ret); BRIDEG_LOGGER.log("FFSetThreadsNumberV2 code={}",code,this); throw new SdkRuntimeException(code); } } break; default: throw new IllegalArgumentException(SimpleLog.logString("INVALID parameter name: {}", name)); } return this; } //////////////////////////////////////// 特征提取系列/////////////////////////////////////////// /** * 人脸识别初始化函数 * * @param licenseCode * 授权码 * @param licenseKey * 授权关键字 * @param path * 授权码文件本地路径 * @param faceDetectionModelPath * 模型路径 * @param handle * [out]句柄 * @return 成功(0)或错误代码(< 0),see also {@link SdkStatus} */ private static native int FFInitV2(byte[] licenseCode, byte[] licenseKey, byte[] path, String faceDetectionModelPath, long[] handle); /** * 人脸识别模块资源释放函数 * * @param handle * 句柄 */ static native void FFDestroyV2(long handle); /** * 对输入图像中检测到的人脸提取特征 * * @param handle * 操作句柄 * @param BGR * 待检测图像BGR格式 * @param width * 图像宽度 * @param height * 图像高度 * @param fea * [out] 人脸特征 空间在外部申请,长度4096 * @param rect * 人脸位置及关键点信息 由人脸检测SDK 产生,参见 * {@link #FDDetect(long, byte[], int, int, int, double[])} * @return 成功(0)或错误代码(< 0),see also {@link SdkStatus} */ private static native int FFFeaExtractByteV2(long handle, byte[] BGR, int width, int height, byte[] fea, double[] rect); /** * 人脸相似度比对函数
* 对人脸特征feaA和feaB进行相似度比对,返回相似度结果 * * @param feaA * 1024 bytes * @param feaB * 1024 bytes * @return 相似度(0.0~1.0) */ public static native double FFSimilarityByteV2(byte[] feaA, byte[] feaB); /** * @return 人脸识别模块版本 */ public static native String FFgetVersionV2(); private static native int FFSetThreadsNumberV2(long handle, int threadsNumbers); @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("V2AndroidBridge [featureHandle="); builder.append(Arrays.toString(featureHandle)); builder.append(", status="); builder.append(status); builder.append("]"); return builder.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy