FaceEngineServiceImpl.java
10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.itboyst.facedemo.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.toolkit.ImageInfo;
import com.itboyst.facedemo.domain.UserFaceEngine;
import com.itboyst.facedemo.dto.ProcessInfo;
import com.itboyst.facedemo.factory.FaceEngineFactory;
import com.itboyst.facedemo.mapper.MybatisUserFaceIEngineMapper;
import com.itboyst.facedemo.mapper.MybatisUserFaceInfoMapper;
import com.itboyst.facedemo.service.FaceEngineService;
import com.itboyst.facedemo.dto.FaceUserInfo;
import com.arcsoft.face.*;
import com.google.common.collect.Lists;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
@Service
public class FaceEngineServiceImpl implements FaceEngineService {
public final static Logger logger = LoggerFactory.getLogger(FaceEngineServiceImpl.class);
@Value("${config.arcface-sdk.sdk-lib-path}")
public String sdkLibPath;
@Value("${config.arcface-sdk.app-id}")
public String appId;
@Value("${config.arcface-sdk.sdk-key}")
public String sdkKey;
@Value("${config.arcface-sdk.thread-pool-size}")
public Integer threadPoolSize;
private Integer passRate = 80;
private ExecutorService executorService;
@Autowired
private MybatisUserFaceIEngineMapper mybatisUserFaceIEngineMapper;
@Autowired
private MybatisUserFaceInfoMapper userFaceInfoMapper;
private GenericObjectPool<FaceEngine> faceEngineObjectPool;
@PostConstruct
public void init() {
executorService = Executors.newFixedThreadPool(threadPoolSize);
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(threadPoolSize);
poolConfig.setMaxTotal(threadPoolSize);
poolConfig.setMinIdle(threadPoolSize);
poolConfig.setLifo(false);
//引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration();
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
//功能配置
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
functionConfiguration.setSupportAge(true);
functionConfiguration.setSupportFace3dAngle(true);
functionConfiguration.setSupportFaceDetect(true);
functionConfiguration.setSupportFaceRecognition(true);
functionConfiguration.setSupportGender(true);
functionConfiguration.setSupportLiveness(true);
functionConfiguration.setSupportIRLiveness(true);
engineConfiguration.setFunctionConfiguration(functionConfiguration);
List<UserFaceEngine> userFaceEngines = mybatisUserFaceIEngineMapper.getUserFaceEngine(11);
UserFaceEngine userFaceEngine = userFaceEngines.get(0);
faceEngineObjectPool = new GenericObjectPool(new FaceEngineFactory(sdkLibPath, userFaceEngine.getApp_id(), userFaceEngine.getSdk_key(), engineConfiguration), poolConfig);//底层库算法对象池
}
private int plusHundred(Float value) {
BigDecimal target = new BigDecimal(value);
BigDecimal hundred = new BigDecimal(100f);
return target.multiply(hundred).intValue();
}
@Override
public List<FaceInfo> detectFaces(ImageInfo imageInfo) {
FaceEngine faceEngine = null;
try {
//获取引擎对象
faceEngine = faceEngineObjectPool.borrowObject();
//人脸检测得到人脸列表
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
//人脸检测
faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
return faceInfoList;
} catch (Exception e) {
logger.error("", e);
} finally {
if (faceEngine != null) {
//释放引擎对象
faceEngineObjectPool.returnObject(faceEngine);
}
}
return null;
}
@Override
public List<ProcessInfo> process(ImageInfo imageInfo) {
FaceEngine faceEngine = null;
try {
//获取引擎对象
faceEngine = faceEngineObjectPool.borrowObject();
//人脸检测得到人脸列表
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
//人脸检测
faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
int processResult = faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, FunctionConfiguration.builder().supportAge(true).supportGender(true).build());
List<ProcessInfo> processInfoList = Lists.newLinkedList();
List<GenderInfo> genderInfoList = new ArrayList<GenderInfo>();
//性别提取
int genderCode = faceEngine.getGender(genderInfoList);
//年龄提取
List<AgeInfo> ageInfoList = new ArrayList<AgeInfo>();
int ageCode = faceEngine.getAge(ageInfoList);
for (int i = 0; i < genderInfoList.size(); i++) {
ProcessInfo processInfo = new ProcessInfo();
processInfo.setGender(genderInfoList.get(i).getGender());
processInfo.setAge(ageInfoList.get(i).getAge());
processInfoList.add(processInfo);
}
return processInfoList;
} catch (Exception e) {
logger.error("", e);
} finally {
if (faceEngine != null) {
//释放引擎对象
faceEngineObjectPool.returnObject(faceEngine);
}
}
return null;
}
/**
* 人脸特征
*
* @param imageInfo
* @return
*/
@Override
public byte[] extractFaceFeature(ImageInfo imageInfo) throws InterruptedException {
FaceEngine faceEngine = null;
try {
//获取引擎对象
faceEngine = faceEngineObjectPool.borrowObject();
//人脸检测得到人脸列表
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
//人脸检测
int i = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
if (CollectionUtil.isNotEmpty(faceInfoList)) {
FaceFeature faceFeature = new FaceFeature();
//提取人脸特征
faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList.get(0), faceFeature);
return faceFeature.getFeatureData();
}
} catch (Exception e) {
logger.error("", e);
} finally {
if (faceEngine != null) {
//释放引擎对象
faceEngineObjectPool.returnObject(faceEngine);
}
}
return null;
}
@Override
public List<FaceUserInfo> compareFaceFeature(byte[] faceFeature, Integer groupId) throws InterruptedException, ExecutionException {
List<FaceUserInfo> resultFaceInfoList = Lists.newLinkedList();//识别到的人脸列表
FaceFeature targetFaceFeature = new FaceFeature();
targetFaceFeature.setFeatureData(faceFeature);
List<FaceUserInfo> faceInfoList = userFaceInfoMapper.getUserFaceInfoByGroupId(groupId); //从数据库中取出人脸库
List<List<FaceUserInfo>> faceUserInfoPartList = Lists.partition(faceInfoList, 1000);//分成1000一组,多线程处理
CompletionService<List<FaceUserInfo>> completionService = new ExecutorCompletionService(executorService);
for (List<FaceUserInfo> part : faceUserInfoPartList) {
completionService.submit(new CompareFaceTask(part, targetFaceFeature));
}
for (int i = 0; i < faceUserInfoPartList.size(); i++) {
List<FaceUserInfo> faceUserInfoList = completionService.take().get();
if (CollectionUtil.isNotEmpty(faceInfoList)) {
resultFaceInfoList.addAll(faceUserInfoList);
}
}
resultFaceInfoList.sort((h1, h2) -> h2.getSimilarValue().compareTo(h1.getSimilarValue()));//从大到小排序
return resultFaceInfoList;
}
private class CompareFaceTask implements Callable<List<FaceUserInfo>> {
private List<FaceUserInfo> faceUserInfoList;
private FaceFeature targetFaceFeature;
public CompareFaceTask(List<FaceUserInfo> faceUserInfoList, FaceFeature targetFaceFeature) {
this.faceUserInfoList = faceUserInfoList;
this.targetFaceFeature = targetFaceFeature;
}
@Override
public List<FaceUserInfo> call() throws Exception {
FaceEngine faceEngine = null;
List<FaceUserInfo> resultFaceInfoList = Lists.newLinkedList();//识别到的人脸列表
try {
faceEngine = faceEngineObjectPool.borrowObject();
for (FaceUserInfo faceUserInfo : faceUserInfoList) {
FaceFeature sourceFaceFeature = new FaceFeature();
sourceFaceFeature.setFeatureData(faceUserInfo.getFaceFeature());
FaceSimilar faceSimilar = new FaceSimilar();
faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
Integer similarValue = plusHundred(faceSimilar.getScore());//获取相似值
if (similarValue > passRate) {//相似值大于配置预期,加入到识别到人脸的列表
FaceUserInfo info = new FaceUserInfo();
info.setName(faceUserInfo.getName());
info.setFaceId(faceUserInfo.getFaceId());
info.setSimilarValue(similarValue);
resultFaceInfoList.add(info);
}
}
} catch (Exception e) {
logger.error("", e);
} finally {
if (faceEngine != null) {
faceEngineObjectPool.returnObject(faceEngine);
}
}
return resultFaceInfoList;
}
}
}