| | |
| | | import com.google.gson.reflect.TypeToken; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.commons.fileupload.MultipartStream; |
| | | import org.apache.commons.io.filefilter.NotFileFilter; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import sun.misc.BASE64Decoder; |
| | | import sun.misc.BASE64Encoder; |
| | |
| | | /** |
| | | * 人脸识别:活体校验,相似度对比,1比N,认证测试误判率-改版 |
| | | * 图片直接放置到服务器: |
| | | * <p> |
| | | * 测试样本库1:nfdw_photo_sample1 |
| | | * 测试样本库2:nfdw_photo_sample2 |
| | | * 基础样本:nfdw_photo_sample_basic |
| | | * |
| | | * 基础人脸库: nfdw_photo_sample |
| | | * 每个人脸的侧面库: nfdw_photo_sample_group/01~0xx |
| | | * 基础样本库: nfdw_photo_sample_basic |
| | | *=============================================== |
| | | * 对比步骤: |
| | | * 每个人脸对比基础样本库 类型为id=1,url为人脸库中的单个人脸正面照 |
| | | * 每个人脸对比每个人脸的侧面库 id=2 |
| | | * |
| | | * @return |
| | | */ |
| | |
| | | map.put("successPicName", ""); |
| | | map.put("failNum", 0); |
| | | map.put("failPicName", ""); |
| | | map.put("failReason", ""); |
| | | //map.put("failReason", ""); |
| | | map.put("testTime", 0); |
| | | map.put("total", 0); |
| | | InputStream inputStream2 = null; |
| | | try { |
| | | //初始化成功图片的文件夹 |
| | |
| | | |
| | | //获取前端传递过来的图片名称(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"+File.separator+face.getUrl())); |
| | | }else{ |
| | | imageInfo2 = ImageFactory.getRGBData(new File(webAppPath + File.separator + projectName + "_photo_sample2"+File.separator+face.getUrl())); |
| | | } |
| | | Face face = getGson().fromJson(json, Face.class); |
| | | //Face face = new Face(); |
| | | //face.setId(1); |
| | | //face.setId(2); |
| | | //face.setUrl("1.jpg"); |
| | | ImageInfo imageInfo2 = ImageFactory.getRGBData(new File(webAppPath + File.separator + projectName + "_photo_sample"+File.separator+face.getUrl())); |
| | | |
| | | FaceEngine faceEngine = FaceEngineFactory.getInstance(); |
| | | //人脸检测 |
| | | List<FaceInfo> faceInfoList2 = new ArrayList<>(); |
| | |
| | | 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(); |
| | | File[] files =null; |
| | | if(face.getId()==1) { |
| | | //对比图片库为基础样本库 |
| | | File basicSampleFile = new File(webAppPath + File.separator + projectName + "_photo_sample_basic"); |
| | | files = basicSampleFile.listFiles(); |
| | | }else{ |
| | | //对比图片库为侧面照 |
| | | File basicSampleFile = new File(webAppPath + File.separator + projectName + "_photo_sample_group"+File.separator+getDirName(face.getUrl())); |
| | | files = basicSampleFile.listFiles(); |
| | | } |
| | | |
| | | for(File tmpFile:files) { |
| | | //for(File tmpFile:files) { |
| | | for (int i = 0; i < files.length-1; i++) { |
| | | File tmpFile = files[i]; |
| | | InputStream inputStream = new FileInputStream(tmpFile); |
| | | ByteArrayOutputStream byteArrayOutputStream = cloneInputStream(inputStream); |
| | | inputStream2 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); |
| | |
| | | if (model.getCode() == 1) { |
| | | map.put("successNum", (Integer) (map.get("successNum")) + 1); |
| | | |
| | | String picName = file.getName(); |
| | | String picName = tmpFile.getName(); |
| | | String fileName = successPicDirName + "\\" + picName; |
| | | File file = new File(fileName); |
| | | |
| | |
| | | map.put("failNum", (Integer) (map.get("failNum")) + 1); |
| | | |
| | | |
| | | String picName = file.getName(); |
| | | String picName = tmpFile.getName(); |
| | | String fileName = failPicDirName + "\\" + picName; |
| | | File file = new File(fileName); |
| | | |
| | |
| | | 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 { |
| | |
| | | } |
| | | } |
| | | model.setCode(1); |
| | | map.put("total",(int) map.get("total")+1); |
| | | } else { |
| | | model.setCode(0); |
| | | model.setMsg("初始化引擎失败"); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 展示样本库1及2的列表,分页: |
| | | * 传入json:{"photoType":1/2,"pageInfo":{"pageNum":1,"pageSize":6}} |
| | | * |
| | | * 获取人脸库的所有人脸图片 |
| | | */ |
| | | public String pagePhotos(){ |
| | | //返回的PageBean |
| | | List<String> sampleList = new ArrayList<>(); |
| | | PhotoInfo photoInfo = ActionUtil.getGson().fromJson(json, PhotoInfo.class); |
| | | PageInfo<String> pageInfo = photoInfo.getPageInfo(); |
| | | int pageNum = pageInfo.getPageNum(); |
| | | int pageSize = pageInfo.getPageSize(); |
| | | List<String> fileNameList = new ArrayList<>(); |
| | | //项目的webapps地址 |
| | | String realPath = ActionUtil.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"); |
| | | File sample = new File(webAppPath + File.separator + projectName + "_photo_sample"); |
| | | |
| | | String[] sampleArr = null; |
| | | if(photoInfo.getPhotoType()==1){ |
| | | sampleArr = sample1.list(); |
| | | }else{ |
| | | sampleArr = sample2.list(); |
| | | } |
| | | int totalSize = sampleArr.length; |
| | | for (int i = (pageNum-1)*pageSize; i < pageNum*pageSize; i++) { |
| | | if(i<totalSize) { |
| | | sampleList.add(sampleArr[i]); |
| | | }else{ |
| | | break; |
| | | /*File[] sampleArr = sample.listFiles(new FileFilter() { |
| | | @Override |
| | | public boolean accept(File pathname) { |
| | | //过滤末尾是.docx的文件 |
| | | return String.valueOf(pathname).endsWith(".jpg"); |
| | | } |
| | | } |
| | | pageInfo.setTotalSize(totalSize); |
| | | pageInfo.setTotalPage((int)Math.round(totalSize*1.0/pageSize)); |
| | | pageInfo.setObj(sampleList); |
| | | result = ActionUtil.getGson().toJson(pageInfo); |
| | | });*/ |
| | | |
| | | String[] fileNameArr = sample.list(); |
| | | /*for (int i = 0; i < fileNameArr.length; i++) { |
| | | |
| | | }*/ |
| | | |
| | | result = ActionUtil.getGson().toJson(fileNameArr); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * 文件夹名称处理, |
| | | * <100,补全为3位 |
| | | * >100的,补全为4位 |
| | | * */ |
| | | private static String getDirName(String photoName){ |
| | | photoName = photoName.split("\\.")[0]; |
| | | String dirName = photoName; |
| | | int length = photoName.length(); |
| | | if(length<3){ |
| | | int zeroNum = 3 - length; |
| | | for (int i = 0; i < zeroNum; i++) { |
| | | dirName = 0+dirName; |
| | | } |
| | | }else{ |
| | | dirName = 0+dirName; |
| | | } |
| | | return dirName; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(getDirName("1.jpg")); |
| | | System.out.println(getDirName("11.jpg")); |
| | | System.out.println(getDirName("111.jpg")); |
| | | } |
| | | |
| | | public static void zipTest() { |
| | | ZipFile zipFile = null; |
| | | try { |