Commit e3e2a55d by xmh

1.0.0

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