All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahuatech.icc.multiinone.face.FaceSearchSDK Maven / Gradle / Ivy

The newest version!
package com.dahuatech.icc.multiinone.face;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.exception.BusinessException;
import com.dahuatech.icc.multiinone.face.vo.FaceSearchRequest;
import com.dahuatech.icc.multiinone.face.vo.FaceSearchResponse;
import com.dahuatech.icc.multiinone.utils.ImageUtils;
import com.dahuatech.icc.multiinone.utils.StringUtils;
import com.dahuatech.icc.oauth.http.IccClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;

import java.util.Calendar;

public class FaceSearchSDK {

    protected final Log logger = LogFactory.get();

    public FaceSearchResponse faceSearchForIvs(FaceSearchRequest request){
        FaceSearchResponse baseResponse = new FaceSearchResponse();
        try{
            request.valid();
            baseResponse.setCurrentPage(request.getPageNum());
            baseResponse.setPageSize(request.getPageSize());
            baseResponse.setCode(Constants.SUCESS_CODE);
            baseResponse.setErrMsg(Constants.SUCCESS_MSG);
            if(StringUtils.isEmpty(request.getSessionId())){
                String sessioinId = startFaceSearch(request);
                request.setSessionId(sessioinId);
                boolean isSuccess = checkIsFinish(request,sessioinId);
                if(isSuccess){
                    return page(request);
                }else{
                    return baseResponse;
                }
            }else{
                return page(request);
            }
        }catch (BusinessException businessException){
            logger.error("人像以图搜图失败{}",businessException,businessException.getMessage());
            baseResponse.setCode(businessException.code);
            baseResponse.setErrMsg(businessException.msg);
        }catch (Exception e){
            logger.error("人像以图搜图异常{}",e,e.getMessage());
            baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            baseResponse.setCode(Constants.SYSTEMERROR_CODE);
        }
        return baseResponse;
    }

    private String startFaceSearch(FaceSearchRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = "/evo-apigw/evo-face/faceSearch/third/faceSearchForIvs";
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject jsonObject = JSONUtil.parseObj(JSONUtil.toJsonStr(request));
            jsonObject.remove("pageNum");
            jsonObject.remove("pageSize");
            jsonObject.remove("ordered");
            jsonObject.remove("image");
            jsonObject.put("ordered",request.getOrdered());
            jsonObject.put("devType","1");
            jsonObject.put("base64Img", ImageUtils.convertToBase64Img(request.getImage()));
            jsonObject.remove("oauthConfigBaseInfo");
            generalRequest.setBody(jsonObject.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("人像以图搜图(IVSS)接口异常{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!generalResponse.isSuccess()){
            logger.error("人像以图搜图(IVSS)失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        if(jsonObject.getStr("data") == null){
            logger.error("人像以图搜图(IVSS)接口异常,进程号为空");
        }
        return jsonObject.getStr("data");
    }

    private boolean checkIsFinish(FaceSearchRequest request,String sessionId) throws ClientException {
        Integer integer = null;
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,request.getQueryTimeOut());
        Long endTime = calendar.getTimeInMillis();
        int i = 1;
        while(true){
            logger.info("人像以图搜图(IVSS)进度处理查询sessionId={},第[{}]次",sessionId,i);
            integer = queryProgress(request);
            if(integer == 1){
                logger.info("人像以图搜图(IVSS)进度处理查询sessionId={},第[{}]次--完成",sessionId,i);
                return true;
            }
            try {
                Thread.sleep(100l);
            } catch (InterruptedException e) {
                logger.error("人像以图搜图(IVSS)进度处理异常{}",e,e.getMessage());
            }
            if(endTime <= System.currentTimeMillis()){
                logger.info("人像以图搜图(IVSS)进度处理查询sessionId={},第[{}]次,未查到",sessionId,i);
                return false;
            }
            i++;
        }
    }

    private Integer queryProgress(FaceSearchRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = String.format("/evo-apigw/evo-face/faceSearch/third/queryProgress/%s",request.getSessionId());
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.GET);
        GeneralResponse generalResponse = null;
        try{
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
            logger.info("人像以图搜图(IVSS)进度查询接口sessionId={},返回报文:{}",request.getSessionId(),JSONUtil.toJsonStr(generalResponse));
        }catch (Exception e){
            logger.error("人像以图搜图(IVSS)进度查询接口异常{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!generalResponse.isSuccess()){
            logger.error("人像以图搜图(IVSS)进度查询失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            return 0;
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        JSONObject data = jsonObject.getJSONObject("data");
        if(data == null){
            logger.error("人像以图搜图(IVSS)进度查询接口异常,数据为空");
            return 0;
        }
        Integer integer = data.getInt("taskStatus");
        return integer;
    }

    private FaceSearchResponse page(FaceSearchRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = "/evo-apigw/evo-face/faceSearch/third/page";
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("searchGroupType",request.getSearchGroupType());
            jsonObject.put("pageNum",request.getPageNum());
            jsonObject.put("pageSize",request.getPageSize());
            jsonObject.put("ordered",request.getOrdered());
            jsonObject.put("sessionId",request.getSessionId());
            generalRequest.setBody(jsonObject.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("人像以图搜图(IVSS)分页查询接口异常{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!generalResponse.isSuccess()){
            logger.error("人像以图搜图(IVSS)分页查询失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        FaceSearchResponse faceSearchResponse = JSONUtil.toBean(jsonObject.getJSONObject("data"),FaceSearchResponse.class);
        faceSearchResponse.setSessionId(request.getSessionId());
        faceSearchResponse.setCode(Constants.SUCESS_CODE);
        faceSearchResponse.setErrMsg(Constants.SUCCESS_MSG);
        return faceSearchResponse;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy