Skip to content

Commit

Permalink
Ignore files with known annotation extension when loading images (#104)
Browse files Browse the repository at this point in the history
* Ignore files with known annotation extension when loading images.

* Pre-filter image files to load.
  • Loading branch information
mfl28 authored Nov 11, 2023
1 parent ea7d111 commit 6ddbe9b
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.concurrent.Task;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ImageMetaDataLoadingService extends IoService<ImageMetaDataLoadingResult> {
private static final String[] ignoredExtensions = {".txt", ".json", ".xml", ".data"};
private final ObjectProperty<File> source = new SimpleObjectProperty<>(this, "source");
private final ObjectProperty<List<File>> imageFiles = new SimpleObjectProperty<>(this, "imageFiles");
private final BooleanProperty reload = new SimpleBooleanProperty(this, "reload");
Expand Down Expand Up @@ -68,11 +70,16 @@ protected ImageMetaDataLoadingResult call() throws Exception {
final List<IOErrorInfoEntry> errorInfoEntries =
Collections.synchronizedList(new ArrayList<>());

int totalNrOfFiles = imageFiles.get().size();
final List<File> filteredFiles = imageFiles.get().stream()
.filter(file -> !StringUtils.endsWithAny(file.getName().toLowerCase(Locale.ENGLISH), ignoredExtensions))
.toList();

int totalNrOfFiles = filteredFiles.size();
final AtomicInteger nrProcessedFiles = new AtomicInteger(0);

fileNameToMetaDataMap
.putAll(imageFiles.get().parallelStream().collect(HashMap::new, (map, item) -> {
.putAll(filteredFiles.parallelStream()
.collect(HashMap::new, (map, item) -> {
updateProgress(1.0 * nrProcessedFiles.incrementAndGet() / totalNrOfFiles, 1.0);
try {
map.put(item.getName(), ImageMetaData.fromFile(item));
Expand All @@ -82,7 +89,7 @@ protected ImageMetaDataLoadingResult call() throws Exception {
}, Map::putAll));

final List<File> validImageFiles =
imageFiles.get().stream().filter(item -> fileNameToMetaDataMap.containsKey(item.getName()))
filteredFiles.stream().filter(item -> fileNameToMetaDataMap.containsKey(item.getName()))
.toList();

return new ImageMetaDataLoadingResult(fileNameToMetaDataMap.size(), errorInfoEntries,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void onOpeningNewImageFolder_WhenBoundingBoxesExist_ShouldResetCorrectly(FxRobot

final IOErrorInfoEntry referenceErrorInfoEntry1 = new IOErrorInfoEntry("cameraman.tif",
"Unsupported image file format.");
final IOErrorInfoEntry referenceErrorInfoEntry2 = new IOErrorInfoEntry("no_image_file.txt",
final IOErrorInfoEntry referenceErrorInfoEntry2 = new IOErrorInfoEntry("no_image_file.pdf",
"Invalid image file.");

verifyThat(errorInfoEntries, Matchers.contains(referenceErrorInfoEntry1, referenceErrorInfoEntry2),
Expand Down
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 6ddbe9b

Please sign in to comment.