-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageService.java
131 lines (95 loc) · 3.98 KB
/
ImageService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package se.mow_e.services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import se.mow_e.models.Coordinate;
import se.mow_e.models.Mower;
import se.mow_e.repository.CoordinateRepo;
import se.mow_e.repository.MowerRepo;
import se.mow_e.utils.UtilImage;
import se.mow_e.websocket.messages.ImageMessage;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executor;
@Service
public class ImageService {
private final Logger log = LoggerFactory.getLogger(ImageService.class);
private final Map<Long, ByteBuffer[]> imageChunkList = new HashMap<>(); // TODO - Replace with CacheMap - AAAAAAA
@Autowired
private CoordinateRepo coordinateRepo;
@Qualifier("imageClassificationTaskExecutor")
@Autowired
private Executor executor;
@Autowired
private ImgClassificationService imgClassificationService;
@Autowired
private MowerRepo mowerRepo;
@Value("${storage.image-dir-path}")
public void setImageDir(String imageDirPath){
UtilImage.IMAGES_DIR = imageDirPath;
}
public void add(ImageMessage message) {
ByteBuffer[] chunks;
Long id = message.getId();
if (!imageChunkList.containsKey(id)) {
chunks = new ByteBuffer[message.getChunkAmount()];
imageChunkList.put(id, chunks);
} else {
chunks = imageChunkList.get(id);
}
chunks[message.getChunkOffset()] = message.getData();
if (allChunksReceived(chunks)) {
int length = 0;
for (ByteBuffer b : chunks) {
if (b != null) length += b.remaining();
}
ByteBuffer buffer = ByteBuffer.allocate(length);
for (ByteBuffer b : chunks) {
if (b != null) buffer.put(b);
}
imageChunkList.remove(id); // No memory leak in case someone stops the image transfer
UUID imageId = UUID.randomUUID();
Coordinate coordinate = coordinateRepo.findCoordinateByTempImageId(String.valueOf(message.getId()));
if (coordinate != null) {
coordinate.setImageId(imageId.toString());
try {
UtilImage.save(imageId.toString(), buffer);
} catch (IOException e) {
log.error("Error while saving image", e);
return;
}
Mower mower = mowerRepo.findMowerByMowerId(coordinate.getMowerId());
mower.addImage(imageId.toString());
coordinateRepo.save(coordinate);
mowerRepo.save(mower);
executor.execute(() -> {
String imgPath = UtilImage.IMAGES_DIR + imageId + UtilImage.IMAGE_FORMAT;
try {
// log.info("Started recognizing the objects from the image: " + imgPath );
Map<String, Float> labels = imgClassificationService.extractLabels(imgPath);
coordinate.setExtra(labels.toString());
coordinateRepo.save(coordinate);
log.info("Recognition successful for " + imgPath);
} catch (Exception e) {
log.error("Error during image classification process for " + imgPath, e);
}
log.info("Executor exists");
});
} else {
log.error("No coordinate found for attaching the image with id: " + message.getId());
}
}
}
private boolean allChunksReceived(ByteBuffer[] chunks) {
for (ByteBuffer b : chunks) {
if (b == null) return false;
}
return true;
}
}