|
|
@@ -2,6 +2,7 @@ package xn.xxp.app;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.app.Activity;
|
|
|
+import android.content.Context;
|
|
|
import android.os.Environment;
|
|
|
import android.util.Log;
|
|
|
|
|
|
@@ -70,15 +71,23 @@ public enum SyncFaceToolFeature {
|
|
|
|
|
|
/** 特征值文件后缀 */
|
|
|
private static final String FEATURE_FILE_EXTENSION = ".dat";
|
|
|
- /** 等待人脸建模完成的最长静默时间(毫秒),期间没有任何建模事件即视为超时 */
|
|
|
- private static final long BUILD_TIMEOUT = 2000;
|
|
|
- /** 单次建模的绝对时长上限(毫秒),防止 SDK 持续上报进度事件导致同步线程被永久挂住 */
|
|
|
- private static final long BUILD_MAX_DURATION = 60_000;
|
|
|
+ /** 等待人脸建模完成的最长时间(毫秒),超时按失败处理,下一轮会重建 */
|
|
|
+ private static final long BUILD_TIMEOUT = 5000;
|
|
|
/** 特征值文件目录,与图片流程保持一致(人脸服务进程需要能读到) */
|
|
|
@SuppressLint("SdCardPath")
|
|
|
private static final String FEATURE_DIR = "/sdcard/" + Environment.DIRECTORY_DOWNLOADS + "/faceFeature/";
|
|
|
/** 判定为 Base64 特征值的最小解码长度,真实人脸特征值至少几百字节 */
|
|
|
private static final int MIN_FEATURE_BYTES = 64;
|
|
|
+ /**
|
|
|
+ * 人脸服务是异步建模:{@code FaceSdk.faceFeatureImport} 返回 1 表示"已受理",
|
|
|
+ * 建模结果通过回调(REMOTE_CMD_202/203)通知,真正的失败码是
|
|
|
+ * 1000=空列表、1015=SDK 未初始化、1201=未绑定人脸服务。
|
|
|
+ */
|
|
|
+ private static final int BUILD_ACCEPTED = 1;
|
|
|
+ /** SharedPreferences 名称,用于持久化"已成功建模进底库"的人员 */
|
|
|
+ private static final String SP_NAME = "xn_xxp_face";
|
|
|
+ /** SharedPreferences 键:已成功建模进底库的人员 userId */
|
|
|
+ private static final String SP_KEY_BUILT = "built_user_ids";
|
|
|
|
|
|
private volatile boolean isBuildFaceEnd = false;
|
|
|
public volatile boolean isSyncFace = false;
|
|
|
@@ -127,11 +136,12 @@ public enum SyncFaceToolFeature {
|
|
|
FaceConfig faceConfig = FaceUtils.getInstance().getFaceConfig();
|
|
|
LogUtils.json("底库配置", faceConfig);
|
|
|
FaceFeatureDao featureDao = RoomTool.getInstance().faceFeatureDao();
|
|
|
- if (isInit) {
|
|
|
- // 删除先前的底库
|
|
|
- FaceUtils.getInstance().deleteAllFace();
|
|
|
- featureDao.clear();
|
|
|
- }
|
|
|
+ // 这里刻意不做全量清库(deleteAllFace + featureDao.clear())。
|
|
|
+ // 人脸服务进程只在自身启动时把底库读进原生比对库(readModelData),
|
|
|
+ // deleteAllFace() 会把这份已缓存的模型数据一并清掉且不会自动恢复,
|
|
|
+ // 之后识别时原生比对库是空的(SetFaceLib: nFaceNum(0)),
|
|
|
+ // 1:N 比对永远拿不到候选,SDK 只能按"陌生人"返回(sim=-1.0,faceId 恒为 null)。
|
|
|
+ // 底库的增删统一交给后面的增量流程:先重建,再删名单外的人。
|
|
|
EventBus.getDefault().register(this);
|
|
|
// 1. 拉取人脸特征值(唯一的数据来源,不再单独拉人脸图片)
|
|
|
List<FaceFeature> features = fetchLabFaceFeatureList();
|
|
|
@@ -147,6 +157,8 @@ public enum SyncFaceToolFeature {
|
|
|
}
|
|
|
// 3. 人脸特征落库(byte[],同时也是人脸资料,界面只认这一张表)
|
|
|
syncFaceFeatures(features);
|
|
|
+ // 3.1 名单外的人从本地表清掉(服务端名单是全量的)
|
|
|
+ clearLocalOutOfRoster(features);
|
|
|
// 4. 筛出需要重建的特征
|
|
|
List<FaceFeature> changed = filterChangedFeatures(features, localFeatures);
|
|
|
LogUtils.d("人脸特征值 " + features.size() + " 条,需要重建 " + changed.size() + " 条");
|
|
|
@@ -160,28 +172,25 @@ public enum SyncFaceToolFeature {
|
|
|
// 6. 人脸建模(新版 SDK 走特征值导入:FaceBuildBean.featureImportPath)
|
|
|
// 先打时间戳:SDK 可能在调用返回前就回调进度事件
|
|
|
lastBuildTime = System.currentTimeMillis();
|
|
|
- long buildStart = lastBuildTime;
|
|
|
+ // 提交前的建模结果快照,用来判断后面拿到的是不是本次的新结果
|
|
|
+ List<FaceBuildBean> before = FaceUtils.getInstance().getFaceResult();
|
|
|
int buildRet = FaceUtils.getInstance().faceFeatureImport(faceBuildBeanList);
|
|
|
- if (buildRet != 0) {
|
|
|
+ if (buildRet != BUILD_ACCEPTED) {
|
|
|
// 1000=空列表 1015=SDK 未初始化 1201=未绑定人脸服务
|
|
|
- LogUtils.e("特征值导入失败, 错误码 = " + buildRet);
|
|
|
+ LogUtils.e("特征值导入提交失败, 错误码 = " + buildRet);
|
|
|
buildOk = false;
|
|
|
} else {
|
|
|
- // 等待人脸建模完成(由 onReceive 回调置位 isBuildFaceEnd)
|
|
|
- while (!isBuildFaceEnd) {
|
|
|
- Thread.sleep(10);
|
|
|
- long now = System.currentTimeMillis();
|
|
|
- if (now - lastBuildTime > BUILD_TIMEOUT) {
|
|
|
- // 2 秒没有任何建模事件,认为超时
|
|
|
- LogUtils.e("等待人脸建模超时");
|
|
|
- buildOk = false;
|
|
|
- break;
|
|
|
- }
|
|
|
- if (now - buildStart > BUILD_MAX_DURATION) {
|
|
|
- LogUtils.e("等待人脸建模超时(超过最大时长)");
|
|
|
- buildOk = false;
|
|
|
- break;
|
|
|
- }
|
|
|
+ // 人脸服务是异步建模:返回 BUILD_ACCEPTED(1) 只代表"已受理",
|
|
|
+ // 真正的结果在服务进程里异步产出。
|
|
|
+ List<String> expected = new ArrayList<>();
|
|
|
+ for (FaceBuildBean bean : faceBuildBeanList) {
|
|
|
+ expected.add(bean.getFaceUuid());
|
|
|
+ }
|
|
|
+ if (waitBuildResult(expected, before)) {
|
|
|
+ applyBuildResult(FaceUtils.getInstance().getFaceResult());
|
|
|
+ } else {
|
|
|
+ LogUtils.e("等待人脸建模超时");
|
|
|
+ buildOk = false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -250,7 +259,26 @@ public enum SyncFaceToolFeature {
|
|
|
if (null == features || features.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
- RoomTool.getInstance().faceFeatureDao().insertAll(features.toArray(new FaceFeature[0]));
|
|
|
+ // 服务端会给没采集过特征值的人下发 faceFeature=null,
|
|
|
+ // 直接落库会让 Room 绑定 null blob 抛 NullPointerException,
|
|
|
+ // 整个同步流程在调用点中断,后面的建模步骤根本执行不到。
|
|
|
+ // 这里只落库真正有特征值的人。
|
|
|
+ List<FaceFeature> valid = new ArrayList<>();
|
|
|
+ for (FaceFeature feature : features) {
|
|
|
+ if (null == feature) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ byte[] data = feature.getFaceFeature();
|
|
|
+ if (null == data || data.length == 0) {
|
|
|
+ LogUtils.d("服务端未下发人脸特征值, 跳过落库", feature.getUserId(), feature.getUserName());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ valid.add(feature);
|
|
|
+ }
|
|
|
+ if (valid.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ RoomTool.getInstance().faceFeatureDao().insertAll(valid.toArray(new FaceFeature[0]));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -267,6 +295,9 @@ public enum SyncFaceToolFeature {
|
|
|
if (null != allFaceIdList) {
|
|
|
inDb.addAll(allFaceIdList);
|
|
|
}
|
|
|
+ // 底库名册只能说明"这个人登记过",不能说明"这个人的模型真的建好了"。
|
|
|
+ // 建模失败时名册里依然会留人,只看名册会永远判定成"没变化",从而再也不重建。
|
|
|
+ Set<String> builtIds = readBuiltIds();
|
|
|
List<FaceFeature> changed = new ArrayList<>();
|
|
|
if (null == features) {
|
|
|
return changed;
|
|
|
@@ -276,8 +307,8 @@ public enum SyncFaceToolFeature {
|
|
|
FaceFeature local = localFeatures.get(feature.getUserId());
|
|
|
boolean contentChanged = null == local
|
|
|
|| !Arrays.equals(feature.getFaceFeature(), local.getFaceFeature());
|
|
|
- if (inDb.contains(userId) && !contentChanged) {
|
|
|
- // 底库里已有且特征值没有变化,跳过
|
|
|
+ if (inDb.contains(userId) && !contentChanged && builtIds.contains(userId)) {
|
|
|
+ // 底库里已有、特征值没有变化、且上一次确实建模成功,跳过
|
|
|
continue;
|
|
|
}
|
|
|
if (null == feature.getFaceFeature() || feature.getFaceFeature().length == 0) {
|
|
|
@@ -333,18 +364,160 @@ public enum SyncFaceToolFeature {
|
|
|
@Subscribe(threadMode = ThreadMode.ASYNC)
|
|
|
public void onReceive(BuildModelEvent buildModelEvent) {
|
|
|
if (buildModelEvent.isDone()) {
|
|
|
- List<FaceBuildBean> list = FaceUtils.getInstance().getFaceResult();
|
|
|
- if (list != null && !list.isEmpty()) {
|
|
|
- for (FaceBuildBean faceBuildBean : list) {
|
|
|
- // 判断建模是否成功 (errCode 为 0 表示成功)
|
|
|
- if (faceBuildBean.getErrCode() != 0) {
|
|
|
- LogUtils.e("建模失败", faceBuildBean.getFaceUuid(), faceBuildBean.getErrCode());
|
|
|
+ applyBuildResult(FaceUtils.getInstance().getFaceResult());
|
|
|
+ isBuildFaceEnd = true;
|
|
|
+ }
|
|
|
+ lastBuildTime = System.currentTimeMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 等待人脸建模完成。
|
|
|
+ *
|
|
|
+ * <p>不能依赖 EventBus 回调:SDK 的 {@code startDev()} 是异步启动的,
|
|
|
+ * 人脸服务的数据监听要等它跑完才注册好,而 App 提交建模后很快就走到
|
|
|
+ * {@code finally} 里的 {@code destroy()},建模完成事件常在监听就绪前到达而被丢弃
|
|
|
+ * (人脸服务侧日志里表现为 {@code faceDataCall: type = 3, listener null}),
|
|
|
+ * 结果就是永远等不到回调。这里改成直接轮询人脸服务的建模结果。
|
|
|
+ *
|
|
|
+ * @return true 表示本次提交的人都已经建模成功
|
|
|
+ */
|
|
|
+ private boolean waitBuildResult(List<String> expectedUuids, List<FaceBuildBean> before) {
|
|
|
+ long deadline = System.currentTimeMillis() + BUILD_TIMEOUT;
|
|
|
+ while (System.currentTimeMillis() < deadline) {
|
|
|
+ List<FaceBuildBean> now = FaceUtils.getInstance().getFaceResult();
|
|
|
+ if (null != now && !now.isEmpty() && isNewBuildResult(before, now)) {
|
|
|
+ for (String uuid : expectedUuids) {
|
|
|
+ FaceBuildBean last = null;
|
|
|
+ for (FaceBuildBean bean : now) {
|
|
|
+ if (bean != null && uuid.equals(bean.getFaceUuid())) {
|
|
|
+ // 结果列表按时间追加,取最后一条即最新一次的结果
|
|
|
+ last = bean;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null == last || last.getErrCode() != 0) {
|
|
|
+ LogUtils.e("建模失败", uuid, null == last ? -1 : last.getErrCode());
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Thread.sleep(100);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断拿到的建模结果是不是本次提交产生的新结果。
|
|
|
+ */
|
|
|
+ private boolean isNewBuildResult(List<FaceBuildBean> before, List<FaceBuildBean> now) {
|
|
|
+ if (null == before || before.isEmpty()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (before.size() != now.size()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ FaceBuildBean b = before.get(before.size() - 1);
|
|
|
+ FaceBuildBean n = now.get(now.size() - 1);
|
|
|
+ if (null == b || null == n) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return b.getErrCode() != n.getErrCode()
|
|
|
+ || !String.valueOf(b.getFaceUuid()).equals(String.valueOf(n.getFaceUuid()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用人脸服务的建模结果更新"已成功建模进底库"的人员名单。
|
|
|
+ *
|
|
|
+ * <p>结果列表按时间追加,同一个人可能出现多条,以最后一条为准。
|
|
|
+ */
|
|
|
+ private void applyBuildResult(List<FaceBuildBean> list) {
|
|
|
+ if (null == list || list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Set<String> built = readBuiltIds();
|
|
|
+ for (FaceBuildBean bean : list) {
|
|
|
+ if (null == bean) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (bean.getErrCode() != 0) {
|
|
|
+ LogUtils.e("建模失败", bean.getFaceUuid(), bean.getErrCode());
|
|
|
+ // 建模失败的人不能算"已建好",下一轮还要重建
|
|
|
+ built.remove(bean.getFaceUuid());
|
|
|
+ } else {
|
|
|
+ built.add(bean.getFaceUuid());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ writeBuiltIds(built);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取"已成功建模进底库"的人员 userId。
|
|
|
+ */
|
|
|
+ private Set<String> readBuiltIds() {
|
|
|
+ Set<String> ids = new HashSet<>();
|
|
|
+ try {
|
|
|
+ String raw = Utils.getApp().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
|
|
|
+ .getString(SP_KEY_BUILT, "");
|
|
|
+ if (null == raw || raw.isEmpty()) {
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+ for (String id : raw.split(",")) {
|
|
|
+ if (!id.isEmpty()) {
|
|
|
+ ids.add(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存"已成功建模进底库"的人员 userId。
|
|
|
+ */
|
|
|
+ private void writeBuiltIds(Set<String> ids) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String id : ids) {
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append(',');
|
|
|
+ }
|
|
|
+ sb.append(id);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Utils.getApp().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
|
|
|
+ .edit().putString(SP_KEY_BUILT, sb.toString()).apply();
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清理本地表里服务端名单之外的人。
|
|
|
+ *
|
|
|
+ * <p>服务端名单是全量的,名单外的人既不该留在底库,也不该留在本地表里。
|
|
|
+ */
|
|
|
+ private void clearLocalOutOfRoster(List<FaceFeature> features) {
|
|
|
+ if (null == features || features.isEmpty()) {
|
|
|
+ // 名单为空时不清理,避免服务端异常返回把本地资料全清掉
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Set<Long> rosterIds = new HashSet<>();
|
|
|
+ for (FaceFeature feature : features) {
|
|
|
+ if (null != feature) {
|
|
|
+ rosterIds.add(feature.getUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FaceFeatureDao featureDao = RoomTool.getInstance().faceFeatureDao();
|
|
|
+ for (FaceFeature local : featureDao.getAll()) {
|
|
|
+ if (!rosterIds.contains(local.getUserId())) {
|
|
|
+ featureDao.delete(local.getUserId());
|
|
|
}
|
|
|
- isBuildFaceEnd = true;
|
|
|
}
|
|
|
- lastBuildTime = System.currentTimeMillis();
|
|
|
}
|
|
|
|
|
|
/**
|