|
@@ -0,0 +1,134 @@
|
|
|
+package com.iden.bms.face;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.face.monitor.FaceMonitor;
|
|
|
+import com.face.monitor.model.FaceModel;
|
|
|
+import com.iden.common.entity.IdenCamera;
|
|
|
+import com.iden.common.entity.IdenCommunity;
|
|
|
+import com.iden.common.entity.IdenImageStore;
|
|
|
+
|
|
|
+import com.iden.common.service.IdenCameraService;
|
|
|
+import com.iden.common.service.IdenCommunityService;
|
|
|
+import com.iden.common.service.IdenImageStoreService;
|
|
|
+
|
|
|
+import com.iden.common.util.DateUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: stw
|
|
|
+ * @Date: 2021/7/14
|
|
|
+ * @Desc:
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class FaceIdenService {
|
|
|
+ @Resource
|
|
|
+ private IdenImageStoreService idenImageStoreService;
|
|
|
+ @Resource
|
|
|
+ private IdenCameraService idenCameraService;
|
|
|
+ @Resource
|
|
|
+ private IdenCommunityService idenCommunityService;
|
|
|
+
|
|
|
+ @Value("${data.image.root:#{null}}")
|
|
|
+ private String imageRoot;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+
|
|
|
+ public void exe() throws Exception {
|
|
|
+
|
|
|
+ File srcImageDirFile = new File(imageRoot + "src/image");
|
|
|
+ //初始化引擎
|
|
|
+ FaceMonitor faceMonitor = new FaceMonitor();
|
|
|
+ faceMonitor.init("./model", 0);
|
|
|
+
|
|
|
+ //使用摄像头编码做目录名,里面图像或视频的名称上加上拍照时间,
|
|
|
+ // 比如 2021-12-17_12:30:23.jpg 2021-12-17_12:30:23~2021-12-17_12:33:23.avi
|
|
|
+ if (srcImageDirFile.isDirectory()) {
|
|
|
+ File[] cameraCodeDirs = srcImageDirFile.listFiles();
|
|
|
+ if(cameraCodeDirs != null && cameraCodeDirs.length > 0){
|
|
|
+ for(File cameraCodedir : cameraCodeDirs) {
|
|
|
+ if(cameraCodedir.isDirectory()){
|
|
|
+ File[] imgFiles = cameraCodedir.listFiles();
|
|
|
+ if(imgFiles != null && imgFiles.length > 0){
|
|
|
+ String cameraCode = cameraCodedir.getName();
|
|
|
+ QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(IdenCamera::getCode,cameraCode);
|
|
|
+ IdenCamera idenCamera = idenCameraService.getOne(queryWrapper);
|
|
|
+ if(idenCamera == null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取特征码
|
|
|
+ FaceModel[] faceModels = FaceIdenTool.extractFeature(faceMonitor,imgFiles);
|
|
|
+
|
|
|
+ List<IdenImageStore> idenImageStoreList = new ArrayList<>();
|
|
|
+ for(File imgFile : imgFiles) {
|
|
|
+ IdenImageStore idenImageStore = new IdenImageStore();
|
|
|
+ idenImageStore.setCameraId(idenCamera.getId());
|
|
|
+ idenImageStore.setCommunityId(idenCamera.getCommunityId());
|
|
|
+ String photographPlace = idenCamera.getPlace();
|
|
|
+ if (StringUtils.isEmpty(photographPlace)) {
|
|
|
+ IdenCommunity idenCommunity = idenCommunityService.getById(idenCamera.getCommunityId());
|
|
|
+ if(idenCommunity != null) {
|
|
|
+ photographPlace = idenCommunity.getName() + "(" + idenCamera.getLongitude() + ":" + idenCamera.getLatitude() + ")";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ idenImageStore.setPhotographPlace(photographPlace);
|
|
|
+
|
|
|
+ String photographTime = imgFile.getName().replace(".jpg","");
|
|
|
+ idenImageStore.setPhotographTime(DateUtils.strToDate(photographTime,"yyyy-MM-dd_HH:mm:ss"));
|
|
|
+
|
|
|
+ idenImageStore.setFeatPtr(FaceIdenTool.getFeatPtr(faceModels,imgFile.getName()));
|
|
|
+
|
|
|
+ File finalDir = new File(imgFile.getParentFile().getAbsolutePath().replace("src","final"));
|
|
|
+ if(!finalDir.exists()){
|
|
|
+ finalDir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ File finalImgFile = new File(finalDir + imgFile.getName());
|
|
|
+ imgFile.renameTo(finalImgFile);//移动到最终目录
|
|
|
+
|
|
|
+ idenImageStore.setImage(finalImgFile.getAbsolutePath());
|
|
|
+
|
|
|
+ //TODO 关联personId
|
|
|
+
|
|
|
+ idenImageStoreList.add(idenImageStore);
|
|
|
+ }
|
|
|
+
|
|
|
+ //把图像存到图库中
|
|
|
+ if (CollUtil.isNotEmpty(idenImageStoreList)) {
|
|
|
+ idenImageStoreService.saveBatch(idenImageStoreList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String faceTestDir = "./data/";
|
|
|
+ String faceFilePath = String.format(Locale.CHINA, "%s%d.jpg", faceTestDir, 1);
|
|
|
+ System.out.println(faceFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|