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.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!