| | |
| | | import com.arcsoft.face.toolkit.ImageFactory; |
| | | import com.arcsoft.face.toolkit.ImageInfo; |
| | | import com.fgkj.actions.ActionUtil; |
| | | import com.fgkj.dto.PageInfo; |
| | | import com.fgkj.dto.ServiceModel; |
| | | import com.fgkj.dto.User_inf; |
| | | import com.fgkj.dto.*; |
| | | import com.fgkj.services.User_infService; |
| | | import com.fgkj.services.User_permitgroupService; |
| | | import com.fgkj.services.face.FaceIdentifyService; |
| | |
| | | import java.nio.charset.Charset; |
| | | import java.sql.SQLException; |
| | | import java.util.*; |
| | | import java.util.Map; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | |
| | |
| | | map.put("successPicName", ""); |
| | | map.put("failNum", 0); |
| | | map.put("failPicName", ""); |
| | | map.put("failReason",""); |
| | | map.put("failReason", ""); |
| | | map.put("testTime", 0); |
| | | InputStream inputStream2 = null; |
| | | try { |
| | |
| | | fot.flush(); |
| | | if (map.get("failPicName").equals("")) { |
| | | map.put("failPicName", picName); |
| | | map.put("failReason",model.getMsg()); |
| | | map.put("failReason", model.getMsg()); |
| | | } else { |
| | | map.put("failPicName", picName + "," + map.get("failPicName")); |
| | | map.put("failReason",model.getMsg() + "," + map.get("failReason")); |
| | | map.put("failReason", model.getMsg() + "," + map.get("failReason")); |
| | | } |
| | | |
| | | } finally { |
| | |
| | | return SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * 人脸识别:活体校验,相似度对比,1比N,认证测试误判率-改版 |
| | | * 图片直接放置到服务器: |
| | | * <p> |
| | | * 测试样本库1:nfdw_photo_sample1 |
| | | * 测试样本库2:nfdw_photo_sample2 |
| | | * 基础样本:nfdw_photo_sample_basic |
| | | * |
| | | * @return |
| | | */ |
| | | public String faceCompare2NPlus() { |
| | | |
| | | //项目的webapps地址 |
| | | String realPath = getApplication().getRealPath("/"); |
| | | String[] split = realPath.split("\\\\"); |
| | | String projectName = split[split.length - 1]; |
| | | String webAppPath = realPath.substring(0, realPath.lastIndexOf(projectName)); |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | ServiceModel model = new ServiceModel(); |
| | | HashMap<Object, Object> map = new HashMap<>(); |
| | | // HashMap<Object, Object> failMap = new HashMap<>(); |
| | | map.put("successNum", 0); |
| | | map.put("successPicName", ""); |
| | | map.put("failNum", 0); |
| | | map.put("failPicName", ""); |
| | | map.put("failReason", ""); |
| | | map.put("testTime", 0); |
| | | InputStream inputStream2 = null; |
| | | try { |
| | | //初始化成功图片的文件夹 |
| | | Map picDirNameMap = initPictureFileDir(); |
| | | String successPicDirName = (String) picDirNameMap.get("success"); |
| | | String failPicDirName = (String) picDirNameMap.get("fail"); |
| | | |
| | | //获取前端传递过来的图片名称(xxx.jpg)和样本库号(1/2) |
| | | //传输格式为:json:{"id":1 或者2,"url":"xxx.jpg"} |
| | | Face face = ActionUtil.getGson().fromJson(json, Face.class); |
| | | ImageInfo imageInfo2 = null; |
| | | if(face.getId()==1) { |
| | | imageInfo2 = ImageFactory.getRGBData(new File(webAppPath + File.separator + projectName + "_photo_sample1")); |
| | | }else{ |
| | | imageInfo2 = ImageFactory.getRGBData(new File(webAppPath + File.separator + projectName + "_photo_sample2")); |
| | | } |
| | | FaceEngine faceEngine = FaceEngineFactory.getInstance(); |
| | | //人脸检测 |
| | | List<FaceInfo> faceInfoList2 = new ArrayList<>(); |
| | | faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), imageInfo2.getImageFormat(), faceInfoList2); |
| | | |
| | | //特征提取 |
| | | FaceFeature faceFeature2 = new FaceFeature(); |
| | | faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), imageInfo2.getImageFormat(), faceInfoList2.get(0), faceFeature2); |
| | | |
| | | //基础样本遍历 |
| | | File basicSampleFile = new File(webAppPath + File.separator + projectName + "_photo_sample_basic"); |
| | | File[] files = basicSampleFile.listFiles(); |
| | | |
| | | for(File tmpFile:files) { |
| | | InputStream inputStream = new FileInputStream(tmpFile); |
| | | ByteArrayOutputStream byteArrayOutputStream = cloneInputStream(inputStream); |
| | | inputStream2 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); |
| | | inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); |
| | | |
| | | ImageInfo imageInfo = ImageFactory.getRGBData(inputStream); |
| | | |
| | | if (faceEngine != null) { |
| | | model = FaceIdentifyUtil.liveDetect(faceEngine, imageInfo); |
| | | // model.setCode(1); |
| | | //活体校验通过 |
| | | if (model.getCode() == 1) { |
| | | model = FaceIdentifyUtil.orientAndPxDetect(faceEngine, imageInfo); |
| | | //人像角度,大小校验通过 |
| | | if (model.getCode() == 1) { |
| | | //对比成功,保存成功次数,存储成功图片 |
| | | model = FaceIdentifyUtil.faceCompare2N(faceEngine, imageInfo, faceFeature2); |
| | | } |
| | | } |
| | | |
| | | if (model.getCode() == 1) { |
| | | map.put("successNum", (Integer) (map.get("successNum")) + 1); |
| | | |
| | | String picName = file.getName(); |
| | | String fileName = successPicDirName + "\\" + picName; |
| | | File file = new File(fileName); |
| | | |
| | | |
| | | FileOutputStream fot = new FileOutputStream(file); |
| | | try { |
| | | byte[] buffer = new byte[1024]; |
| | | int len = 0; |
| | | while ((len = inputStream2.read(buffer)) > 0) { |
| | | fot.write(buffer, 0, len); |
| | | } |
| | | fot.flush(); |
| | | if (map.get("successPicName").equals("")) { |
| | | map.put("successPicName", picName); |
| | | } else { |
| | | map.put("successPicName", picName + "," + map.get("successPicName")); |
| | | } |
| | | } finally { |
| | | fot.flush(); |
| | | fot.close(); |
| | | } |
| | | |
| | | } else { |
| | | map.put("failNum", (Integer) (map.get("failNum")) + 1); |
| | | |
| | | |
| | | String picName = file.getName(); |
| | | String fileName = failPicDirName + "\\" + picName; |
| | | File file = new File(fileName); |
| | | |
| | | FileOutputStream fot = new FileOutputStream(file); |
| | | try { |
| | | byte[] buffer = new byte[1024]; |
| | | int len = 0; |
| | | while ((len = inputStream2.read(buffer)) > 0) { |
| | | fot.write(buffer, 0, len); |
| | | } |
| | | fot.flush(); |
| | | if (map.get("failPicName").equals("")) { |
| | | map.put("failPicName", picName); |
| | | map.put("failReason", model.getMsg()); |
| | | } else { |
| | | map.put("failPicName", picName + "," + map.get("failPicName")); |
| | | map.put("failReason", model.getMsg() + "," + map.get("failReason")); |
| | | } |
| | | |
| | | } finally { |
| | | fot.flush(); |
| | | fot.close(); |
| | | } |
| | | } |
| | | model.setCode(1); |
| | | } else { |
| | | model.setCode(0); |
| | | model.setMsg("初始化引擎失败"); |
| | | } |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | if (inputStream2 != null) { |
| | | inputStream2.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | //匹配成功率 |
| | | Integer total = (int) map.get("total"); |
| | | Integer successNum = (int) map.get("successNum"); |
| | | double successRate = BigDecimal.valueOf(successNum).divide(BigDecimal.valueOf(total), 7, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | map.put("successRate", successRate); |
| | | |
| | | long endTime = System.currentTimeMillis(); |
| | | Long testTime = endTime - startTime; |
| | | map.put("testTime", testTime.doubleValue() / 1000); |
| | | model.setData(map); |
| | | |
| | | result = ActionUtil.tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * 展示样本库1及2的列表,分页: |
| | | * 传入json:{"photoType":1/2,"pageInfo":{"pageNum":1,"pageSize:6"}} |
| | | * |
| | | */ |
| | | public String pagePhotos(){ |
| | | PhotoInfo photoInfo = ActionUtil.getGson().fromJson(json, PhotoInfo.class); |
| | | //项目的webapps地址 |
| | | String realPath = getApplication().getRealPath("/"); |
| | | String[] split = realPath.split("\\\\"); |
| | | String projectName = split[split.length - 1]; |
| | | String webAppPath = realPath.substring(0, realPath.lastIndexOf(projectName)); |
| | | File sample1 = new File(webAppPath + File.separator + projectName + "_photo_sample1"); |
| | | File sample2 = new File(webAppPath + File.separator + projectName + "_photo_sample2"); |
| | | |
| | | //Perry TODO |
| | | return null; |
| | | } |
| | | |
| | | public static void zipTest() { |
| | | ZipFile zipFile = null; |
| | | try { |