RED-3813: image recategorization
added first simple clustering service
This commit is contained in:
parent
0cdd10e985
commit
18a8f5215f
@ -0,0 +1,61 @@
|
||||
package com.iqser.red.service.persistence.management.v1.processor.service.image;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math3.ml.clustering.Cluster;
|
||||
import org.apache.commons.math3.ml.clustering.DBSCANClusterer;
|
||||
import org.apache.commons.math3.ml.distance.DistanceMeasure;
|
||||
import org.apache.commons.math3.ml.distance.ManhattanDistance;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.FileManagementStorageService;
|
||||
import com.knecon.fforesight.tenantcommons.TenantContext;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ImageClusteringService {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final double eps = 26;
|
||||
private final int minPts = 3;
|
||||
|
||||
private final int fixedLength = 25;
|
||||
private final DistanceMeasure distanceMeasure = new ManhattanDistance();
|
||||
|
||||
@Autowired
|
||||
FileManagementStorageService fileManagementStorageService;
|
||||
|
||||
|
||||
public void clusterImages(String storageId) throws Exception {
|
||||
|
||||
DBSCANClusterer<Image> dbscanClusterer = new DBSCANClusterer<>(eps, minPts, distanceMeasure);
|
||||
|
||||
try (InputStream inputStream = fileManagementStorageService.getObject(TenantContext.getTenantId(), storageId)) {
|
||||
ImageServiceResponse imageServiceResponse = objectMapper.readValue(inputStream, ImageServiceResponse.class);
|
||||
List<ImageMetadata> imageMetadataList = imageServiceResponse.getData();
|
||||
List<Image> imageList = new ArrayList<>();
|
||||
for (ImageMetadata metadata : imageMetadataList) {
|
||||
Image image = new Image(metadata.getRepresentation(), fixedLength);
|
||||
imageList.add(image);
|
||||
}
|
||||
if (imageList.isEmpty()) {
|
||||
throw new IllegalArgumentException("The image list is empty. Unable to perform clustering.");
|
||||
}
|
||||
List<Cluster<Image>> clusters = dbscanClusterer.cluster(imageList);
|
||||
for (Cluster<Image> cluster : clusters) {
|
||||
List<Image> clusterImages = cluster.getPoints();
|
||||
log.info("cluster: {}", clusterImages.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -58,6 +58,7 @@ public class ImageMessageReceiver {
|
||||
String dossierId = (String) imageResponse.get("dossierId");
|
||||
String fileId = (String) imageResponse.get("fileId");
|
||||
addFileIdToTrace(fileId);
|
||||
|
||||
log.warn("Received message from {} for dossierId {} and fileId {}", MessagingConfiguration.IMAGE_SERVICE_DLQ, dossierId, fileId);
|
||||
fileStatusProcessingUpdateService.analysisFailed(dossierId,
|
||||
fileId,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user