Commit 73c4f50b by xmh

init

0 parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xmh.es</groupId>
<artifactId>xmh-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xmh-test</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.viontech.keliu</groupId>
<artifactId>keliu-base</artifactId>
<version>6.0.8-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.viontech.keliu</groupId>
<artifactId>keliu-storage</artifactId>
<version>6.0.8-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.viontech.keliu</groupId>
<artifactId>keliu-storage</artifactId>
<version>6.0.8-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.viontech.keliu</groupId>
<artifactId>AlgApiClient</artifactId>
<version>6.0.4-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</exclusion>
<exclusion>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-websocket -->
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-websocket</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>application-*.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.xmh.es.match;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.viontech.keliu", "com.xmh.es"})
public class XmhTestApplication {
public final static String POOL_NAME = "test_pool";
public static void main(String[] args) {
SpringApplication.run(XmhTestApplication.class, args);
}
}
package com.xmh.es.match.config;
import com.viontech.keliu.websocket.AlgApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: 王洪波
* @Date: 2019/2/22.
*/
@Configuration
public class AlgApiClientConfiguration {
@Value("${ws.featureUrl:}")
private String featureUrl;
@Value("${ws.url:}")
private String compareUrl;
@Bean("algApiClientFeature")
@ConditionalOnProperty(name = "ws.featureUrl")
public AlgApiClient algApiClientFeatureConfig() {
return new AlgApiClient(featureUrl);
}
@Bean("algApiClient")
@ConditionalOnProperty(name = "ws.url")
public AlgApiClient algApiClient() {
return new AlgApiClient(compareUrl);
}
}
package com.xmh.es.match.controller;
import com.xmh.es.match.entity.vo.RequestData;
import com.xmh.es.match.service.MatchService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* .
*
* @author 谢明辉
* @date 2020/11/23
*/
@RestController("/alg")
public class MainController {
@Resource
private MatchService matchService;
@PostMapping("/match")
public Object match(RequestData requestData) throws Exception {
return matchService.match(requestData);
}
}
package com.xmh.es.match.entity;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Data {
private String type;
private Double[] data;
}
package com.xmh.es.match.entity;
import com.viontech.keliu.base.BaseModel;
import com.viontech.keliu.util.DateUtil;
import java.util.Date;
public class FaceRecognition extends BaseModel {
private Long id;
private Long deviceId;
private Long channelId;
private Long gateId;
private String deviceSerialnum;
private String channelSerialnum;
private Short personType;
private String facePic;
private Short facePicNum;
private Short bodyPicNum;
private String bodyPic;
private String showbodyPic;
private Short mood;
private Short age;
private Short gender;
private Short direction;
private Date counttime;
private Date countdate;
private Date modifyTime;
private Date createTime;
private Long mallId;
private Long accountId;
private String personUnid;
private Short status;
private Short faceFeatureType;
private Short bodyFeatureType;
private String trackInfo;
private Integer trackTime;
private Short happyConf;
/** 历史到店次数 */
private Short historyArrivalCount;
/** 今日到店次数 */
private Short todayArrivalCount;
private String unid;
/** 图像质量分 **/
private Float faceScore;
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getChannelId() {
return channelId;
}
public void setChannelId(Long channelId) {
this.channelId = channelId;
}
public Long getGateId() {
return gateId;
}
public void setGateId(Long gateId) {
this.gateId = gateId;
}
public String getDeviceSerialnum() {
return deviceSerialnum;
}
public void setDeviceSerialnum(String deviceSerialnum) {
this.deviceSerialnum = deviceSerialnum;
}
public String getChannelSerialnum() {
return channelSerialnum;
}
public void setChannelSerialnum(String channelSerialnum) {
this.channelSerialnum = channelSerialnum;
}
public Short getPersonType() {
return personType;
}
public void setPersonType(Short personType) {
this.personType = personType;
}
public String getFacePic() {
if (facePic == null) {
facePic = unid + "-" + DateUtil.format("yyyyMMddHHmmss", counttime) + "-face-0.jpg";
}
return facePic;
}
public void setFacePic(String facePic) {
this.facePic = facePic;
}
public String getBodyPic() {
if (bodyPic == null) {
bodyPic = unid + "-" + DateUtil.format("yyyyMMddHHmmss", counttime) + "-body-0.jpg";
}
return bodyPic;
}
public void setBodyPic(String bodyPic) {
this.bodyPic = bodyPic;
}
public String getShowbodyPic() {
return showbodyPic;
}
public void setShowbodyPic(String showbodyPic) {
this.showbodyPic = showbodyPic;
}
public Short getMood() {
return mood;
}
public void setMood(Short mood) {
this.mood = mood;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Short getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public Short getDirection() {
return direction;
}
public void setDirection(Short direction) {
this.direction = direction;
}
public Date getCounttime() {
return counttime;
}
public void setCounttime(Date counttime) {
this.counttime = counttime;
}
public Date getCountdate() {
return countdate;
}
public void setCountdate(Date countdate) {
this.countdate = countdate;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Long getMallId() {
return mallId;
}
public void setMallId(Long mallId) {
this.mallId = mallId;
}
public Long getAccountId() {
return accountId;
}
public void setAccountId(Long accountId) {
this.accountId = accountId;
}
public String getPersonUnid() {
return personUnid;
}
public void setPersonUnid(String personUnid) {
this.personUnid = personUnid;
}
public Short getStatus() {
return status;
}
public void setStatus(Short status) {
this.status = status;
}
public String getTrackInfo() {
return trackInfo;
}
public void setTrackInfo(String trackInfo) {
this.trackInfo = trackInfo;
}
public Integer getTrackTime() {
return trackTime;
}
public void setTrackTime(Integer trackTime) {
this.trackTime = trackTime;
}
public Short getHappyConf() {
return happyConf;
}
public void setHappyConf(Short happyConf) {
this.happyConf = happyConf;
}
public Short getHistoryArrivalCount() {
return historyArrivalCount;
}
public void setHistoryArrivalCount(Short historyArrivalCount) {
this.historyArrivalCount = historyArrivalCount;
}
public Short getTodayArrivalCount() {
return todayArrivalCount;
}
public void setTodayArrivalCount(Short todayArrivalCount) {
this.todayArrivalCount = todayArrivalCount;
}
public String getUnid() {
return unid;
}
public void setUnid(String unid) {
this.unid = unid;
}
public Float getFaceScore() {
return faceScore;
}
public void setFaceScore(Float faceScore) {
this.faceScore = faceScore;
}
public Short getFacePicNum() {
return facePicNum;
}
public void setFacePicNum(Short facePicNum) {
this.facePicNum = facePicNum;
}
public Short getBodyPicNum() {
return bodyPicNum;
}
public void setBodyPicNum(Short bodyPicNum) {
this.bodyPicNum = bodyPicNum;
}
public Short getFaceFeatureType() {
return faceFeatureType;
}
public void setFaceFeatureType(Short faceFeatureType) {
this.faceFeatureType = faceFeatureType;
}
public Short getBodyFeatureType() {
return bodyFeatureType;
}
public void setBodyFeatureType(Short bodyFeatureType) {
this.bodyFeatureType = bodyFeatureType;
}
}
\ No newline at end of file \ No newline at end of file
package com.xmh.es.match.entity;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class Feature {
private String filename;
private String type;
private List<Data> datas;
private Float face_score;
private Integer face_type;
}
package com.xmh.es.match.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* .
*
* @author 谢明辉
* @date 2020/11/20
*/
@Getter
@Setter
@Accessors(chain = true)
public class MatchResult {
private String personId;
private String poolId;
private float score;
}
package com.xmh.es.match.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* .
*
* @author 谢明辉
* @date 2020/11/23
*/
@Getter
@Setter
@Accessors(chain = true)
public class Person {
private Double[] feature;
private String personId;
}
package com.xmh.es.match.entity.vo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;
/**
* .
*
* @author 谢明辉
* @date 2020/11/23
*/
@Getter
@Setter
public class RequestData {
private MultipartFile file;
private String poolId;
}
package com.xmh.es.match.repository;
import com.xmh.es.match.entity.FaceRecognition;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* .
*
* @author 谢明辉
* @date 2020/11/20
*/
@Repository
public class FaceRecognitionRepository {
private static final String QUERY_FACE = "select * from d_face_recognition where countdate=? and mall_id=? and direction=1 and gender>-1 and face_score>0.7";
@Resource
private JdbcTemplate jdbcTemplate;
public List<FaceRecognition> queryFaceRecognitions(Date date, Long mallId) {
return jdbcTemplate.query(QUERY_FACE, new BeanPropertyRowMapper<>(FaceRecognition.class), date, mallId);
}
}
package com.xmh.es.match.service;
import com.xmh.es.match.entity.MatchResult;
import com.xmh.es.match.entity.Person;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.ScriptScoreQueryBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* .
*
* @author 谢明辉
* @date 2020/11/20
*/
@Service
public class AlgService {
@Resource
private RestHighLevelClient client;
/**
* 建立特征库索引
*
* @param poolName 特征库名称,作为index
*/
public Object createPool(String poolName) throws IOException {
CreateIndexRequest test2 = new CreateIndexRequest(poolName);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("data");
{
builder.field("type", "dense_vector");
builder.field("dims", 512);
}
builder.endObject();
builder.startObject("personId");
{
builder.field("type", "keyword");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
test2.mapping(builder);
CreateIndexResponse createIndexResponse = client.indices().create(test2, RequestOptions.DEFAULT);
if (createIndexResponse.isAcknowledged()) {
return "success";
} else {
return "failed";
}
}
/**
* 删除特征库
*
* @param poolName 特征库名称,index
*/
public Object deletePool(String poolName) throws IOException {
boolean exists = client.indices().exists(new GetIndexRequest(poolName), RequestOptions.DEFAULT);
if (!exists) {
return "特征池不存在";
}
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(poolName);
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
if (delete.isAcknowledged()) {
return "success";
} else {
return "failed";
}
}
/**
* 查询特征库列表
*/
public String[] queryPoolList() throws IOException {
GetIndexResponse response = client.indices().get(new GetIndexRequest("*pool*"), RequestOptions.DEFAULT);
return response.getIndices();
}
/**
* 人员比对
*
* @param person 需要比对的人员
* @param poolId 用来比对的特征库
*/
public List<MatchResult> matchPerson(Person person, String poolId) throws Exception {
Double[] feature = person.getFeature();
if (feature.length < 512) {
throw new Exception("特征维数为:" + feature.length + ",小于512维");
}
SearchRequest searchRequest = new SearchRequest(poolId);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.fetchSource("personId", null);
builder.size(5);
Map<String, Object> params = new HashMap<>(1);
params.put("data", feature);
Script script = new Script(
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
"cosineSimilarity(params.data, 'data')", params);
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.matchAllQuery(), script);
builder.query(queryBuilder);
builder.fetchSource("personId", null);
SearchRequest source = searchRequest.source(builder);
SearchResponse search = client.search(source, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
SearchHit[] hits1 = hits.getHits();
ArrayList<MatchResult> matchResults = new ArrayList<>();
for (SearchHit item : hits1) {
Map<String, Object> sourceAsMap = item.getSourceAsMap();
String personId = (String) sourceAsMap.get("personId");
float score = item.getScore();
String index = item.getIndex();
MatchResult matchResult = new MatchResult().setPersonId(personId).setScore(score).setPoolId(index);
matchResults.add(matchResult);
}
return matchResults;
}
/**
* 添加人员
*
* @param person 需要添加的人员
* @param poolId 特征库名称
*/
public Object addPerson(Person person, String poolId) throws IOException {
IndexRequest indexRequest = new IndexRequest(poolId);
indexRequest.id(person.getPersonId());
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
xContentBuilder.startObject();
xContentBuilder.field("personId", person.getPersonId());
xContentBuilder.field("data", person.getFeature());
xContentBuilder.endObject();
indexRequest.source(xContentBuilder);
return client.index(indexRequest, RequestOptions.DEFAULT);
}
/**
* 删除人员
*
* @param personId 需要删除的人员的id
* @param poolId 特征池名称
*/
public Object delPerson(String personId, String poolId) throws Exception {
DeleteRequest deleteRequest = new DeleteRequest(poolId, personId);
return client.delete(deleteRequest, RequestOptions.DEFAULT);
}
}
package com.xmh.es.match.service;
import com.viontech.keliu.i18n.util.LocalMessageUtil;
import com.viontech.keliu.websocket.AlgApiClient;
import com.xmh.es.match.entity.MatchResult;
import com.xmh.es.match.entity.Person;
import com.xmh.es.match.entity.vo.RequestData;
import com.xmh.es.match.util.Utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* .
*
* @author 谢明辉
* @date 2020/11/23
*/
@Service
public class MatchService {
@Resource
private AlgApiClient algApiClientFeature;
@Resource
private AlgService algService;
public List<MatchResult> match(RequestData requestData) throws Exception {
MultipartFile file = requestData.getFile();
BufferedImage originalImage = ImageIO.read(file.getInputStream());
BufferedImage jpgImage = Utils.convertImg2Jpg(originalImage);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(jpgImage, "jpg", out);
String base64 = Base64.getEncoder().encodeToString(out.toByteArray());
Map<String, Object> options = new HashMap<>();
options.put("vendor", "vion");
options.put("needFaceCheck", 1);
options.put("vendor", "vion");
CompletableFuture<JSONObject> responseFuture = algApiClientFeature.getFeatureAndAttr(base64,
AlgApiClient.IMAGE_TYPE_FACE, AlgApiClient.IMAGE_FORMAT_JPG, null, options);
JSONObject jsonObject = responseFuture.get(120, TimeUnit.SECONDS);
int success = jsonObject.getInt("success");
if (0 == success) {
throw new RuntimeException(jsonObject.getString("description"));
}
JSONObject faceFeatureJson = jsonObject.getJSONObject("faceFeature");
String message = LocalMessageUtil.getMessage("Message.picError");
if (faceFeatureJson == null) {
throw new Exception("无法提取特征");
}
JSONArray feature = faceFeatureJson.getJSONArray("feature");
List<Object> objects = feature.toList();
Double[] data = objects.toArray(new Double[]{});
Person person = new Person().setFeature(data);
return algService.matchPerson(person, requestData.getPoolId());
}
}
package com.xmh.es.match.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.viontech.keliu.model.Person;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
/**
* .
*
* @author 谢明辉
* @date 2020/11/19
*/
@Component
public class AlgUtil {
@Resource
private RestTemplateBuilder restTemplateBuilder;
public Object matchPerson(String poolId, Person person) {
RestTemplate restTemplate = restTemplateBuilder.build();
MatchPersonRequestBody matchPersonRequestBody = new MatchPersonRequestBody(Arrays.asList(person.getFaceFeatures().get(0).getFeature()));
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://127.0.0.1:9200/" + poolId.toLowerCase() + "/_search", matchPersonRequestBody, String.class);
return responseEntity.getBody();
}
public Object addPerson(String poolId, Person person) throws JsonProcessingException {
RestTemplate restTemplate = restTemplateBuilder.build();
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
HashMap<String, Object> req = new HashMap<>();
req.put("personId", person.getPersonId());
req.put("data", person.getFaceFeatures().get(0).getFeature());
HttpEntity<HashMap> httpEntity = new HttpEntity<>(req, headers);
ResponseEntity<String> response = restTemplate.exchange("http://127.0.0.1:9200/" + poolId.toLowerCase() + "/_doc/" + person.getPersonId(), HttpMethod.PUT, httpEntity, String.class);
return response.getBody();
}
public Object createPersonPool(String poolId) {
RestTemplate restTemplate = restTemplateBuilder.build();
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
HttpEntity<String> stringHttpEntity = new HttpEntity<>("{\"mappings\":{\"properties\":{\"data\":{\"type\":\"dense_vector\",\"dims\":512},\"personId\":{\"type\":\"keyword\"}}}}", headers);
ResponseEntity<HashMap> exchange = restTemplate.exchange("http://127.0.0.1:9200/" + poolId.toLowerCase(), HttpMethod.PUT, stringHttpEntity, HashMap.class);
HashMap body = exchange.getBody();
return body;
}
public Object deletePersonPool(String poolId) {
try {
RestTemplate restTemplate = restTemplateBuilder.build();
ResponseEntity<HashMap> exchange = restTemplate.exchange("http://127.0.0.1:9200/" + poolId.toLowerCase(), HttpMethod.DELETE, null, HashMap.class);
return exchange.getBody();
} catch (HttpClientErrorException e) {
int rawStatusCode = e.getRawStatusCode();
if (rawStatusCode != HttpStatus.NOT_FOUND.value()) {
return "failed";
} else {
return "success";
}
}
}
}
package com.xmh.es.match.test;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* .
*
* @author 谢明辉
* @date 2020/11/19
*/
public class MatchPersonRequestBody {
/**
* size : 5
* query : {"script_score":{"query":{"match_all":{}},"script":{"source":"cosineSimilarity(params.data, 'data') + 1.0","params":{"data":[0.068108,0.014368,0.091762,0.010903,0.01443,0.041033,-0.074874,0.046189,-0.02562,-0.055261,-0.005639,0.043233,-0.098272,-0.049989,-0.039348,0.010861,0.038638,-0.02885,0.028153,0.084619,-0.02077,0.008786,0.049098,0.045098,-0.014676,0.013116,0.036837,0.076818,0.0551,0.138394,0.043373,-0.014598,-0.00636,0.039918,-0.012339,-0.101808,-0.038001,0.039365,-0.049202,-0.035135,0.01485,-0.023692,0.057755,0.01204,-1.41E-4,-0.073259,-0.001348,0.009547,-0.007392,-0.054066,-0.021259,0.044755,-0.027385,0.021667,-0.023678,0.012275,0.009847,0.036068,0.009615,0.009852,-0.066564,0.033921,-0.026398,0.026663,-0.012794,0.043082,-0.018076,-0.00119,0.070574,0.03906,0.041317,-0.00347,0.020672,-0.080717,-0.013964,0.033847,0.020069,-0.007568,-0.033825,-0.102988,-0.051587,0.024974,0.029852,0.022228,0.055335,0.0428,-0.048463,0.025859,0.020033,-0.001027,-0.018752,-0.015112,-0.01754,0.053016,0.065319,-4.01E-4,-0.014958,0.018154,0.117003,-0.068761,-0.025001,0.018488,0.048777,0.057625,-0.105694,0.032833,-0.124412,-0.013078,0.01698,-0.046103,-0.088228,-9.98E-4,-0.013608,-0.036054,-0.004108,0.045773,-0.020305,-0.052917,0.020522,0.048404,-0.022486,0.023345,-0.028884,0.051395,-0.002595,-0.047451,-0.026204,-0.034879,-0.058463,0.021812,-0.026729,0.00715,-0.004115,-0.10578,0.077419,0.04775,0.031711,0.043292,0.041632,0.024481,0.116116,-0.077412,0.011272,0.013683,0.06864,0.03319,0.037092,-0.028257,0.078974,0.045608,0.035625,0.014927,0.021315,0.039978,-0.078317,0.00931,-0.033991,-0.026805,-0.040349,0.009949,0.125242,-0.008613,0.024056,0.057199,0.001171,0.004686,-0.042565,0.016594,0.023595,0.063368,0.017078,-0.014546,0.006337,0.00453,-0.01467,0.008986,0.032903,0.007178,0.04214,-0.007898,-0.019146,0.019857,-0.011497,0.028511,0.029444,0.033012,0.033782,0.029449,-0.044013,0.092618,-0.037648,-0.142108,-0.052529,-0.004662,-0.035764,-0.020602,0.024088,0.009376,0.075521,-0.007092,-0.098967,0.016986,-0.054567,0.07151,0.060147,-0.040985,0.012475,-0.033651,-0.052747,0.020169,0.041299,-0.107351,0.01141,0.026381,0.04032,-0.021364,-0.050897,0.004235,0.074893,0.002633,-0.038374,-0.028078,0.016358,-0.002606,0.028945,-0.007388,-0.028372,-0.04412,-0.051457,-0.008291,0.068626,-0.070521,-0.019987,0.054285,-0.020395,0.039718,-0.0204,-0.003545,0.053826,-0.027971,-0.038554,-0.004846,-0.058588,-0.025345,-0.01278,0.036282,-0.001409,0.080856,0.038562,0.021508,0.066544,0.064538,0.049733,-0.028369,0.060829,-0.019053,0.037779,-0.012294,-0.007169,0.04696,-0.004027,0.015136,-0.017986,-0.002829,0.035304,0.008131,0.049546,0.05303,0.017578,-0.020608,-0.004012,0.007314,0.00661,-0.088826,0.016745,-0.001807,0.024721,0.044723,-0.032498,-0.002926,0.050226,0.029758,-0.013113,-0.007432,0.032713,0.082661,0.016649,0.001712,-0.023498,0.083134,0.016716,0.063441,0.020973,-0.032077,-0.065,-0.020679,-0.106898,6.7E-5,0.08936,-0.047306,-0.005193,-0.012552,0.018224,0.021233,0.018646,-0.049475,0.007828,0.023464,0.013327,0.044293,0.030354,0.026781,-0.024248,-0.003235,-0.0522,-0.012828,-0.003599,-0.008353,-0.004334,0.067778,-0.057591,0.040552,-0.011844,0.04713,-0.011499,0.017168,0.031079,-0.068287,0.06261,0.02574,0.050446,-0.048067,-0.044463,0.022323,-0.013615,-0.054985,-0.019576,0.012493,0.037085,0.067325,-0.001564,-0.030091,0.03865,-0.009867,-0.008387,0.08873,0.039624,-0.076159,-0.030602,-0.007072,-0.02094,0.018184,-0.074487,0.065397,-0.002812,0.024861,0.083803,0.032194,0.040155,-0.025194,0.053609,-0.027503,0.045811,-0.06006,0.034613,0.0211,0.056577,0.001632,-3.22E-4,0.073088,0.063413,0.002621,-0.018644,-0.005028,-0.044343,0.062261,0.059549,0.030542,-0.056124,0.028872,-0.025748,-0.02019,0.009599,0.044675,0.03804,0.008471,0.022815,0.033123,0.075135,0.029521,-0.003475,-0.013425,-0.044886,-0.002728,0.022921,0.00168,-0.021697,-0.001536,0.053755,-0.009561,0.060567,0.001702,-0.089443,0.051019,-0.031763,-0.00312,0.0025,0.07553,0.053744,-0.02158,0.041417,0.050216,-0.038148,-0.027796,-0.041404,-0.031577,0.076185,0.032895,-0.017738,-0.012533,-0.039051,0.017185,0.066937,-0.031614,0.033627,-0.068471,-0.012564,0.020083,0.021593,0.036123,-0.038548,-0.085489,0.004702,-0.026352,0.020948,-0.01069,0.015037,-0.008545,-0.064654,0.014926,0.003012,0.045,-0.001665,0.027143,-0.049315,0.071771,-0.04606,-0.097489,-6.7E-4,0.055146,-0.012744,0.002475,-0.020666,0.006715,-0.027963,0.0547,-0.022611,-0.001958,-0.01883,0.051501,-0.007927,0.098186,-0.05518,0.001813,-0.003165,-0.001546,0.093926,0.049312,-0.040377,0.027179,-0.013377,-0.047165,-0.029489,0.032273,0.073167,0.001453,-0.045706,0.088015,-0.079898,-0.076594,-0.003219,-0.033113,-0.037722,0.060423,-0.013939,-0.021772,-0.019925,0.029442,-0.076246,-0.014815,0.053427,-0.052024,0.016712,0.065146,-0.015647,0.024286,0.007779,-0.08242,0.107829,0.066978,0.037417,0.065988,-0.028896,0.042163,-0.005107,-0.017907,-0.009163,-0.040879,-0.023788,0.013808,0.038175,-0.124999]}}}}
*/
private int size;
private QueryBeanX query;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public QueryBeanX getQuery() {
return query;
}
public void setQuery(QueryBeanX query) {
this.query = query;
}
public MatchPersonRequestBody(List<Double> data) {
this.size = 5;
this.query = new QueryBeanX();
QueryBeanX.ScriptScoreBean scriptScoreBean = new QueryBeanX.ScriptScoreBean();
this.query.setScript_score(scriptScoreBean);
scriptScoreBean.setQuery(Collections.singletonMap("match_all", Collections.emptyMap()));
QueryBeanX.ScriptScoreBean.ScriptBean scriptBean = new QueryBeanX.ScriptScoreBean.ScriptBean();
scriptScoreBean.setScript(scriptBean);
QueryBeanX.ScriptScoreBean.ScriptBean.ParamsBean paramsBean = new QueryBeanX.ScriptScoreBean.ScriptBean.ParamsBean();
paramsBean.setData(data);
scriptBean.setParams(paramsBean);
}
public static class QueryBeanX {
private ScriptScoreBean script_score;
public ScriptScoreBean getScript_score() {
return script_score;
}
public void setScript_score(ScriptScoreBean script_score) {
this.script_score = script_score;
}
public static class ScriptScoreBean {
/**
* query : {"match_all":{}}
* script : {"source":"cosineSimilarity(params.data, 'data') + 1.0","params":{"data":[0.068108,0.014368,0.091762,0.010903,0.01443,0.041033,-0.074874,0.046189,-0.02562,-0.055261,-0.005639,0.043233,-0.098272,-0.049989,-0.039348,0.010861,0.038638,-0.02885,0.028153,0.084619,-0.02077,0.008786,0.049098,0.045098,-0.014676,0.013116,0.036837,0.076818,0.0551,0.138394,0.043373,-0.014598,-0.00636,0.039918,-0.012339,-0.101808,-0.038001,0.039365,-0.049202,-0.035135,0.01485,-0.023692,0.057755,0.01204,-1.41E-4,-0.073259,-0.001348,0.009547,-0.007392,-0.054066,-0.021259,0.044755,-0.027385,0.021667,-0.023678,0.012275,0.009847,0.036068,0.009615,0.009852,-0.066564,0.033921,-0.026398,0.026663,-0.012794,0.043082,-0.018076,-0.00119,0.070574,0.03906,0.041317,-0.00347,0.020672,-0.080717,-0.013964,0.033847,0.020069,-0.007568,-0.033825,-0.102988,-0.051587,0.024974,0.029852,0.022228,0.055335,0.0428,-0.048463,0.025859,0.020033,-0.001027,-0.018752,-0.015112,-0.01754,0.053016,0.065319,-4.01E-4,-0.014958,0.018154,0.117003,-0.068761,-0.025001,0.018488,0.048777,0.057625,-0.105694,0.032833,-0.124412,-0.013078,0.01698,-0.046103,-0.088228,-9.98E-4,-0.013608,-0.036054,-0.004108,0.045773,-0.020305,-0.052917,0.020522,0.048404,-0.022486,0.023345,-0.028884,0.051395,-0.002595,-0.047451,-0.026204,-0.034879,-0.058463,0.021812,-0.026729,0.00715,-0.004115,-0.10578,0.077419,0.04775,0.031711,0.043292,0.041632,0.024481,0.116116,-0.077412,0.011272,0.013683,0.06864,0.03319,0.037092,-0.028257,0.078974,0.045608,0.035625,0.014927,0.021315,0.039978,-0.078317,0.00931,-0.033991,-0.026805,-0.040349,0.009949,0.125242,-0.008613,0.024056,0.057199,0.001171,0.004686,-0.042565,0.016594,0.023595,0.063368,0.017078,-0.014546,0.006337,0.00453,-0.01467,0.008986,0.032903,0.007178,0.04214,-0.007898,-0.019146,0.019857,-0.011497,0.028511,0.029444,0.033012,0.033782,0.029449,-0.044013,0.092618,-0.037648,-0.142108,-0.052529,-0.004662,-0.035764,-0.020602,0.024088,0.009376,0.075521,-0.007092,-0.098967,0.016986,-0.054567,0.07151,0.060147,-0.040985,0.012475,-0.033651,-0.052747,0.020169,0.041299,-0.107351,0.01141,0.026381,0.04032,-0.021364,-0.050897,0.004235,0.074893,0.002633,-0.038374,-0.028078,0.016358,-0.002606,0.028945,-0.007388,-0.028372,-0.04412,-0.051457,-0.008291,0.068626,-0.070521,-0.019987,0.054285,-0.020395,0.039718,-0.0204,-0.003545,0.053826,-0.027971,-0.038554,-0.004846,-0.058588,-0.025345,-0.01278,0.036282,-0.001409,0.080856,0.038562,0.021508,0.066544,0.064538,0.049733,-0.028369,0.060829,-0.019053,0.037779,-0.012294,-0.007169,0.04696,-0.004027,0.015136,-0.017986,-0.002829,0.035304,0.008131,0.049546,0.05303,0.017578,-0.020608,-0.004012,0.007314,0.00661,-0.088826,0.016745,-0.001807,0.024721,0.044723,-0.032498,-0.002926,0.050226,0.029758,-0.013113,-0.007432,0.032713,0.082661,0.016649,0.001712,-0.023498,0.083134,0.016716,0.063441,0.020973,-0.032077,-0.065,-0.020679,-0.106898,6.7E-5,0.08936,-0.047306,-0.005193,-0.012552,0.018224,0.021233,0.018646,-0.049475,0.007828,0.023464,0.013327,0.044293,0.030354,0.026781,-0.024248,-0.003235,-0.0522,-0.012828,-0.003599,-0.008353,-0.004334,0.067778,-0.057591,0.040552,-0.011844,0.04713,-0.011499,0.017168,0.031079,-0.068287,0.06261,0.02574,0.050446,-0.048067,-0.044463,0.022323,-0.013615,-0.054985,-0.019576,0.012493,0.037085,0.067325,-0.001564,-0.030091,0.03865,-0.009867,-0.008387,0.08873,0.039624,-0.076159,-0.030602,-0.007072,-0.02094,0.018184,-0.074487,0.065397,-0.002812,0.024861,0.083803,0.032194,0.040155,-0.025194,0.053609,-0.027503,0.045811,-0.06006,0.034613,0.0211,0.056577,0.001632,-3.22E-4,0.073088,0.063413,0.002621,-0.018644,-0.005028,-0.044343,0.062261,0.059549,0.030542,-0.056124,0.028872,-0.025748,-0.02019,0.009599,0.044675,0.03804,0.008471,0.022815,0.033123,0.075135,0.029521,-0.003475,-0.013425,-0.044886,-0.002728,0.022921,0.00168,-0.021697,-0.001536,0.053755,-0.009561,0.060567,0.001702,-0.089443,0.051019,-0.031763,-0.00312,0.0025,0.07553,0.053744,-0.02158,0.041417,0.050216,-0.038148,-0.027796,-0.041404,-0.031577,0.076185,0.032895,-0.017738,-0.012533,-0.039051,0.017185,0.066937,-0.031614,0.033627,-0.068471,-0.012564,0.020083,0.021593,0.036123,-0.038548,-0.085489,0.004702,-0.026352,0.020948,-0.01069,0.015037,-0.008545,-0.064654,0.014926,0.003012,0.045,-0.001665,0.027143,-0.049315,0.071771,-0.04606,-0.097489,-6.7E-4,0.055146,-0.012744,0.002475,-0.020666,0.006715,-0.027963,0.0547,-0.022611,-0.001958,-0.01883,0.051501,-0.007927,0.098186,-0.05518,0.001813,-0.003165,-0.001546,0.093926,0.049312,-0.040377,0.027179,-0.013377,-0.047165,-0.029489,0.032273,0.073167,0.001453,-0.045706,0.088015,-0.079898,-0.076594,-0.003219,-0.033113,-0.037722,0.060423,-0.013939,-0.021772,-0.019925,0.029442,-0.076246,-0.014815,0.053427,-0.052024,0.016712,0.065146,-0.015647,0.024286,0.007779,-0.08242,0.107829,0.066978,0.037417,0.065988,-0.028896,0.042163,-0.005107,-0.017907,-0.009163,-0.040879,-0.023788,0.013808,0.038175,-0.124999]}}
*/
private Map query;
private ScriptBean script;
public Map getQuery() {
return query;
}
public void setQuery(Map query) {
this.query = query;
}
public ScriptBean getScript() {
return script;
}
public void setScript(ScriptBean script) {
this.script = script;
}
public static class ScriptBean {
/**
* source : cosineSimilarity(params.data, 'data') + 1.0
* params : {"data":[0.068108,0.014368,0.091762,0.010903,0.01443,0.041033,-0.074874,0.046189,-0.02562,-0.055261,-0.005639,0.043233,-0.098272,-0.049989,-0.039348,0.010861,0.038638,-0.02885,0.028153,0.084619,-0.02077,0.008786,0.049098,0.045098,-0.014676,0.013116,0.036837,0.076818,0.0551,0.138394,0.043373,-0.014598,-0.00636,0.039918,-0.012339,-0.101808,-0.038001,0.039365,-0.049202,-0.035135,0.01485,-0.023692,0.057755,0.01204,-1.41E-4,-0.073259,-0.001348,0.009547,-0.007392,-0.054066,-0.021259,0.044755,-0.027385,0.021667,-0.023678,0.012275,0.009847,0.036068,0.009615,0.009852,-0.066564,0.033921,-0.026398,0.026663,-0.012794,0.043082,-0.018076,-0.00119,0.070574,0.03906,0.041317,-0.00347,0.020672,-0.080717,-0.013964,0.033847,0.020069,-0.007568,-0.033825,-0.102988,-0.051587,0.024974,0.029852,0.022228,0.055335,0.0428,-0.048463,0.025859,0.020033,-0.001027,-0.018752,-0.015112,-0.01754,0.053016,0.065319,-4.01E-4,-0.014958,0.018154,0.117003,-0.068761,-0.025001,0.018488,0.048777,0.057625,-0.105694,0.032833,-0.124412,-0.013078,0.01698,-0.046103,-0.088228,-9.98E-4,-0.013608,-0.036054,-0.004108,0.045773,-0.020305,-0.052917,0.020522,0.048404,-0.022486,0.023345,-0.028884,0.051395,-0.002595,-0.047451,-0.026204,-0.034879,-0.058463,0.021812,-0.026729,0.00715,-0.004115,-0.10578,0.077419,0.04775,0.031711,0.043292,0.041632,0.024481,0.116116,-0.077412,0.011272,0.013683,0.06864,0.03319,0.037092,-0.028257,0.078974,0.045608,0.035625,0.014927,0.021315,0.039978,-0.078317,0.00931,-0.033991,-0.026805,-0.040349,0.009949,0.125242,-0.008613,0.024056,0.057199,0.001171,0.004686,-0.042565,0.016594,0.023595,0.063368,0.017078,-0.014546,0.006337,0.00453,-0.01467,0.008986,0.032903,0.007178,0.04214,-0.007898,-0.019146,0.019857,-0.011497,0.028511,0.029444,0.033012,0.033782,0.029449,-0.044013,0.092618,-0.037648,-0.142108,-0.052529,-0.004662,-0.035764,-0.020602,0.024088,0.009376,0.075521,-0.007092,-0.098967,0.016986,-0.054567,0.07151,0.060147,-0.040985,0.012475,-0.033651,-0.052747,0.020169,0.041299,-0.107351,0.01141,0.026381,0.04032,-0.021364,-0.050897,0.004235,0.074893,0.002633,-0.038374,-0.028078,0.016358,-0.002606,0.028945,-0.007388,-0.028372,-0.04412,-0.051457,-0.008291,0.068626,-0.070521,-0.019987,0.054285,-0.020395,0.039718,-0.0204,-0.003545,0.053826,-0.027971,-0.038554,-0.004846,-0.058588,-0.025345,-0.01278,0.036282,-0.001409,0.080856,0.038562,0.021508,0.066544,0.064538,0.049733,-0.028369,0.060829,-0.019053,0.037779,-0.012294,-0.007169,0.04696,-0.004027,0.015136,-0.017986,-0.002829,0.035304,0.008131,0.049546,0.05303,0.017578,-0.020608,-0.004012,0.007314,0.00661,-0.088826,0.016745,-0.001807,0.024721,0.044723,-0.032498,-0.002926,0.050226,0.029758,-0.013113,-0.007432,0.032713,0.082661,0.016649,0.001712,-0.023498,0.083134,0.016716,0.063441,0.020973,-0.032077,-0.065,-0.020679,-0.106898,6.7E-5,0.08936,-0.047306,-0.005193,-0.012552,0.018224,0.021233,0.018646,-0.049475,0.007828,0.023464,0.013327,0.044293,0.030354,0.026781,-0.024248,-0.003235,-0.0522,-0.012828,-0.003599,-0.008353,-0.004334,0.067778,-0.057591,0.040552,-0.011844,0.04713,-0.011499,0.017168,0.031079,-0.068287,0.06261,0.02574,0.050446,-0.048067,-0.044463,0.022323,-0.013615,-0.054985,-0.019576,0.012493,0.037085,0.067325,-0.001564,-0.030091,0.03865,-0.009867,-0.008387,0.08873,0.039624,-0.076159,-0.030602,-0.007072,-0.02094,0.018184,-0.074487,0.065397,-0.002812,0.024861,0.083803,0.032194,0.040155,-0.025194,0.053609,-0.027503,0.045811,-0.06006,0.034613,0.0211,0.056577,0.001632,-3.22E-4,0.073088,0.063413,0.002621,-0.018644,-0.005028,-0.044343,0.062261,0.059549,0.030542,-0.056124,0.028872,-0.025748,-0.02019,0.009599,0.044675,0.03804,0.008471,0.022815,0.033123,0.075135,0.029521,-0.003475,-0.013425,-0.044886,-0.002728,0.022921,0.00168,-0.021697,-0.001536,0.053755,-0.009561,0.060567,0.001702,-0.089443,0.051019,-0.031763,-0.00312,0.0025,0.07553,0.053744,-0.02158,0.041417,0.050216,-0.038148,-0.027796,-0.041404,-0.031577,0.076185,0.032895,-0.017738,-0.012533,-0.039051,0.017185,0.066937,-0.031614,0.033627,-0.068471,-0.012564,0.020083,0.021593,0.036123,-0.038548,-0.085489,0.004702,-0.026352,0.020948,-0.01069,0.015037,-0.008545,-0.064654,0.014926,0.003012,0.045,-0.001665,0.027143,-0.049315,0.071771,-0.04606,-0.097489,-6.7E-4,0.055146,-0.012744,0.002475,-0.020666,0.006715,-0.027963,0.0547,-0.022611,-0.001958,-0.01883,0.051501,-0.007927,0.098186,-0.05518,0.001813,-0.003165,-0.001546,0.093926,0.049312,-0.040377,0.027179,-0.013377,-0.047165,-0.029489,0.032273,0.073167,0.001453,-0.045706,0.088015,-0.079898,-0.076594,-0.003219,-0.033113,-0.037722,0.060423,-0.013939,-0.021772,-0.019925,0.029442,-0.076246,-0.014815,0.053427,-0.052024,0.016712,0.065146,-0.015647,0.024286,0.007779,-0.08242,0.107829,0.066978,0.037417,0.065988,-0.028896,0.042163,-0.005107,-0.017907,-0.009163,-0.040879,-0.023788,0.013808,0.038175,-0.124999]}
*/
private String source = "cosineSimilarity(params.data, 'data') + 1.0";
private ParamsBean params;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public ParamsBean getParams() {
return params;
}
public void setParams(ParamsBean params) {
this.params = params;
}
public static class ParamsBean {
private List<Double> data;
public List<Double> getData() {
return data;
}
public void setData(List<Double> data) {
this.data = data;
}
}
}
}
}
}
package com.xmh.es.match.util;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* .
*
* @author 谢明辉
* @date 2020/11/23
*/
public class Utils {
public static BufferedImage convertImg2Jpg(BufferedImage image) {
BufferedImage newBufferedImage;
//防止图片变色
int width = image.getWidth();
int height = image.getHeight();
if (width > 600 || height > 600) {
newBufferedImage = new BufferedImage(600, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = newBufferedImage.createGraphics();
graphics.drawImage(image, 0, 0, 600, 600, Color.WHITE, null);
} else {
newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = newBufferedImage.createGraphics();
graphics.drawImage(image, 0, 0, Color.WHITE, null);
}
return newBufferedImage;
}
}
# database
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://pgm-2ze197c18ro6p1r1fo.pg.rds.aliyuncs.com:3433/ShoppingMall_retail2.0
spring.datasource.username=vion
spring.datasource.password=cdmqYwBq9uAdvLJb
# oss
oss.config.endPoint=oss-cn-beijing.aliyuncs.com
oss.config.accessKeyId=LTAI9WfUr3GDne4c
oss.config.accessKeySecret=SDzMs26kNyiDIIRJIY2qUBxBOeUEZ8
oss.config.bucket=vion-retail
# ws
ws.featureUrl=http://101.200.130.13:10000
\ No newline at end of file \ No newline at end of file
spring:
elasticsearch:
rest:
uris: http://127.0.0.1:9200
profiles:
active: option
package com.xmh.es.match;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.keliu.storage.Storage;
import com.viontech.keliu.util.DateUtil;
import com.xmh.es.match.entity.*;
import com.xmh.es.match.repository.FaceRecognitionRepository;
import com.xmh.es.match.service.AlgService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* .
*
* @author 谢明辉
* @date 2020/11/20
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class Test0 {
@Resource
ObjectMapper objectMapper;
@Resource
private AlgService algService;
@Resource
private FaceRecognitionRepository faceRecognitionRepository;
@Resource
private Storage featureStorage;
@Test
public void createPoolTest() throws IOException {
Object test2 = algService.createPool(XmhTestApplication.POOL_NAME);
System.out.println(test2);
}
@Test
public void deleteIndexTest() throws IOException {
algService.deletePool(XmhTestApplication.POOL_NAME);
}
@Test
public void queryIndex() throws IOException {
String[] strings = algService.queryPoolList();
for (String string : strings) {
System.out.println(string);
}
}
@Test
public void addPersonTest() throws IOException {
Person person1 = getPerson("3");
Object test2 = algService.addPerson(person1, XmhTestApplication.POOL_NAME);
System.out.println(objectMapper.writeValueAsString(test2));
}
@Test
public void matchPersonTest() throws Exception {
Person person = getPerson("3");
List<MatchResult> test2 = algService.matchPerson(person, XmhTestApplication.POOL_NAME);
System.out.println(objectMapper.writeValueAsString(test2));
}
@Test
public void batchAdd() throws ParseException {
Date start = DateUtil.parse(DateUtil.FORMAT_SHORT, "2020-10-01");
Date end = DateUtil.parse(DateUtil.FORMAT_SHORT, "2020-11-19");
List<Date> days = DateUtil.getDaysBetweenDates(start, end);
for (Date day : days) {
List<FaceRecognition> faceRecognitions = faceRecognitionRepository.queryFaceRecognitions(day, 2L);
for (FaceRecognition faceRecognition : faceRecognitions) {
try {
Double[] feature = getFeature(faceRecognition);
if (feature == null) {
continue;
}
Person person = new Person();
person.setPersonId(faceRecognition.getUnid());
person.setFeature(feature);
Object o = algService.addPerson(person, XmhTestApplication.POOL_NAME);
System.out.println(objectMapper.writeValueAsString(o));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private Double[] getFeature(FaceRecognition face) throws JsonProcessingException {
Short faceFeatureType = face.getFaceFeatureType();
String facePic = face.getFacePic();
if (faceFeatureType == 1) {
facePic = facePic.replace("face-0.jpg", "face-F.jpg");
}
String json = (String) featureStorage.getItem(face.getChannelSerialnum() + "/" + facePic);
Feature feature = objectMapper.readValue(json, Feature.class);
List<Data> datas = feature.getDatas();
if (datas != null && datas.size() > 0) {
Data data = datas.get(0);
Double[] data1 = data.getData();
if (data1.length == 512) {
return data1;
}
}
return null;
}
public Person getPerson(String id) throws IOException {
Path path = Paths.get("C:\\Users\\vion\\Desktop\\" + id + ".feature");
String featureStr = Files.readAllLines(path).get(0);
Feature feature = objectMapper.readValue(featureStr, Feature.class);
Double[] data = feature.getDatas().get(0).getData();
Person person = new Person();
person.setPersonId(UUID.randomUUID().toString());
person.setFeature(data);
return person;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!