Skip to content
Snippets Groups Projects
Unverified Commit 0a61842c authored by BARBIER Marc's avatar BARBIER Marc
Browse files

improved logging and better labeling for images

parent 90b7d188
Branches
No related tags found
No related merge requests found
......@@ -55,15 +55,17 @@ public final class ImageStore {
int imageId = imageCount++;
Number[][][] rawImage = ImageUtils.convertImageToRaw(tiffImage);
File rawOutput = new File(Settings.IMAGE_FOLDER + imageId + ".dat");
File pngOutput = new File(Settings.IMAGE_FOLDER + imageId + ".jpg");
File jpgOutput = new File(Settings.IMAGE_FOLDER + imageId + ".jpg");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(rawOutput));
oos.writeObject(rawImage);
oos.close();
ImageUtils.convertRawImageToJpg(rawImage, pngOutput);
ImageUtils.convertRawImageToJpg(rawImage, jpgOutput);
nameMap.put(imageId, label);
System.out.println("stored image " + jpgOutput.getAbsolutePath());
saveDatabase();
return imageId;
}
......
......@@ -43,6 +43,7 @@ public class ImageUtils {
Rasters rasters = image.getFileDirectories().get(0).readRasters();
System.out.println("image has " + rasters.getFieldTypes().length + " canals");
System.out.println("image type is " + rasters.getFieldTypes()[0]);
Number[][][] rawImage = new Number[rasters.getHeight()][rasters.getWidth()][rasters.getFieldTypes().length];
......
......@@ -7,11 +7,14 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -35,7 +38,6 @@ import be.uantwerpen.ldataminining.model.Table;
import be.uantwerpen.ldataminining.preprocessing.ArffDenseUtils;
import be.uantwerpen.ldataminining.preprocessing.AutoConvertCSVToArff;
import be.uantwerpen.ldataminining.preprocessing.CSVUtils;
import be.uantwerpen.ldataminining.utils.CollectionUtils;
import be.uantwerpen.mime_webapp.Settings;
import be.uantwerpen.mime_webapp.dao.ProjectRepository;
import be.uantwerpen.mime_webapp.model.FileItem;
......@@ -48,13 +50,14 @@ public class ImageController extends AbstractController {
@PostMapping("/image/store")
public ResponseEntity<Integer[]> store(
@RequestParam("files") MultipartFile[] files
) {
) throws IOException {
List<Integer> fileIds = new ArrayList<>(files.length);
List<Future<?>> futures = new LinkedList<>();
//thread pool
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for(MultipartFile file : files) {
executor.submit(() -> {
Future<?> thread = executor.submit(() -> {
//this function is slow so we are using multiple threads for it
try {
fileIds.add(ImageStore.store(file));
......@@ -62,6 +65,7 @@ public class ImageController extends AbstractController {
e.printStackTrace();
}
});
futures.add(thread);
}
//waiting for all threads to finish
......@@ -71,6 +75,16 @@ public class ImageController extends AbstractController {
executor.awaitTermination(Long.MAX_VALUE, java.util.concurrent.TimeUnit.DAYS);
} catch (InterruptedException e) {}
}
//rethrow exceptions during thread execution
for(Future<?> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new IOException(e);
}
}
return ResponseEntity.ok(fileIds.toArray(new Integer[0]));
}
......@@ -169,8 +183,7 @@ public class ImageController extends AbstractController {
Table table = new Table();
//TODO: check if this is the right order
String[] labels = { "A", "R", "G", "B" };
table.addRow(Arrays.asList(labels).subList(0, result.length));
table.addRow(getLabels(result.length));
for(int i = 0; i < result[0].length; i++) {
List<String> row = new ArrayList<>(result.length);
......@@ -198,6 +211,16 @@ public class ImageController extends AbstractController {
}
}
// 4 : ARGB
// 3 : RGB
// these two don't really make sens but it safer to have them and i don't have other labels for them
// 2 : GB
// 1 : B
private List<String> getLabels(int length) {
String[] labels = { "A", "R", "G", "B" };
return Arrays.asList(labels).subList(length == 4 ? 0: 1, length);
}
@PostMapping("/image/cluster")
public ResponseEntity<String> kMeans(
@RequestParam("ids") int[] ids,
......@@ -237,9 +260,7 @@ public class ImageController extends AbstractController {
Table table = new Table();
//TODO: check if this is the right order
String[] labels = { "A", "R", "G", "B" };
table.addRow(Arrays.asList(labels).subList(0, result.length));
table.addRow(getLabels(result.length));
for(int i = 0; i < result[0].length; i++) {
List<String> row = new ArrayList<>(result.length);
......@@ -302,7 +323,7 @@ public class ImageController extends AbstractController {
new File(Settings.IMAGE_CACHE).mkdirs();
File tmpimage = new File(Settings.IMAGE_CACHE + currentItem.getFilename() + imageid + ":" + canalId + ".jpg");
if(tmpimage.exists()) {
return ResponseEntity.ok(Files.readAllBytes(tmpimage.toPath()));
//return ResponseEntity.ok(Files.readAllBytes(tmpimage.toPath()));
}
Table table = ArffDenseUtils.loadArff(currentItem.getFile());
......@@ -332,7 +353,7 @@ public class ImageController extends AbstractController {
private static Byte[] getColor(double val) {
//setting the value as seed so each value correspond to one color
final Random random = new Random((long)(val * 10000));
final Random random = new Random((long)(val * 100));
Byte[] colors = { (byte)random.nextInt(255), (byte)random.nextInt(255), (byte)random.nextInt(255) };
return colors;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment