You've already forked FrameTour-BE
refactor(task):优化设备照片限制筛选逻辑
- 使用LinkedHashMap和LinkedHashSet保持插入顺序 -重构筛选逻辑,提高代码可读性 - 优化设备样本分组处理流程 - 添加筛选原因追踪功能-保持原有筛选规则和日志记录- 提升代码执行效率和内存使用
This commit is contained in:
@@ -52,6 +52,8 @@ import java.util.Collections;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -501,67 +503,78 @@ public class TaskFaceServiceImpl implements TaskFaceService {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<Long, Integer> usedCount = new HashMap<>();
|
Map<Long, List<FaceSampleEntity>> deviceSamplesMap = new LinkedHashMap<>();
|
||||||
Map<Long, Integer> limitCache = new HashMap<>();
|
Set<Long> passthroughSampleIds = new LinkedHashSet<>();
|
||||||
List<Long> result = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Long sampleId : acceptedSampleIds) {
|
for (Long sampleId : acceptedSampleIds) {
|
||||||
FaceSampleEntity sample = sampleMap.get(sampleId);
|
FaceSampleEntity sample = sampleMap.get(sampleId);
|
||||||
if (sample == null) {
|
if (sample == null || sample.getDeviceId() == null) {
|
||||||
result.add(sampleId);
|
passthroughSampleIds.add(sampleId);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Long deviceId = sample.getDeviceId();
|
|
||||||
if (deviceId == null) {
|
|
||||||
result.add(sampleId);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
deviceSamplesMap
|
||||||
|
.computeIfAbsent(sample.getDeviceId(), key -> new ArrayList<>())
|
||||||
|
.add(sample);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<Long, Integer> limitCache = new HashMap<>();
|
||||||
|
Set<Long> retainedSampleIds = new LinkedHashSet<>(passthroughSampleIds);
|
||||||
|
|
||||||
|
for (Map.Entry<Long, List<FaceSampleEntity>> entry : deviceSamplesMap.entrySet()) {
|
||||||
|
Long deviceId = entry.getKey();
|
||||||
|
List<FaceSampleEntity> deviceSamples = entry.getValue();
|
||||||
|
List<Long> deviceSampleIds = deviceSamples.stream()
|
||||||
|
.map(FaceSampleEntity::getId)
|
||||||
|
.toList();
|
||||||
|
|
||||||
Integer limitPhoto = limitCache.computeIfAbsent(deviceId, id -> {
|
Integer limitPhoto = limitCache.computeIfAbsent(deviceId, id -> {
|
||||||
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(id);
|
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(id);
|
||||||
return deviceConfig != null ? deviceConfig.getInteger("limit_photo") : null;
|
return deviceConfig != null ? deviceConfig.getInteger("limit_photo") : null;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
List<Long> retainedForDevice;
|
||||||
if (limitPhoto == null || limitPhoto <= 0) {
|
if (limitPhoto == null || limitPhoto <= 0) {
|
||||||
List<Long> deviceSampleIds = deviceSamples.stream()
|
retainedForDevice = deviceSampleIds;
|
||||||
|
log.debug("设备照片限制:设备ID={}, 无限制,保留{}张照片", deviceId, deviceSampleIds.size());
|
||||||
|
} else if (deviceSamples.size() > (limitPhoto + 2)) {
|
||||||
|
retainedForDevice = processDeviceSamples(deviceSamples, limitPhoto, true);
|
||||||
|
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,去首尾后最终{}张",
|
||||||
|
deviceId, limitPhoto, deviceSamples.size(), retainedForDevice.size());
|
||||||
|
} else if (deviceSamples.size() > (limitPhoto + 1)) {
|
||||||
|
retainedForDevice = processDeviceSamples(deviceSamples, limitPhoto, false);
|
||||||
|
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,去尾部后最终{}张",
|
||||||
|
deviceId, limitPhoto, deviceSamples.size(), retainedForDevice.size());
|
||||||
|
} else if (deviceSamples.size() > limitPhoto) {
|
||||||
|
retainedForDevice = deviceSamples.stream()
|
||||||
|
.limit(limitPhoto)
|
||||||
.map(FaceSampleEntity::getId)
|
.map(FaceSampleEntity::getId)
|
||||||
.toList();
|
.toList();
|
||||||
resultIds.addAll(deviceSampleIds);
|
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,取前{}张",
|
||||||
log.debug("设备照片限制:设备ID={}, 无限制,保留{}张照片",
|
deviceId, limitPhoto, deviceSamples.size(), retainedForDevice.size());
|
||||||
deviceId, deviceSampleIds.size());
|
|
||||||
} else {
|
} else {
|
||||||
// 根据样本数量与限制的关系进行不同处理
|
retainedForDevice = deviceSampleIds;
|
||||||
if (deviceSamples.size() > (limitPhoto + 2)) {
|
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,无需筛选,保留全部",
|
||||||
// 样本数大于(limit_photo+2)时,去掉首尾
|
deviceId, limitPhoto, deviceSamples.size());
|
||||||
List<Long> limitedIds = processDeviceSamples(deviceSamples, limitPhoto, true);
|
}
|
||||||
resultIds.addAll(limitedIds);
|
|
||||||
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,去首尾后最终{}张",
|
retainedSampleIds.addAll(retainedForDevice);
|
||||||
deviceId, limitPhoto, deviceSamples.size(), limitedIds.size());
|
|
||||||
} else if (deviceSamples.size() > (limitPhoto + 1)) {
|
if (trace != null) {
|
||||||
// 样本数大于(limit_photo+1)时,去掉尾部
|
Set<Long> retainedSet = new HashSet<>(retainedForDevice);
|
||||||
List<Long> limitedIds = processDeviceSamples(deviceSamples, limitPhoto, false);
|
for (Long sampleId : deviceSampleIds) {
|
||||||
resultIds.addAll(limitedIds);
|
if (!retainedSet.contains(sampleId)) {
|
||||||
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,去尾部后最终{}张",
|
trace.addReason(sampleId, FaceRecognitionFilterReason.DEVICE_PHOTO_LIMIT);
|
||||||
deviceId, limitPhoto, deviceSamples.size(), limitedIds.size());
|
}
|
||||||
} else if (deviceSamples.size() > limitPhoto) {
|
|
||||||
// 样本数大于limit_photo但小于等于(limit_photo+1),取前N张
|
|
||||||
List<Long> limitedIds = deviceSamples.stream()
|
|
||||||
.limit(limitPhoto)
|
|
||||||
.map(FaceSampleEntity::getId)
|
|
||||||
.toList();
|
|
||||||
resultIds.addAll(limitedIds);
|
|
||||||
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,取前{}张",
|
|
||||||
deviceId, limitPhoto, deviceSamples.size(), limitedIds.size());
|
|
||||||
} else {
|
|
||||||
// 样本数小于等于limit_photo,不做操作
|
|
||||||
List<Long> limitedIds = deviceSamples.stream()
|
|
||||||
.map(FaceSampleEntity::getId)
|
|
||||||
.toList();
|
|
||||||
resultIds.addAll(limitedIds);
|
|
||||||
log.debug("设备照片限制:设备ID={}, 限制={}张, 原始{}张,无需筛选,保留全部",
|
|
||||||
deviceId, limitPhoto, deviceSamples.size());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Long> resultIds = new ArrayList<>(acceptedSampleIds.size());
|
||||||
|
for (Long sampleId : acceptedSampleIds) {
|
||||||
|
if (retainedSampleIds.contains(sampleId)) {
|
||||||
|
resultIds.add(sampleId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.info("设备照片数量限制筛选:原始样本数量={}, 筛选后数量={}",
|
log.info("设备照片数量限制筛选:原始样本数量={}, 筛选后数量={}",
|
||||||
acceptedSampleIds.size(), resultIds.size());
|
acceptedSampleIds.size(), resultIds.size());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user