Commit 877b932f by xmh

初版

1 parent 4f063055
...@@ -29,3 +29,4 @@ build/ ...@@ -29,3 +29,4 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
/logs/
...@@ -8,12 +8,11 @@ ...@@ -8,12 +8,11 @@
<version>2.3.1.RELEASE</version> <version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.xmh.es</groupId> <groupId>com.viontech</groupId>
<artifactId>xmh-test</artifactId> <artifactId>VVAS-Match</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>xmh-test</name> <name>VVAS-Match</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
...@@ -26,11 +25,6 @@ ...@@ -26,11 +25,6 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
...@@ -39,25 +33,19 @@ ...@@ -39,25 +33,19 @@
<groupId>com.viontech.keliu</groupId> <groupId>com.viontech.keliu</groupId>
<artifactId>keliu-base</artifactId> <artifactId>keliu-base</artifactId>
<version>6.0.8-SNAPSHOT</version> <version>6.0.8-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<groupId>com.github.pagehelper</groupId>
</exclusion>
</exclusions>
</dependency> </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> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
...@@ -89,7 +77,7 @@ ...@@ -89,7 +77,7 @@
</dependencies> </dependencies>
<build> <build>
<finalName>es-match</finalName> <finalName>VVAS-Match</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
package com.viontech.match;
/**
* .
*
* @author 谢明辉
* @date 2020/11/25
*/
public enum PoolEnum {
/** */
POOL_2K("test_pool_2k", 2000),
POOL_5K("test_pool_5k", 5000),
POOL_2W("test_pool_2w", 20000),
POOL_10W("test_pool_10w", 100000),
POOL_50W("test_pool_50w", 500000),
POOL_100W("test_pool_100w", 1000000),
POOL_200W("test_pool_200w", 2000000);
public String name;
public int num;
PoolEnum(String name, int num) {
this.name = name;
this.num = num;
}
}
...@@ -5,8 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -5,8 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication @SpringBootApplication
@ComponentScan(basePackages = {"com.viontech"}) @ComponentScan(basePackages = {"com.viontech"})public class XmhTestApplication {
public class XmhTestApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(XmhTestApplication.class, args); SpringApplication.run(XmhTestApplication.class, args);
......
package com.viontech.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.viontech.match.controller; package com.viontech.match.controller;
import com.fasterxml.jackson.core.JsonProcessingException; import com.viontech.match.entity.vo.RequestVo;
import com.fasterxml.jackson.databind.ObjectMapper; import com.viontech.match.entity.vo.ResponseVo;
import com.viontech.keliu.storage.Storage; import com.viontech.match.service.PersonService;
import com.viontech.keliu.util.DateUtil; import com.viontech.match.service.PoolService;
import com.viontech.match.PoolEnum; import lombok.extern.slf4j.Slf4j;
import com.viontech.match.entity.*; import org.springframework.web.bind.annotation.PostMapping;
import com.viontech.match.entity.vo.RequestData; import org.springframework.web.bind.annotation.RequestBody;
import com.viontech.match.repository.FaceRecognitionRepository; import org.springframework.web.bind.annotation.RequestMapping;
import com.viontech.match.service.AlgService; import org.springframework.web.bind.annotation.RestController;
import com.viontech.match.service.MatchService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/** /**
* . * .
...@@ -30,154 +20,37 @@ import java.util.stream.Collectors; ...@@ -30,154 +20,37 @@ import java.util.stream.Collectors;
*/ */
@RestController @RestController
@RequestMapping("/alg") @RequestMapping("/alg")
@Slf4j
public class MainController { public class MainController {
private final ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(20, 30, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
private final AtomicInteger integer = new AtomicInteger();
@Resource @Resource
ObjectMapper objectMapper; private PersonService personService;
@Resource @Resource
private MatchService matchService; private PoolService poolService;
@Resource
private FaceRecognitionRepository faceRecognitionRepository; @PostMapping
@Resource public ResponseVo all(@RequestBody RequestVo requestVo) throws Exception {
private AlgService algService; String rid = requestVo.getRid();
@Resource try{
private Storage featureStorage; switch (requestVo.getCommand()) {
case AddPersonPool:
@PostMapping("/match") return poolService.createPool(requestVo);
public Object match(RequestData requestData) throws Exception { case DelPersonPool:
MultipartFile file = requestData.getFile(); return poolService.deletePool(requestVo);
Double[] data = matchService.extractFeature(file); case ModifyPersonPool:
Person person = new Person().setFeature(data); return poolService.modifyPool(requestVo);
long begin = System.currentTimeMillis(); case QueryPersonPool:
List<MatchResult> match = algService.matchPerson(person, requestData.getPoolId()); return poolService.queryPool(requestVo);
long timeConsumingMatching = System.currentTimeMillis() - begin; case MatchPerson:
List<String> collect = match.stream().map(MatchResult::getPersonId).collect(Collectors.toList()); return personService.matchPerson(requestVo);
List<FaceRecognition> faceRecognitions = faceRecognitionRepository.queryFaceRecognitionByUnids(collect); case UpdatePerson:
if (faceRecognitions == null) { return personService.updatePerson(requestVo);
return null; default:
} return ResponseVo.commandNotFound(rid);
Map<String, FaceRecognition> map = faceRecognitions.stream().collect(Collectors.toMap(FaceRecognition::getUnid, x -> x, (x, y) -> y));
for (MatchResult item : match) {
FaceRecognition faceRecognition = map.get(item.getPersonId());
if (faceRecognition != null) {
faceRecognition.setMatchScore(item.getScore());
}
}
Collection<FaceRecognition> values = map.values();
List<FaceRecognition> collect1 = values.stream().sorted((o1, o2) -> {
if (o1.getMatchScore() > o2.getMatchScore()) {
return -1;
} else if (o1.getMatchScore().equals(o2.getMatchScore())) {
return 0;
} else {
return 1;
}
}).collect(Collectors.toList());
HashMap<String, Object> resultMap = new HashMap<>(2);
resultMap.put("time", timeConsumingMatching);
resultMap.put("result", collect1);
return resultMap;
}
@GetMapping("/poolIds")
public Object getPoolIds() throws IOException {
return algService.queryPoolList();
}
@GetMapping("/create/{num}")
public Object createPool(@PathVariable("num") Integer num) throws Exception {
PoolEnum pool;
switch (num) {
case 2000:
pool = PoolEnum.POOL_2K;
break;
case 5000:
pool = PoolEnum.POOL_5K;
break;
case 20000:
pool = PoolEnum.POOL_2W;
break;
case 100000:
pool = PoolEnum.POOL_10W;
break;
case 500000:
pool = PoolEnum.POOL_50W;
break;
case 1000000:
pool = PoolEnum.POOL_100W;
break;
case 2000000:
pool = PoolEnum.POOL_200W;
break;
default:
throw new RuntimeException("请输入[2000,5000,20000,100000,500000,1000000,2000000]");
}
algService.deletePool(pool.name);
algService.createPool(pool.name);
Date end = DateUtil.addDays(new Date(), -1);
Date start = DateUtil.addDays(end, -30);
integer.set(0);
List<Date> days = DateUtil.getDaysBetweenDates(start, end);
for (Date day : days) {
List<FaceRecognition> faceRecognitions = faceRecognitionRepository.queryFaceRecognitions(day, 2L);
for (FaceRecognition faceRecognition : faceRecognitions) {
poolExecutor.execute(() -> {
try {
if (integer.get() >= pool.num) {
return;
}
Double[] feature = getFeature(faceRecognition);
if (feature == null) {
return;
}
Person person = new Person();
person.setPersonId(faceRecognition.getPersonUnid());
person.setFeature(feature);
person.setId(faceRecognition.getUnid());
Object o = algService.addPerson(person, pool.name);
System.out.println(objectMapper.writeValueAsString(o));
integer.addAndGet(1);
} catch (Exception e) {
e.printStackTrace();
}
});
if (integer.get() > pool.num) {
return "success";
}
while (poolExecutor.getQueue().size() > 10000) {
System.out.println(poolExecutor.getQueue().size());
TimeUnit.SECONDS.sleep(4);
}
if (integer.get() > pool.num) {
return "success";
}
}
}
return "success";
}
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;
} }
}catch(Exception e){
log.error("",e);
return ResponseVo.error(rid, e.getMessage());
} }
return null;
} }
} }
package com.viontech.match.entity;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Data {
private String type;
private Double[] data;
}
package com.viontech.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 String facePath;
private String trackPath;
private Short happyConf;
/** 历史到店次数 */
private Short historyArrivalCount;
/** 今日到店次数 */
private Short todayArrivalCount;
private String unid;
/** 图像质量分 **/
private Float faceScore;
private Float matchScore;
@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;
}
private String getRootPath(String pathType) {
Date countdate = this.getCountdate();
if (countdate == null) {
return "获取轨迹根路径失败。【日期字段无法获取】";
}
String yyyyMMdd = DateUtil.format("yyyyMMdd", countdate);
String channelSerialnum = this.getChannelSerialnum();
if (channelSerialnum == null) {
return "获取轨迹根路径失败。【通道序列号字段无法获取】";
}
return pathType + "/" + yyyyMMdd + "/" + channelSerialnum + "/";
}
public String getFacePath() {
facePath = getRootPath("face");
return facePath;
}
public String getTrackPath() {
trackPath = getRootPath("track");
return trackPath;
}
public void setFacePath(String facePath) {
this.facePath = facePath;
}
public void setTrackPath(String trackPath) {
this.trackPath = trackPath;
}
public Float getMatchScore() {
return matchScore;
}
public void setMatchScore(Float matchScore) {
this.matchScore = matchScore;
}
}
\ No newline at end of file \ No newline at end of file
package com.viontech.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.viontech.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 String id;
private Double[] feature;
private String personId;
}
...@@ -2,21 +2,19 @@ package com.viontech.match.entity; ...@@ -2,21 +2,19 @@ package com.viontech.match.entity;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.experimental.Accessors;
/** /**
* . * .
* *
* @author 谢明辉 * @author 谢明辉
* @date 2020/11/20 * @date 2020/11/27
*/ */
@Getter @Getter
@Setter @Setter
@Accessors(chain = true) public class PoolInfo {
public class MatchResult {
private String personId;
private String poolId; private String poolId;
private float score; private Integer poolType;
private Long personCount;
} }
package com.viontech.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.viontech.match.entity.vo;
import com.viontech.keliu.model.Person;
import com.viontech.match.enumeration.CommandEnum;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.List;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
@Getter
@Setter
public class RequestVo {
/** 公用 */
private CommandEnum command;
private String rid;
private String poolId;
/** 增加修改特征池时使用 */
private Integer poolType;
private List<Person> personPool;
private Integer updateType;
/** 删除时使用 */
private List<HashMap<String, String>> personIds;
private Integer flushPool;
/** 查询时使用 */
private Integer listAll;
/** 人员匹配时用 */
private Integer personType;
private Person person;
private String personPoolId;
private List<String> unionPersonPoolId;
public void setPoolId(String poolId) {
this.poolId = poolId.toLowerCase();
}
public Integer getFlushPool() {
if (flushPool == null) {
flushPool = 1;
}
return flushPool;
}
public Integer getListAll() {
if (listAll == null) {
listAll = 1;
}
return listAll;
}
}
package com.viontech.match.entity.vo;
import com.viontech.keliu.model.Person;
import com.viontech.keliu.model.Pool;
import com.viontech.match.entity.PoolInfo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.List;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
@Getter
@Setter
@ToString
public class ResponseVo {
private Integer success;
private String description;
private String rid;
private Integer errCode;
private List<Pool> personPoolStatus;
private Integer oper;
private Integer index;
private Integer match;
private List<PoolInfo> poolIds;
private List<Person> matchPersons;
public ResponseVo(String rid) {
this.rid = rid;
}
public static ResponseVo commandNotFound(String rid) {
return error(rid, "command not found");
}
public static ResponseVo error(String rid, String description) {
return error(rid, 1, description);
}
public static ResponseVo error(String rid, Integer errCode, String description) {
ResponseVo responseVo = new ResponseVo(rid);
responseVo.setSuccess(0);
responseVo.setErrCode(errCode);
responseVo.setDescription(description);
return responseVo;
}
public static ResponseVo success(String rid) {
return success(rid, "success");
}
public static ResponseVo success(String rid, String description) {
ResponseVo responseVo = new ResponseVo(rid);
responseVo.setSuccess(1);
responseVo.setDescription(description);
responseVo.setErrCode(0);
return responseVo;
}
}
package com.viontech.match.enumeration;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
public enum CommandEnum {
/** 命令 */
AddPersonPool,
DelPersonPool,
ModifyPersonPool,
QueryPersonPool,
MatchPerson,
UpdatePerson
}
package com.viontech.match.repository;
import com.viontech.match.entity.FaceRecognition;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* .
*
* @author 谢明辉
* @date 2020/11/20
*/
@Repository
public class FaceRecognitionRepository {
private static final BeanPropertyRowMapper<FaceRecognition> FACE_RECOGNITION_ROW_MAPPER = new BeanPropertyRowMapper<>(FaceRecognition.class);
private static final String QUERY_FACE = "select unid,person_unid,face_feature_type,face_pic,channel_serialnum from d_face_recognition where countdate=? and direction=1 and gender>-1 and face_score>0.7";
private static final String QUERY_FACE_BY_UNID = "select face_pic,id,channel_serialnum,device_serialnum,person_unid,unid,id,countdate,counttime,age,gender,direction,mall_id,account_id,gate_id,face_score,person_type from d_face_recognition where unid in (:unids)";
@Resource
private JdbcTemplate jdbcTemplate;
@Resource
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public List<FaceRecognition> queryFaceRecognitions(Date date, Long mallId) {
return jdbcTemplate.query(QUERY_FACE, FACE_RECOGNITION_ROW_MAPPER, date);
}
public List<FaceRecognition> queryFaceRecognitionByUnids(List<String> unidList) {
if (unidList == null || unidList.size() == 0) {
return null;
}
Map<String, List<String>> param = Collections.singletonMap("unids", unidList);
return namedParameterJdbcTemplate.query(QUERY_FACE_BY_UNID, param, FACE_RECOGNITION_ROW_MAPPER);
}
}
package com.viontech.match.service;
import com.viontech.match.entity.Person;
import com.viontech.match.entity.MatchResult;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
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.common.xcontent.XContentType;
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("*"), 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(40);
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') + 1) / 2 * 100", 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) {
String personId = item.getId();
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)
.id(person.getId())
.source(XContentType.JSON, "personId", person.getPersonId(), "data", person.getFeature());
return client.index(indexRequest, RequestOptions.DEFAULT);
}
public Object addAllPerson(List<Person> persons, String poolId) throws Exception {
BulkRequest bulkRequest = new BulkRequest();
for (Person person : persons) {
bulkRequest.add(new IndexRequest(poolId)
.id(person.getId())
.source(XContentType.JSON, "personId", person.getPersonId(), "data", person.getFeature()));
}
return client.bulk(bulkRequest, 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.viontech.match.service;
import com.viontech.keliu.i18n.util.LocalMessageUtil;
import com.viontech.keliu.websocket.AlgApiClient;
import com.viontech.match.entity.MatchResult;
import com.viontech.match.entity.Person;
import com.viontech.match.entity.vo.RequestData;
import com.viontech.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();
Double[] data = extractFeature(file);
Person person = new Person().setFeature(data);
return algService.matchPerson(person, requestData.getPoolId());
}
public Double[] extractFeature(MultipartFile file) throws Exception {
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();
return objects.toArray(new Double[]{});
}
}
package com.viontech.match.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.keliu.model.FaceFeature;
import com.viontech.keliu.model.Person;
import com.viontech.keliu.model.Pool;
import com.viontech.match.entity.vo.RequestVo;
import com.viontech.match.entity.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScriptScoreQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
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.*;
import java.util.stream.Collectors;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
@Service
@Slf4j
public class PersonService {
@Resource
private RestHighLevelClient client;
@Resource
private PoolService poolService;
@Resource
private ObjectMapper objectMapper;
/**
* 人员比对
*/
public ResponseVo matchPerson(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
String poolId = requestVo.getPersonPoolId();
List<String> unionPersonPoolId = requestVo.getUnionPersonPoolId();
List<String> poolIds = new ArrayList<>();
List<Person> result = new ArrayList<>();
List<Pool> poolStatus = new ArrayList<>();
if (unionPersonPoolId != null && unionPersonPoolId.size() > 0) {
poolIds.addAll(unionPersonPoolId);
}
if (poolId != null) {
poolIds.add(poolId);
}
log.info("人员匹配操作开始,PoolIds:[{}]", poolIds.toString());
try {
for (String id : poolIds) {
Pool pool = new Pool();
pool.setPersonPoolId(id);
if (poolService.existPool(id)) {
pool.setStatus(0);
List<Person> people = matchPerson(id, requestVo.getPerson());
result.addAll(people);
} else {
pool.setStatus(1);
}
poolStatus.add(pool);
}
ResponseVo success = ResponseVo.success(rid, "success");
success.setMatchPersons(result);
success.setMatch(1);
success.setPersonPoolStatus(poolStatus);
log.info("人员匹配操作完成,PoolIds:[{}},结果:[{}]", poolIds.toString(), objectMapper.writeValueAsString(success));
return success;
} catch (ElasticsearchStatusException e) {
log.error("matchPerson", e);
ResponseVo error = ResponseVo.error(rid, e.getDetailedMessage());
error.setMatch(0);
return error;
}
}
/**
* 修改人员特征
*/
public ResponseVo updatePerson(RequestVo requestVo) {
return null;
}
/**
* 添加人员
*/
public BulkResponse addPerson(String poolId, List<Person> personPool) throws IOException {
BulkRequest bulkRequest = new BulkRequest(poolId);
for (Person person : personPool) {
Integer age = person.getAge();
String gender = person.getGender();
String personId = person.getPersonId();
List<FaceFeature> faceFeatures = person.getFaceFeatures();
if (faceFeatures != null && faceFeatures.size() > 0) {
for (FaceFeature faceFeature : faceFeatures) {
Double[] feature = faceFeature.getFeature();
String fid = faceFeature.getFid();
IndexRequest indexRequest = new IndexRequest(poolId)
.source(XContentType.JSON, "personId", personId, "data", feature, "fid", fid, "age", age, "gender", gender);
bulkRequest.add(indexRequest);
}
}
}
if (bulkRequest.requests().size() == 0) {
return null;
}
return client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
/**
* 删除人员
*/
public BulkByScrollResponse deletePerson(String poolId, String personId) throws IOException {
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(poolId)
.setQuery(new TermQueryBuilder("personId", personId))
.setRefresh(true);
return client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
}
public List<Person> matchPerson(String poolId, Person person) throws Exception {
List<FaceFeature> faceFeatures = person.getFaceFeatures();
List<Person> persons = new ArrayList<>();
if (faceFeatures != null && faceFeatures.size() > 0) {
for (FaceFeature faceFeature : faceFeatures) {
Double[] feature = faceFeature.getFeature();
if (feature.length < 512) {
continue;
}
Script script = new Script(
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
"(cosineSimilarity(params.data, 'data') + 1) / 2 * 100", Collections.singletonMap("data", feature));
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.matchAllQuery(), script);
SearchSourceBuilder builder = new SearchSourceBuilder()
.size(5)
.query(queryBuilder)
.fetchSource(new String[]{"personId", "age", "gender", "fid"}, null);
SearchRequest searchRequest = new SearchRequest(poolId)
.source(builder);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit item : hits1) {
Map<String, Object> source = item.getSourceAsMap();
Person p = new Person();
p.setPersonId((String) source.get("personId"));
p.setAge((Integer) source.get("age"));
p.setGender((String) source.get("gender"));
p.setScore((int) item.getScore());
p.setPersonPoolId(item.getIndex());
persons.add(p);
}
}
} else {
log.info("no face feature");
}
if (persons.size() > 5) {
persons = persons.stream().sorted(Comparator.comparingInt(Person::getScore)).limit(5).collect(Collectors.toList());
}
return persons;
}
}
package com.viontech.match.service;
import com.viontech.keliu.model.Person;
import com.viontech.match.entity.PoolInfo;
import com.viontech.match.entity.vo.RequestVo;
import com.viontech.match.entity.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
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.rest.RestStatus;
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;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
@Service
@Slf4j
public class PoolService {
@Resource
private PersonService personService;
@Resource
private RestHighLevelClient client;
/**
* 添加特征池
*/
public ResponseVo createPool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
String poolId = requestVo.getPoolId();
log.info("特征池创建操作开始:[{}}", poolId);
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(poolId);
XContentBuilder builder = getCreateIndexXContentBuilder();
createIndexRequest.mapping(builder);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
List<Person> personPool = requestVo.getPersonPool();
if (personPool != null && personPool.size() > 0) {
BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool);
if (bulkItemResponses != null) {
log.info(bulkItemResponses.buildFailureMessage());
}
}
log.info("特征池创建操作完成:[{}]", poolId);
return ResponseVo.success(rid);
} catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.BAD_REQUEST) {
return ResponseVo.error(rid, 20, "already exit poolId");
} else {
log.error("createPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
}
/**
* 删除特征池
*/
public ResponseVo deletePool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
Integer flushPool = requestVo.getFlushPool();
String poolId = requestVo.getPoolId();
log.info("特征池删除操作开始:[{}],FlushPool:[{}]", poolId, flushPool);
try {
if (0 == flushPool) {
List<HashMap<String, String>> personIds = requestVo.getPersonIds();
for (HashMap<String, String> item : personIds) {
String personId = item.get("personId");
if (personId != null) {
personService.deletePerson(poolId, personId);
}
}
} else if (flushPool == 1) {
AcknowledgedResponse delete = client.indices().delete(new DeleteIndexRequest(poolId), RequestOptions.DEFAULT);
}
log.info("特征池删除操作完成:[{}],FlushPool:[{}]", poolId, flushPool);
return ResponseVo.success(rid);
} catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.NOT_FOUND) {
return ResponseVo.error(rid, 4, "pollID not exist");
} else {
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
}
/**
* 修改特征池(添加人员)
*/
public ResponseVo modifyPool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
String poolId = requestVo.getPoolId();
List<Person> personPool = requestVo.getPersonPool();
Integer updateType = requestVo.getUpdateType();
log.info("特征池修改操作开始:[{}],updateType:[{}]", poolId, updateType);
try {
BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool);
if (bulkItemResponses != null && bulkItemResponses.hasFailures()) {
log.info(bulkItemResponses.buildFailureMessage());
}
log.info("特征池修改操作完成:[{}],updateType:[{}]", poolId, updateType);
return ResponseVo.success(rid, "success");
} catch (ElasticsearchStatusException e) {
log.error("modifyPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
/**
* 查询特征池信息
*/
public ResponseVo queryPool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
Integer listAll = requestVo.getListAll();
String poolId = requestVo.getPoolId();
try {
List<PoolInfo> poolInfos = new ArrayList<>();
if (listAll != 0) {
poolId = "*";
}
log.info("查询特征池操作开始:[{}],ListAll:[{}]", poolId, listAll);
GetIndexResponse response = client.indices().get(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
String[] indices = response.getIndices();
for (String index : indices) {
PoolInfo poolInfo = new PoolInfo();
poolInfo.setPoolId(index);
CountRequest countRequest = new CountRequest(index);
CountResponse count = client.count(countRequest, RequestOptions.DEFAULT);
poolInfo.setPersonCount(count.getCount());
poolInfos.add(poolInfo);
}
ResponseVo success = ResponseVo.success(rid, "success");
success.setPoolIds(poolInfos);
log.info("查询特征池操作完成:[{}],ListAll:[{}]", poolId, listAll);
return success;
} catch (ElasticsearchStatusException e) {
log.error("queryPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
public XContentBuilder getCreateIndexXContentBuilder() throws IOException {
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.startObject("fid");
{
builder.field("type", "text");
}
builder.endObject();
builder.startObject("age");
{
builder.field("type", "integer");
}
builder.endObject();
builder.startObject("gender");
{
builder.field("type", "keyword");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
return builder;
}
public boolean existPool(String poolId) throws IOException {
return client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
}
}
package com.viontech.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.viontech.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;
}
}
}
}
}
}
# 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
# other # other
server.port=12000 server.port=12000
spring.jackson.time-zone=GMT+8 spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# es # es
spring.elasticsearch.rest.uris=http://139.217.100.35:9200
\ No newline at end of file \ No newline at end of file
spring.elasticsearch.rest.uris=http://192.168.9.116:9200
\ No newline at end of file \ No newline at end of file
spring: spring:
profiles: profiles:
active: option active: option
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
default-property-inclusion: non_null
<?xml version="1.0" encoding="UTF-8"?>
<!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 -->
<!-- scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true -->
<!-- scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
<!-- debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="10 seconds">
<!--<include resource="org/springframework/boot/logging/logback/base.xml" />-->
<contextName>logback</contextName>
<!-- name的值是变量的名称,value的值时变量定义的值。通过定义的值会被插入到logger上下文中。定义变量后,可以使“${}”来使用变量。 -->
<property name="log.path" value="logs"/>
<property name="pattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] %logger{50} - %msg%n"/>
<!--输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<encoder>
<Pattern>${pattern}</Pattern>
<!-- 设置字符集 -->
</encoder>
</appender>
<!--输出到文件-->
<!-- 时间滚动输出 level为 DEBUG 日志 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_debug.log</file>
<!--日志文件输出格式-->
<encoder>
<Pattern>${pattern}</Pattern>
<charset>UTF-8</charset> <!-- 设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_info.log</file>
<!--日志文件输出格式-->
<encoder>
<Pattern>${pattern}</Pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>NEUTRAL</onMismatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_warn.log</file>
<!--日志文件输出格式-->
<encoder>
<Pattern>${pattern}</Pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>5</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录warn级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_error.log</file>
<!--日志文件输出格式-->
<encoder>
<Pattern>${pattern}</Pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="com.viontech.match" level="debug">
<appender-ref ref="DEBUG_FILE"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="WARN_FILE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
</configuration>
\ No newline at end of file \ No newline at end of file
package com.viontech.match; package com.viontech.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.viontech.match.entity.*;
import com.viontech.match.repository.FaceRecognitionRepository;
import com.viontech.match.service.AlgService;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; 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.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* . * .
* *
...@@ -42,137 +15,5 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -42,137 +15,5 @@ import java.util.concurrent.atomic.AtomicInteger;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class Test0 { public class Test0 {
private final ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(20, 30, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
private final AtomicInteger integer = new AtomicInteger();
private final PoolEnum pool = PoolEnum.POOL_50W;
@Resource
ObjectMapper objectMapper;
@Resource
private AlgService algService;
@Resource
private FaceRecognitionRepository faceRecognitionRepository;
@Resource
private Storage featureStorage;
@Resource
private RestHighLevelClient client;
@Test
public void createPoolTest() throws IOException {
Object test2 = algService.createPool(pool.name);
System.out.println(test2);
}
@Test
public void deleteIndexTest() throws IOException {
algService.deletePool(pool.name);
}
@Test
public void queryIndex() throws IOException {
String[] strings = algService.queryPoolList();
HashMap<String, Object> result = new HashMap<>();
for (String item : strings) {
CountResponse count = client.count(new CountRequest(item), RequestOptions.DEFAULT);
long count1 = count.getCount();
result.put(item, count1);
}
System.out.println(result.toString());
}
@Test
public void addPersonTest() throws IOException {
Person person1 = getPerson("3");
Object test2 = algService.addPerson(person1, pool.name);
System.out.println(objectMapper.writeValueAsString(test2));
}
@Test
public void matchPersonTest() throws Exception {
Person person = getPerson("3");
List<MatchResult> test2 = algService.matchPerson(person, pool.name);
System.out.println(objectMapper.writeValueAsString(test2));
}
@Test
public void batchAdd() throws ParseException, IOException, InterruptedException {
algService.deletePool(pool.name);
algService.createPool(pool.name);
Date start = DateUtil.parse(DateUtil.FORMAT_SHORT, "2020-11-01");
Date end = DateUtil.parse(DateUtil.FORMAT_SHORT, "2020-11-24");
int i = 0;
List<Date> days = DateUtil.getDaysBetweenDates(start, end);
for (Date day : days) {
List<FaceRecognition> faceRecognitions = faceRecognitionRepository.queryFaceRecognitions(day, 2L);
for (FaceRecognition faceRecognition : faceRecognitions) {
poolExecutor.execute(() -> {
try {
if (integer.get() >= pool.num) {
return;
}
Double[] feature = getFeature(faceRecognition);
if (feature == null) {
return;
}
Person person = new Person();
person.setPersonId(faceRecognition.getPersonUnid());
person.setFeature(feature);
person.setId(faceRecognition.getUnid());
Object o = algService.addPerson(person, pool.name);
System.out.println(objectMapper.writeValueAsString(o));
integer.addAndGet(1);
} catch (Exception e) {
e.printStackTrace();
}
});
if (integer.get() > pool.num) {
return;
}
while (poolExecutor.getQueue().size() > 1000) {
System.out.println(poolExecutor.getQueue().size());
TimeUnit.SECONDS.sleep(10);
}
if (integer.get() > pool.num) {
return;
}
}
}
}
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!