1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package com.iden.bms.face;
- import com.face.monitor.FaceMonitor;
- import com.face.monitor.model.FaceModel;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- public class FaceIdenTool {
- private static final Logger logger = LogManager.getLogger(FaceIdenTool.class);
- public static FaceModel[] extractFeature(FaceMonitor faceMonitor, File[] imgFiles){
- List<byte[]> faceTestImageList = new ArrayList<>();
- for(File imgFile : imgFiles) {
- faceTestImageList.add(readFileBytes(imgFile.getAbsolutePath()));
- }
- FaceModel[] faceModels = faceMonitor.extractFeature(faceTestImageList);
- return faceModels;
- }
- public static String getFeatPtr(FaceModel[] faceModels,String name) {
- if(faceModels != null && faceModels.length > 0) {
- for(FaceModel faceModel : faceModels){
- logger.info("name; " + faceModel.getName());
- logger.info("personId: " + faceModel.getPersonId());
- //logger.info("featValue: " + faceModel.getFeatValue());
- if (name.equals(faceModel.getName())) {
- return new String(faceModel.getFeatValue());
- }
- }
- }
- return null;
- }
- /**
- * 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);
- }
- }
- }
- }
|