|  | @@ -0,0 +1,205 @@
 | 
	
		
			
				|  |  | +package com.face.monitor;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.face.monitor.model.FaceModel;
 | 
	
		
			
				|  |  | +import com.face.monitor.model.FaceRecogRetrieveResult;
 | 
	
		
			
				|  |  | +import com.face.monitor.model.Image;
 | 
	
		
			
				|  |  | +import com.face.monitor.model.RecogResult;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.*;
 | 
	
		
			
				|  |  | +import java.util.ArrayList;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class FaceIdenTool {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 初始化引擎,加载图像人脸特征库
 | 
	
		
			
				|  |  | +     * @param idenRoot
 | 
	
		
			
				|  |  | +     * @param imgFiles
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static FaceMonitor initFaceMonitor(String idenRoot, File[] imgFiles) {
 | 
	
		
			
				|  |  | +        //初始化引擎
 | 
	
		
			
				|  |  | +        FaceMonitor faceMonitor = new FaceMonitor();
 | 
	
		
			
				|  |  | +        faceMonitor.init(idenRoot + "face/model", 0);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<byte[]> faceTestImageList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        if(imgFiles != null && imgFiles.length > 0){
 | 
	
		
			
				|  |  | +            for(File imgFile : imgFiles) {
 | 
	
		
			
				|  |  | +                if(imgFile!= null && imgFile.isFile())
 | 
	
		
			
				|  |  | +                faceTestImageList.add(readFileBytes(imgFile.getAbsolutePath()));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        FaceModel[] faceModels = faceMonitor.extractFeature(faceTestImageList);
 | 
	
		
			
				|  |  | +        //加载人脸特征库
 | 
	
		
			
				|  |  | +        if (faceModels != null && faceModels.length > 0) {
 | 
	
		
			
				|  |  | +            faceMonitor.loadFaceDataset(faceModels);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return faceMonitor;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 批量获取人脸特征码结构体
 | 
	
		
			
				|  |  | +     * @param idenRoot
 | 
	
		
			
				|  |  | +     * @param imgFiles
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static FaceModel[] extractFeature(String idenRoot, File[] imgFiles){
 | 
	
		
			
				|  |  | +        //初始化引擎
 | 
	
		
			
				|  |  | +        FaceMonitor faceMonitor = new FaceMonitor();
 | 
	
		
			
				|  |  | +        faceMonitor.init(idenRoot + "face/model", 0);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<byte[]> faceTestImageList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        if(imgFiles != null && imgFiles.length > 0){
 | 
	
		
			
				|  |  | +            for(File imgFile : imgFiles) {
 | 
	
		
			
				|  |  | +                faceTestImageList.add(readFileBytes(imgFile.getAbsolutePath()));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        FaceModel[] faceModels = faceMonitor.extractFeature(faceTestImageList);
 | 
	
		
			
				|  |  | +        //释放人脸引擎
 | 
	
		
			
				|  |  | +        faceMonitor.releaseEngine();
 | 
	
		
			
				|  |  | +        return faceModels;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 得到单个特征码
 | 
	
		
			
				|  |  | +     * @param idenRoot
 | 
	
		
			
				|  |  | +     * @param imgFile
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String getFeatPtr(String idenRoot, File imgFile) {
 | 
	
		
			
				|  |  | +        if(imgFile == null || !imgFile.isFile()){
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        File[] imgFiles = new File[]{imgFile};
 | 
	
		
			
				|  |  | +        FaceModel[] faceModels = extractFeature(idenRoot,imgFiles);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if(faceModels != null && faceModels.length > 0) {
 | 
	
		
			
				|  |  | +            FaceModel faceModel = faceModels[0];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            return ByteUtil.byte2Hex(faceModel.getFeatValue());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * faceModel在faceMonitor的人脸特征库中检索命中的index
 | 
	
		
			
				|  |  | +     * @param faceMonitor
 | 
	
		
			
				|  |  | +     * @param faceModel
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static FaceRetrieveResultVO getHitResult(FaceMonitor faceMonitor, FaceModel faceModel) {
 | 
	
		
			
				|  |  | +        if (faceMonitor == null){
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        FaceRecogRetrieveResult faceRecogRetrieveResult = faceMonitor.faceRecogRetrieveFaceDataset(faceModel);
 | 
	
		
			
				|  |  | +        if(faceRecogRetrieveResult != null) {
 | 
	
		
			
				|  |  | +            int index = faceRecogRetrieveResult.getOnePredictIds()[0];
 | 
	
		
			
				|  |  | +            float score = faceRecogRetrieveResult.getOnePredictScores()[0];
 | 
	
		
			
				|  |  | +            System.out.println("score888888==" + score);
 | 
	
		
			
				|  |  | +            if (score >= 65) {
 | 
	
		
			
				|  |  | +                FaceRetrieveResultVO faceRetrieveResultVO = new FaceRetrieveResultVO();
 | 
	
		
			
				|  |  | +                faceRetrieveResultVO.setIndex(index);
 | 
	
		
			
				|  |  | +                faceRetrieveResultVO.setScore(score);
 | 
	
		
			
				|  |  | +                return faceRetrieveResultVO;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static boolean isHit(String idenRoot, String featPtrVisitor, List<String> featPtrList){
 | 
	
		
			
				|  |  | +        //初始化引擎
 | 
	
		
			
				|  |  | +        FaceMonitor faceMonitor = new FaceMonitor();
 | 
	
		
			
				|  |  | +        faceMonitor.init(idenRoot + "face/model", 0);
 | 
	
		
			
				|  |  | +        FaceModel[] dataset = new FaceModel[featPtrList.size()];
 | 
	
		
			
				|  |  | +        for (int i = 0; i < featPtrList.size(); i++) {
 | 
	
		
			
				|  |  | +            String featPtr = featPtrList.get(i);
 | 
	
		
			
				|  |  | +            FaceModel faceModel = new FaceModel();
 | 
	
		
			
				|  |  | +            faceModel.setName(String.valueOf(i));
 | 
	
		
			
				|  |  | +            faceModel.setPersonId(i);
 | 
	
		
			
				|  |  | +            faceModel.setFeatValue(ByteUtil.hex2Byte(featPtr));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        faceMonitor.loadFaceDataset(dataset);
 | 
	
		
			
				|  |  | +        FaceModel faceModelPtrVisitor = new FaceModel();
 | 
	
		
			
				|  |  | +        faceModelPtrVisitor.setName(String.valueOf(0));
 | 
	
		
			
				|  |  | +        faceModelPtrVisitor.setPersonId(0);
 | 
	
		
			
				|  |  | +        faceModelPtrVisitor.setFeatValue(ByteUtil.hex2Byte(featPtrVisitor));
 | 
	
		
			
				|  |  | +        FaceRetrieveResultVO vo = getHitResult(faceMonitor,faceModelPtrVisitor);
 | 
	
		
			
				|  |  | +        if(vo != null) {
 | 
	
		
			
				|  |  | +            return true;
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 使用dataPath的图片去库里查找,返回命中结果
 | 
	
		
			
				|  |  | +     * @param idenRoot
 | 
	
		
			
				|  |  | +     * @param dataPath
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static RecogResult watchFrame(String idenRoot,  String dataPath) {
 | 
	
		
			
				|  |  | +        //初始化引擎
 | 
	
		
			
				|  |  | +        FaceMonitor faceMonitor = new FaceMonitor();
 | 
	
		
			
				|  |  | +        faceMonitor.init(idenRoot + "face/model", 0);
 | 
	
		
			
				|  |  | +        Image image = faceMonitor.readImage(dataPath);
 | 
	
		
			
				|  |  | +        return faceMonitor.faceRecogMonitor(image);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //释放人脸引擎
 | 
	
		
			
				|  |  | +    public static void releaseEngine(FaceMonitor faceMonitor) {
 | 
	
		
			
				|  |  | +        if(faceMonitor != null){
 | 
	
		
			
				|  |  | +            faceMonitor.releaseEngine();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * read file
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param filePath
 | 
	
		
			
				|  |  | +     * @return if file not exist, return null, else return content of file
 | 
	
		
			
				|  |  | +     * @throws RuntimeException if an error occurs while operator BufferedReader
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static byte[] readFileBytes(String filePath) {
 | 
	
		
			
				|  |  | +        File file = new File(filePath);
 | 
	
		
			
				|  |  | +        if (file == null || !file.isFile()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        InputStream inputStream = null;
 | 
	
		
			
				|  |  | +        ByteArrayOutputStream baos = null;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            inputStream = new FileInputStream(file);
 | 
	
		
			
				|  |  | +            baos = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            byte[] buffer = new byte[1024];
 | 
	
		
			
				|  |  | +            int byteCount = 0;
 | 
	
		
			
				|  |  | +            while ((byteCount = inputStream.read(buffer)) != -1) {// 循环从输入流读取
 | 
	
		
			
				|  |  | +                // buffer字节
 | 
	
		
			
				|  |  | +                baos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            baos.flush();// 刷新缓冲区
 | 
	
		
			
				|  |  | +            return baos.toByteArray();
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +            //throw new RuntimeException("IOException occurred. ", e);
 | 
	
		
			
				|  |  | +        } finally {
 | 
	
		
			
				|  |  | +            close(inputStream);
 | 
	
		
			
				|  |  | +            close(baos);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static void close(Closeable closeable) {
 | 
	
		
			
				|  |  | +        if (closeable != null) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                closeable.close();
 | 
	
		
			
				|  |  | +            } catch (IOException e) {
 | 
	
		
			
				|  |  | +                throw new RuntimeException("IOException occurred. ", e);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |