人脸识别去重时间窗口
1、基本介绍
-
人脸识别系统中存在去重时间窗口,也可以叫防重复识别间隔
-
同一张人脸被识别后,在设定的时间窗口内再次出现时,不再重复处理
-
该窗口时长可配置,常见的例如,5秒、3秒,具体取决于业务场景对效率的要求
2、代码实现
java
// 识别窗口,单位秒
public static final int THROTTLE_WINDOW = 10;
// key 为人脸 Id
// value 为最近一次被采纳识别的时间戳
private static final Map<Integer, Long> identifyTimeMap = new HashMap<>();
public static boolean isInThrottle(int faceImgId) {
Long lastIdentifyTime = identifyTimeMap.get(faceImgId);
if (lastIdentifyTime == null) return false;
return System.currentTimeMillis() - lastIdentifyTime < THROTTLE_WINDOW * 1000L;
}
public static void recordIdentify(int faceImgId) {
identifyTimeMap.put(faceImgId, System.currentTimeMillis());
}
public static void resetThrottle() {
identifyTimeMap.clear();
}
// 返回 true 表示要被拦截,返回 false 表示不被拦截
java
// 识别窗口,单位秒
public static final int THROTTLE_WINDOW = 10;
// key 为人脸 Id
// value 为最近一次被采纳识别的时间戳
private static final Map<Integer, Long> identifyTimeMap = new HashMap<>();
public static boolean handleThrottle(int faceImgId) {
Long lastIdentifyTime = identifyTimeMap.get(faceImgId);
if (lastIdentifyTime != null
&& System.currentTimeMillis() - lastIdentifyTime < THROTTLE_WINDOW * 1000L) {
return true;
}
identifyTimeMap.put(faceImgId, System.currentTimeMillis());
return false;
}
public static void resetThrottle() {
identifyTimeMap.clear();
}
// 返回 true 表示要被拦截,返回 false 表示不被拦截