whycxzp
2021-02-05 172c73ced959d5fec2f8b3a992d20b49780f57a5
update faceIdentify test
1个文件已添加
1个文件已修改
213 ■■■■■ 已修改文件
nfdw_stand/src/com/fgkj/actions/face/FaceIdentifyAction.java 190 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nfdw_stand/src/com/fgkj/dto/PhotoInfo.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nfdw_stand/src/com/fgkj/actions/face/FaceIdentifyAction.java
@@ -7,9 +7,7 @@
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;
@@ -29,6 +27,7 @@
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;
@@ -674,6 +673,191 @@
        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 {
nfdw_stand/src/com/fgkj/dto/PhotoInfo.java
New file
@@ -0,0 +1,23 @@
package com.fgkj.dto;
public class PhotoInfo {
    private Integer photoType;
    private PageInfo pageInfo;
    public Integer getPhotoType() {
        return photoType;
    }
    public void setPhotoType(Integer photoType) {
        this.photoType = photoType;
    }
    public PageInfo getPageInfo() {
        return pageInfo;
    }
    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }
}