IT评测·应用市场-qidao123.com

标题: 百度人脸识别_SpringBoot整合离线SDK [打印本页]

作者: 民工心事    时间: 2023-6-9 16:17
标题: 百度人脸识别_SpringBoot整合离线SDK
一、前言

二、SDK 引入并配置

  1. @Slf4j
  2. @Component
  3. @Conditional(FaceSdkEnableCondition.class)
  4. public class Face {
  5.     // *******以下为人脸sdk api接口*********
复制代码
  1. // ********* 以下为系统加载库文件及opencv **********
  2.     private static String libPath;
  3.     public Face(){ }
  4.     @Autowired
  5.     public Face(Environment env) {
  6.         // 初始化libPath
  7.         libPath = env.getProperty("face-sdk.libPath",String.class);
  8.         // 加载dll文件
  9.         System.load(libPath + "BaiduFaceApi.dll");
  10.         System.load(libPath + "opencv_java320.dll");
  11.     }
  12.     Face api = null;
  13.     /*  sdk初始化 */
  14.     @PostConstruct
  15.     public void init() {
  16.         log.info("离线SDK开始初始化");
  17.         log.info("SDK路径:{}",libPath);
  18.         api = new Face();
  19.         int res = api.sdkInit(libPath);
  20.         if (res != 0) {
  21.             log.info("sdk init fail and error = {}\n", res);
  22.             return;
  23.         }
  24.         log.info("离线SDK初始化完成");
  25.     }
  26.     /*  sdk释放内存 */
  27.     @PreDestroy
  28.     private void destroy() {
  29.         if(api != null){
  30.             api.sdkDestroy();
  31.         }
  32.         log.info("离线SDK销毁");
  33.     }
复制代码
  1. package cn.dyina.config;
  2. public class FaceSdkEnableCondition implements Condition {
  3.     @Override
  4.     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  5.         Environment env = context.getEnvironment();
  6.         // 根据face-sdk.enable属性来决定是否创建 Face bean
  7.         return "true".equals(env.getProperty("face-sdk.enable"));
  8.     }
  9. }
复制代码
三、项目使用

  1. @SpringBootApplication(scanBasePackages = {"cn.dyina","com.jni"})
  2. public class SpringBootBaiDuFaceSdkApplication {
  3.     //...
  4. }
复制代码
  1. @Slf4j
  2. @Service
  3. public class FaceService {
  4.     @Value("${face-sdk.imagesPath}")
  5.     String imagesPath;
  6.     public String registerFace(String fileName, String nickName) {
  7.         // 注册人脸图片
  8.         String ip_nickName = fileName.replaceFirst("[.][^.]+$", "");
  9.         // 获取人脸特征值
  10.         Mat mat = Imgcodecs.imread(imagesPath + fileName);
  11.         long matAddr = mat.getNativeObjAddr();
  12.         // 填充人脸信息 后期将从数据库获取
  13.         String userInfo = nickName;
  14.         String userId = ip_nickName.replace(".","");
  15.         String groupId = "Face";
  16.         // 用人脸特征值注册
  17.         JSONObject res = JSONObject.parseObject(Face.userAddByMat(matAddr, userId, groupId, userInfo));
  18.         log.info("user add result is:{}", res);
  19.         return res.getString("msg");
  20.     }
  21.     public String identifyFace(String fileName) {
  22.         // 调用人脸sdkAPI
  23.         Face.loadDbFace();
  24.         Mat mat1 = Imgcodecs.imread(imagesPath + fileName);
  25.         long mat1Addr = mat1.getNativeObjAddr();
  26.         int type = 0;
  27.         // 和人脸库里面的人脸特征值比较(人脸识别)
  28.         JSONObject res = JSONObject.parseObject(Face.identifyWithAllByMat(mat1Addr, type));
  29.         log.info("identify res is:{}", res);
  30.         if (!res.getString("errno").equals("0")) {
  31.             return res.getString("msg");
  32.         }
  33.         // 获取人脸识别信息
  34.         double score = res.getJSONObject("data")
  35.                 .getJSONArray("result")
  36.                 .getJSONObject(0)
  37.                 .getDouble("score");
  38.         String userId = res.getJSONObject("data")
  39.                 .getJSONArray("result")
  40.                 .getJSONObject(0)
  41.                 .getString("user_id");
  42.         if (score > 80) {
  43.             String nickName = userId.split("_")[1];
  44.             return nickName;
  45.         } else {
  46.             log.info("根据图片获取人员信息失败");
  47.             return "Match score is low";
  48.         }
  49.     }
  50. }
复制代码
  1. /**
  2. *  人脸识别 页面相关接口
  3. */
  4. @Slf4j
  5. @RestController
  6. @CrossOrigin(origins = "*", maxAge = 3600)
  7. public class FaceController {
  8.     @Autowired
  9.     private FaceService faceService;
  10.     @Value("${face-sdk.imagesPath}")
  11.     String imagesPath;
  12.     /**
  13.      * 人脸注册
  14.      * @param file
  15.      * @param ip
  16.      * @param nickName
  17.      * @return
  18.      * @throws IOException
  19.      */
  20.     @PostMapping("/faceRegister")
  21.     public R<String> faceRegister(@RequestParam("photo") MultipartFile file,
  22.                                   @RequestParam("ip") String ip,
  23.                                   @RequestParam("nickName") String nickName) throws IOException {
  24.         String photo = Base64.getEncoder().encodeToString(file.getBytes());
  25.         String fileName = ip + "_" + nickName + ".jpg";
  26.         Base64ToImage.saveImage(imagesPath, fileName, photo);
  27.         // 人脸注册
  28.         String res = faceService.registerFace(fileName, nickName);
  29.         if(!res.equals("success")){
  30.             return R.error(res,null);
  31.         }
  32.         return R.success(null);
  33.     }
  34.     /**
  35.      * 检测人脸
  36.      * @param photo
  37.      * @param ip
  38.      * @return
  39.      */
  40.     @PostMapping("/faceDetection")
  41.     public R<String> faceDetection(@RequestParam("photo") String photo, @RequestParam("ip") String ip) {
  42.         String fileName = ip +".jpg"; // 临时存储,用于检测
  43.         Base64ToImage.saveImage(imagesPath, fileName, photo);
  44.         String res = faceService.identifyFace(fileName);
  45.         return R.success(res);
  46.     }
  47.     /**
  48.      * 查询用户组人脸
  49.      * @param groupId
  50.      * @return
  51.      */
  52.     @GetMapping("/getAllFace")
  53.     public R<List> getAllFace(@RequestParam("groupId") String groupId){
  54.         log.info("====>> getAllFace");
  55.         List<String> userIdList = faceService.getAllFace(groupId);
  56.         return R.success(userIdList);
  57.     }
  58. }
复制代码
后记


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4