Commit e3e2a55d by xmh

1.0.0

1 parent d0cf0291
...@@ -135,7 +135,7 @@ public class PersonService { ...@@ -135,7 +135,7 @@ public class PersonService {
continue; continue;
} }
if (feature.length > Constant.BODY_FEATURE_DIMS) { if (feature.length > Constant.BODY_FEATURE_DIMS) {
feature = Arrays.copyOf(feature, Constant.BODY_FEATURE_DIMS); feature = Arrays.copyOfRange(feature, 3, Constant.BODY_FEATURE_DIMS + 3);
} }
String fid = bodyFeature.getBid(); String fid = bodyFeature.getBid();
...@@ -181,17 +181,20 @@ public class PersonService { ...@@ -181,17 +181,20 @@ public class PersonService {
private void matchFace(List<FaceFeature> faceFeatures, String poolId, List<Person> matchResult) throws IOException { private void matchFace(List<FaceFeature> faceFeatures, String poolId, List<Person> matchResult) throws IOException {
if (faceFeatures != null && faceFeatures.size() > 0) { if (faceFeatures != null && faceFeatures.size() > 0) {
for (FaceFeature faceFeature : faceFeatures) { for (FaceFeature faceFeature : faceFeatures) {
Double[] feature = faceFeature.getFeature(); Double[] feature = faceFeature.getFeature();
if (feature == null || feature.length != Constant.FACE_FEATURE_DIMS) { if (feature == null || feature.length != Constant.FACE_FEATURE_DIMS) {
log.info("人脸特征维数小于512,跳过比对");
continue; continue;
} }
Script script = new Script( Script script = new Script(
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
"(cosineSimilarity(params.face, 'data') + 1) / 2 * 100", Collections.singletonMap("face", feature)); "(cosineSimilarity(params.face, 'data') + 1) / 2 * 100", Collections.singletonMap("face", feature));
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.existsQuery("data"), script);
matchResult.addAll(match0(poolId, script)); matchResult.addAll(match0(poolId, queryBuilder));
} }
} else { } else {
log.info("no face feature"); log.info("no face feature");
...@@ -203,30 +206,29 @@ public class PersonService { ...@@ -203,30 +206,29 @@ public class PersonService {
for (BodyFeature faceFeature : bodyFeatures) { for (BodyFeature faceFeature : bodyFeatures) {
Double[] feature = faceFeature.getFeature(); Double[] feature = faceFeature.getFeature();
if (feature == null || feature.length < Constant.BODY_FEATURE_DIMS) { if (feature == null || feature.length < Constant.BODY_FEATURE_DIMS) {
log.info("人体特征维数小于2048,跳过比对");
continue; continue;
} }
if (feature.length > Constant.BODY_FEATURE_DIMS) { if (feature.length > Constant.BODY_FEATURE_DIMS) {
feature = Arrays.copyOf(feature, Constant.BODY_FEATURE_DIMS); feature = Arrays.copyOfRange(feature, 3, Constant.BODY_FEATURE_DIMS + 3);
} }
Script script = new Script( Script script = new Script(
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
"(cosineSimilarity(params.body, 'body') + 1) / 2 * 100", Collections.singletonMap("body", feature)); "(cosineSimilarity(params.body, 'body') + 1) / 2 * 100", Collections.singletonMap("body", feature));
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.existsQuery("body"), script);
matchResult.addAll(match0(poolId, script)); matchResult.addAll(match0(poolId, queryBuilder));
} }
} else {
log.info("no body feature");
} }
} }
private List<Person> match0(String poolId, Script script) throws IOException { private List<Person> match0(String poolId, ScriptScoreQueryBuilder scriptScoreQueryBuilder) throws IOException {
List<Person> persons = new ArrayList<>(); List<Person> persons = new ArrayList<>();
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.matchAllQuery(), script);
SearchSourceBuilder builder = new SearchSourceBuilder() SearchSourceBuilder builder = new SearchSourceBuilder()
.size(Constant.MATCH_RESULT_SIZE) .size(Constant.MATCH_RESULT_SIZE)
.query(queryBuilder) .query(scriptScoreQueryBuilder)
.fetchSource(new String[]{"personId", "age", "gender", "fid"}, null); .fetchSource(new String[]{"personId", "age", "gender", "fid"}, null);
SearchRequest searchRequest = new SearchRequest(poolId) SearchRequest searchRequest = new SearchRequest(poolId)
.source(builder); .source(builder);
......
...@@ -52,7 +52,7 @@ public class PoolService { ...@@ -52,7 +52,7 @@ public class PoolService {
log.info("特征池创建操作开始:[{}}", poolId); log.info("特征池创建操作开始:[{}}", poolId);
try { try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(poolId); CreateIndexRequest createIndexRequest = new CreateIndexRequest(poolId);
XContentBuilder builder = getCreateIndexXContentBuilder(); XContentBuilder builder = getCreateIndexContentBuilder();
createIndexRequest.mapping(builder); createIndexRequest.mapping(builder);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
...@@ -100,7 +100,7 @@ public class PoolService { ...@@ -100,7 +100,7 @@ public class PoolService {
return ResponseVo.success(rid); return ResponseVo.success(rid);
} catch (ElasticsearchStatusException e) { } catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.NOT_FOUND) { if (e.status() == RestStatus.NOT_FOUND) {
return ResponseVo.error(rid, 4, "pollID not exist"); return ResponseVo.error(rid, 4, "poolId not exists");
} else { } else {
return ResponseVo.error(rid, e.getDetailedMessage()); return ResponseVo.error(rid, e.getDetailedMessage());
} }
...@@ -116,6 +116,9 @@ public class PoolService { ...@@ -116,6 +116,9 @@ public class PoolService {
List<Person> personPool = requestVo.getPersonPool(); List<Person> personPool = requestVo.getPersonPool();
Integer updateType = requestVo.getUpdateType(); Integer updateType = requestVo.getUpdateType();
log.info("特征池修改操作开始:[{}],updateType:[{}]", poolId, updateType); log.info("特征池修改操作开始:[{}],updateType:[{}]", poolId, updateType);
if (!client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT)) {
return ResponseVo.error(rid, 4, "poolId not exists");
}
try { try {
BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool); BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool);
if (bulkItemResponses != null && bulkItemResponses.hasFailures()) { if (bulkItemResponses != null && bulkItemResponses.hasFailures()) {
...@@ -165,32 +168,33 @@ public class PoolService { ...@@ -165,32 +168,33 @@ public class PoolService {
} }
public XContentBuilder getCreateIndexXContentBuilder() throws IOException { public XContentBuilder getCreateIndexContentBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder(); XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject(); builder.startObject();
{ {
builder.startObject("properties"); builder.startObject("properties");
{ {
// 人脸特征
builder.startObject("data"); builder.startObject("data");
{ {
builder.field("type", "dense_vector"); builder.field("type", "dense_vector");
builder.field("dims", Constant.FACE_FEATURE_DIMS); builder.field("dims", Constant.FACE_FEATURE_DIMS);
} }
builder.endObject(); builder.endObject();
// 人体特征
builder.startObject("body"); builder.startObject("body");
{ {
builder.field("type", "dense_vector"); builder.field("type", "dense_vector");
builder.field("dims", Constant.BODY_FEATURE_DIMS); builder.field("dims", Constant.BODY_FEATURE_DIMS);
} }
builder.endObject(); builder.endObject();
// 人员id
builder.startObject("personId"); builder.startObject("personId");
{ {
builder.field("type", "keyword"); builder.field("type", "keyword");
} }
builder.endObject(); builder.endObject();
// 特征id
builder.startObject("fid"); builder.startObject("fid");
{ {
builder.field("type", "text"); builder.field("type", "text");
...@@ -200,12 +204,16 @@ public class PoolService { ...@@ -200,12 +204,16 @@ public class PoolService {
builder.startObject("age"); builder.startObject("age");
{ {
builder.field("type", "integer"); builder.field("type", "integer");
builder.field("doc_values", false);
builder.field("index", false);
} }
builder.endObject(); builder.endObject();
builder.startObject("gender"); builder.startObject("gender");
{ {
builder.field("type", "keyword"); builder.field("type", "keyword");
builder.field("doc_values", false);
builder.field("index", false);
} }
builder.endObject(); builder.endObject();
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!