VehicleEngine.java 5.44 KB
package com.objecteye.utils;

import com.objecteye.pojo.SearchDataFromRetrieveDb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class VehicleEngine {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${requestBase64}")
    private String requestBase64;
    @Value("${requestPath}")
    private String requestPath;
    @Value("${humanVehicleBase64}")
    private String humanVehicleBase64;
    @Value("${extractFeatureByBase64}")
    private String extractFeatureByBase64;
    @Value("${searchDataFromRetrieveDbPath}")
    private String searchDataFromRetrieveDbPath;
    @Value("${addUrlToDeployDb}")
    private String addUrlToDeployDb;
    @Value("${searchDataFromDeployDb}")
    private String searchDataFromDeployDb;
    @Value("${deleteDataFromDeployDb}")
    private String deleteDataFromDeployDb;

    private final Logger logger = LoggerFactory.getLogger(VehicleEngine.class);


    /**
     * 通过base64获取 车辆结构化数据信息
     *
     * @param tpxx base64参数信息
     * @return 车辆结构化数据信息
     */
    public String getVehicleInfoByBase64(String tpxx) {
        return getInfoByBase64(tpxx, requestBase64);
    }

    /**
     * 通过base64获取到人车的信息
     *
     * @param tpxx base64参数信息
     * @return 人车结构化数据
     */
    public String getHumanVehicleInfoByBase64(String tpxx) {
        return getInfoByBase64(tpxx, humanVehicleBase64);
    }

    /**
     * 根据参数获取指定uri的返回值 body体
     *
     * @param tpxx 请求参数
     * @param uri  请求地址
     * @return 返回信息body体
     */
    public String getInfoByBase64(String tpxx, String uri) {
        Map<String, String> uriVariables = new HashMap<>(16);
        String body = null;
        if (tpxx != null && tpxx.length() > 0) {
            uriVariables.put("TPXX", tpxx);
            ResponseEntity<String> responseEntity = null;
            try {
                responseEntity = restTemplate.postForEntity(uri, uriVariables, String.class);
                body = responseEntity.getBody();
            } catch (RestClientException e) {
                e.printStackTrace();
            }
        }
        return body;
    }

    /**
     * 获取人脸底库对比数据
     *
     * @param topN      选取数据量(可能不足)
     * @param threshold 设定阈值
     * @param threadNum 检索线程个数
     * @param feature   特征值数组
     * @return 符合要求的人脸对比数据
     */
    public SearchDataFromRetrieveDb searchDataFromRetrieveDb(int topN, float threshold, int threadNum, float[] feature) {
        Map<String, Object> uriVariables = new HashMap<>(16);
        SearchDataFromRetrieveDb body = null;
        if (feature != null && feature.length > 0) {
            uriVariables.put("topN", topN);
            uriVariables.put("threshold", threshold);
            uriVariables.put("threadNum", threadNum);
            uriVariables.put("feature", feature);
            ResponseEntity<SearchDataFromRetrieveDb> responseEntity = null;
            try {
                responseEntity = restTemplate.postForEntity(searchDataFromRetrieveDbPath, uriVariables, SearchDataFromRetrieveDb.class);
                body = responseEntity.getBody();
            } catch (RestClientException e) {
                logger.error("您输入的图片因路径或其他原因导致无法出现识别,请将图片放入到http://192.168.10.4:8084/home/lxu/vehicleImages/new_wfcl/路径下");
            }
        }
        return body;
    }

    /**
     * 通过base64获取到人像数据
     *
     * @param base64 base64数据
     * @return 获取人脸特征值数据
     */
    public String getPeopleInfoByBase64(String base64) {
        return getInfoByBase64(base64, extractFeatureByBase64);
    }

    /**
     * 根据参数获取人像底库数据
     *
     * @param map 请求参数
     * @return 人像底库满足条件的数据
     */
    public String searchDataFromDeployDb(Map<String, Object> map) {
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(map);
        ResponseEntity<String> response = restTemplate.postForEntity(searchDataFromDeployDb, requestEntity, String.class);
        return response.getBody();
    }

    /**
     * 根据url添加到布控库
     *
     * @param map
     * @return
     */
    public String addUrlToDeployDb(Map<String, Object> map) {
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(map);
        ResponseEntity<String> response = restTemplate.postForEntity(addUrlToDeployDb, requestEntity, String.class);
        return response.getBody();
    }

    /**
     * 删除库中的数据
     *
     * @param map
     * @return
     */
    public String deleteDataFromDeployDb(Map<String, Object> map) {
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(map);
        ResponseEntity<String> response = restTemplate.postForEntity(deleteDataFromDeployDb, requestEntity, String.class);
        return response.getBody();
    }
}