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

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

The newest version!
package net.facelib.mtfsdk;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Arrays;

import com.gitee.l0km.com4j.base.BinaryUtils;
import com.gitee.l0km.com4j.base.Judge;

import net.gdface.sdk.fse.BaseFeatureSearchEngine;
import net.gdface.sdk.fse.FeatureSe;

/**
 * 基于MTFSDK Arm android算法的特征码内存搜索引擎(FSE,feature search engine)java接口类(JNI)
 * @author guyadong
 *
 */
public class FseMtfAndroidBridge extends BaseFeatureSearchEngine implements MtfsdkConstant{
	private static final String DEFAULT_LIB_NAME = "FS_FaceFeatureCompare";

	private static final FeatureSe FSE_INSTANCE = new MtfFeatureSeImpl(FseMtfAndroidBridge.class.getName());
	static {
		try{
			System.loadLibrary(DEFAULT_LIB_NAME);
			init(DEFAULT_MODE,256,DEFAULT_LOAD_FACTOR,DEFAULT_OVERBLOCK_CAPACITY);

			libraryLoaded = true;
		}catch(Exception e){
			throw new ExceptionInInitializerError(e);
		}
	}
	protected FseMtfAndroidBridge() {
	}
	
	/**
	 * 返回FSE搜索引擎的单实例
	 * @return FSE搜索引擎的单实例
	 */
	public static FeatureSe getFeatureSe(){
		return isLibraryLoaded() ? FSE_INSTANCE : null;
	}
	static class MtfFeatureSeImpl extends FeatureSeImpl{

		MtfFeatureSeImpl(String name) {
			super(name);
		}
		@Override
		public boolean addFeature(byte[] featureId, byte[] feature, String imgMD5, int group) {
			if(Judge.isEmpty(feature)){
				return false;
			}
			Float dec = null;
			if(feature.length == FEATURE_BYTES){
				ByteBuffer byteBuffer = ByteBuffer.wrap(feature).order(ByteOrder.LITTLE_ENDIAN);
				FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
				float last = floatBuffer.get(FEATURE_LEN - 1);
				// 对最后一个float的值根据值的范围进行恢复
				if (last > 5.0f && last < 11.0f) {/*身份证照片*/
					dec = last - 10.f;
				} else if (last > 15.0f && last < 21.0f) {/*黑暗模式*/
					dec = last - 20.f;
				}
			}
			if(null != dec)	{
				if(null == featureId){
					// 使用原始输入的特征计算MD5,确保MD5与外部保持一致
					featureId = BinaryUtils.getMD5(feature);
				}
				// 为了不修改输入数据,重新复制一份来修改
				byte[] copy = Arrays.copyOf(feature,feature.length);
				ByteBuffer byteBuffer = ByteBuffer.wrap(copy).order(ByteOrder.LITTLE_ENDIAN);
				FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
				floatBuffer.put(FEATURE_LEN - 1,  dec);
				// 使用修改后的特征数据加入FSE
				return super.addFeature(featureId, copy, imgMD5, group);
			}else	{
				return super.addFeature(featureId, feature, imgMD5, group);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy