FaceIdenTool.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.iden.bms.face;
  2. import com.face.monitor.FaceMonitor;
  3. import com.face.monitor.model.FaceModel;
  4. import org.apache.logging.log4j.LogManager;
  5. import org.apache.logging.log4j.Logger;
  6. import java.io.*;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class FaceIdenTool {
  10. private static final Logger logger = LogManager.getLogger(FaceIdenTool.class);
  11. public static FaceModel[] extractFeature(FaceMonitor faceMonitor, File[] imgFiles){
  12. List<byte[]> faceTestImageList = new ArrayList<>();
  13. for(File imgFile : imgFiles) {
  14. faceTestImageList.add(readFileBytes(imgFile.getAbsolutePath()));
  15. }
  16. FaceModel[] faceModels = faceMonitor.extractFeature(faceTestImageList);
  17. return faceModels;
  18. }
  19. public static String getFeatPtr(FaceModel[] faceModels,String name) {
  20. if(faceModels != null && faceModels.length > 0) {
  21. for(FaceModel faceModel : faceModels){
  22. logger.info("name; " + faceModel.getName());
  23. logger.info("personId: " + faceModel.getPersonId());
  24. //logger.info("featValue: " + faceModel.getFeatValue());
  25. if (name.equals(faceModel.getName())) {
  26. return new String(faceModel.getFeatValue());
  27. }
  28. }
  29. }
  30. return null;
  31. }
  32. /**
  33. * read file
  34. *
  35. * @param filePath
  36. * @return if file not exist, return null, else return content of file
  37. * @throws RuntimeException if an error occurs while operator BufferedReader
  38. */
  39. private static byte[] readFileBytes(String filePath) {
  40. File file = new File(filePath);
  41. if (file == null || !file.isFile()) {
  42. return null;
  43. }
  44. InputStream inputStream = null;
  45. ByteArrayOutputStream baos = null;
  46. try {
  47. inputStream = new FileInputStream(file);
  48. baos = new ByteArrayOutputStream();
  49. byte[] buffer = new byte[1024];
  50. int byteCount = 0;
  51. while ((byteCount = inputStream.read(buffer)) != -1) {// 循环从输入流读取
  52. // buffer字节
  53. baos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
  54. }
  55. baos.flush();// 刷新缓冲区
  56. return baos.toByteArray();
  57. } catch (IOException e) {
  58. return null;
  59. //throw new RuntimeException("IOException occurred. ", e);
  60. } finally {
  61. close(inputStream);
  62. close(baos);
  63. }
  64. }
  65. private static void close(Closeable closeable) {
  66. if (closeable != null) {
  67. try {
  68. closeable.close();
  69. } catch (IOException e) {
  70. throw new RuntimeException("IOException occurred. ", e);
  71. }
  72. }
  73. }
  74. }