You've already forked FrameTour-BE
feat(repository): 优化AI相机照片处理性能
- 引入CompletableFuture实现照片处理并发执行 - 创建专用线程池IMAGE_PROCESS_EXECUTOR管理异步任务 - 将原有串行处理逻辑改为并行处理 - 更新默认存储适配器从assets到assets-ext
This commit is contained in:
@@ -28,11 +28,24 @@ import org.springframework.stereotype.Component;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class SourceRepository {
|
public class SourceRepository {
|
||||||
|
private static final ExecutorService IMAGE_PROCESS_EXECUTOR = Executors.newFixedThreadPool(
|
||||||
|
Runtime.getRuntime().availableProcessors(),
|
||||||
|
runnable -> {
|
||||||
|
Thread thread = new Thread(runnable);
|
||||||
|
thread.setName("ai-cam-image-processor-" + thread.getId());
|
||||||
|
thread.setDaemon(true);
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SourceMapper sourceMapper;
|
private SourceMapper sourceMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -97,15 +110,20 @@ public class SourceRepository {
|
|||||||
// 2. 构建图像处理配置
|
// 2. 构建图像处理配置
|
||||||
BceEnhancerConfig config = buildEnhancerConfig();
|
BceEnhancerConfig config = buildEnhancerConfig();
|
||||||
|
|
||||||
// 3. 为每张照片创建处理管线并执行
|
// 3. 并发处理所有照片
|
||||||
for (SourceEntity source : aiCamImages) {
|
List<CompletableFuture<Void>> futures = aiCamImages.stream()
|
||||||
try {
|
.map(source -> CompletableFuture.runAsync(() -> {
|
||||||
processSingleAiCamImage(source, config);
|
try {
|
||||||
} catch (Exception e) {
|
processSingleAiCamImage(source, config);
|
||||||
log.error("处理AI相机照片失败, sourceId: {}, error: {}", source.getId(), e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// 继续处理下一张照片,不中断整个流程
|
log.error("处理AI相机照片失败, sourceId: {}, error: {}", source.getId(), e.getMessage(), e);
|
||||||
}
|
// 继续处理下一张照片,不中断整个流程
|
||||||
}
|
}
|
||||||
|
}, IMAGE_PROCESS_EXECUTOR))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
// 4. 等待所有任务完成
|
||||||
|
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
||||||
|
|
||||||
log.info("AI相机照片处理完成, faceId: {}", faceId);
|
log.info("AI相机照片处理完成, faceId: {}", faceId);
|
||||||
|
|
||||||
@@ -139,7 +157,7 @@ public class SourceRepository {
|
|||||||
adapter = StorageFactory.get(configManager.getString("store_type"));
|
adapter = StorageFactory.get(configManager.getString("store_type"));
|
||||||
adapter.loadConfig(configManager.getObject("store_config_json", Map.class));
|
adapter.loadConfig(configManager.getObject("store_config_json", Map.class));
|
||||||
} catch (StorageUnsupportedException ignored) {
|
} catch (StorageUnsupportedException ignored) {
|
||||||
adapter = StorageFactory.use("assets");
|
adapter = StorageFactory.use("assets-ext");
|
||||||
}
|
}
|
||||||
context.setStorageAdapter(adapter);
|
context.setStorageAdapter(adapter);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user