-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSV export strategy for Rectangle annotations (#122)
* CSV export for specific format The CSV file can be used with the tooling mentioned here: https://apple.github.io/turicreate/docs/userguide/object_detection/data-preparation.html * Added com.opencsv to module-info * CSVSaveStrategy tests * Made constant private * Updated license headers --------- Co-authored-by: Markus Fleischhacker <markus.fleischhacker28+git@gmail.com>
- Loading branch information
Showing
7 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/com/github/mfl28/boundingboxeditor/model/io/CSVSaveStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2024 Markus Fleischhacker <markus.fleischhacker28@gmail.com> | ||
* | ||
* This file is part of Bounding Box Editor | ||
* | ||
* Bounding Box Editor is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Bounding Box Editor is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Bounding Box Editor. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.github.mfl28.boundingboxeditor.model.io; | ||
|
||
import com.github.mfl28.boundingboxeditor.model.data.*; | ||
import com.github.mfl28.boundingboxeditor.model.io.results.IOErrorInfoEntry; | ||
import com.github.mfl28.boundingboxeditor.model.io.results.ImageAnnotationExportResult; | ||
import com.opencsv.CSVWriterBuilder; | ||
import com.opencsv.ICSVWriter; | ||
import javafx.beans.property.DoubleProperty; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Saving-strategy to export annotations to a CSV file. | ||
* | ||
* The CSVSaveStrategy supports {@link BoundingBoxData} only. | ||
*/ | ||
public class CSVSaveStrategy implements ImageAnnotationSaveStrategy { | ||
private static final String FILE_NAME_SERIALIZED_NAME = "name"; | ||
private static final String ID_SERIALIZED_NAME = "id"; | ||
private static final String LABEL_SERIALIZED_NAME = "label"; | ||
private static final String MIN_X_SERIALIZED_NAME = "xMin"; | ||
private static final String MAX_X_SERIALIZED_NAME = "xMax"; | ||
private static final String MIN_Y_SERIALIZED_NAME = "yMin"; | ||
private static final String MAX_Y_SERIALIZED_NAME = "yMax"; | ||
private static final String UNSUPPORTED_BOUNDING_SHAPE = "CSV can export Rectangles only"; | ||
|
||
@Override | ||
public ImageAnnotationExportResult save(ImageAnnotationData annotations, Path destination, | ||
DoubleProperty progress) { | ||
final int totalNrAnnotations = annotations.imageAnnotations().size(); | ||
int nrProcessedAnnotations = 0; | ||
|
||
final List<IOErrorInfoEntry> errorEntries = new ArrayList<>(); | ||
|
||
try(ICSVWriter writer = new CSVWriterBuilder(Files.newBufferedWriter(destination, StandardCharsets.UTF_8)).build()) { | ||
String[] header = {FILE_NAME_SERIALIZED_NAME, ID_SERIALIZED_NAME, LABEL_SERIALIZED_NAME, MIN_X_SERIALIZED_NAME, MAX_X_SERIALIZED_NAME, MIN_Y_SERIALIZED_NAME, MAX_Y_SERIALIZED_NAME}; | ||
writer.writeNext(header); | ||
for (ImageAnnotation imageAnnotation : annotations.imageAnnotations()) { | ||
for (BoundingShapeData boundingShapeData : imageAnnotation.getBoundingShapeData()) { | ||
if (boundingShapeData instanceof BoundingBoxData boundingBoxData) { | ||
double xMin = imageAnnotation.getImageMetaData().getImageWidth() * boundingBoxData.getXMinRelative(); | ||
double xMax = imageAnnotation.getImageMetaData().getImageWidth() * boundingBoxData.getXMaxRelative(); | ||
double yMin = imageAnnotation.getImageMetaData().getImageHeight() * boundingBoxData.getYMinRelative(); | ||
double yMax = imageAnnotation.getImageMetaData().getImageHeight() * boundingBoxData.getYMaxRelative(); | ||
String[] line = { imageAnnotation.getImageFileName(), String.valueOf(nrProcessedAnnotations), boundingShapeData.getCategoryName(), String.valueOf((int) xMin), String.valueOf((int) xMax), String.valueOf((int) yMin), String.valueOf((int) yMax)}; | ||
writer.writeNext(line); | ||
} else { | ||
errorEntries.add(new IOErrorInfoEntry(imageAnnotation.getImageFileName(), UNSUPPORTED_BOUNDING_SHAPE)); | ||
} | ||
progress.set(1.0 * nrProcessedAnnotations++ / totalNrAnnotations); | ||
} | ||
} | ||
} catch(IOException e) { | ||
errorEntries.add(new IOErrorInfoEntry(destination.getFileName().toString(), e.getMessage())); | ||
} | ||
|
||
return new ImageAnnotationExportResult( | ||
errorEntries.isEmpty() ? totalNrAnnotations : 0, | ||
errorEntries | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/test/java/com/github/mfl28/boundingboxeditor/model/io/CSVSaveStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (C) 2024 Markus Fleischhacker <markus.fleischhacker28@gmail.com> | ||
* | ||
* This file is part of Bounding Box Editor | ||
* | ||
* Bounding Box Editor is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Bounding Box Editor is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Bounding Box Editor. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.github.mfl28.boundingboxeditor.model.io; | ||
|
||
import com.github.mfl28.boundingboxeditor.model.data.*; | ||
import com.github.mfl28.boundingboxeditor.model.io.results.ImageAnnotationExportResult; | ||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
import javafx.beans.property.SimpleDoubleProperty; | ||
import javafx.scene.paint.Color; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CSVSaveStrategyTest { | ||
|
||
/** | ||
* One image with two annotations gets exported to CSV. | ||
*/ | ||
@Test | ||
void multipleRectangles() throws IOException { | ||
ObjectCategory objectCategory = new ObjectCategory("category", Color.YELLOW); | ||
|
||
ImageAnnotation imageAnnotation = new ImageAnnotation(new ImageMetaData("sample.png", "folderName", "url", 100, 200, 0)); | ||
List<BoundingShapeData> boundingShapeDatas = new ArrayList<>(); | ||
boundingShapeDatas.add(new BoundingBoxData(objectCategory, 0,0,0.5,0.5, List.of("tag"))); | ||
boundingShapeDatas.add(new BoundingBoxData(objectCategory, 0,0,0.25,0.25, List.of("tag"))); | ||
imageAnnotation.setBoundingShapeData(boundingShapeDatas); | ||
|
||
ImageAnnotationData annotations = new ImageAnnotationData(List.of(imageAnnotation), | ||
Map.of("object", 1), | ||
Map.of("object", objectCategory)); | ||
Path destination = Jimfs.newFileSystem(Configuration.unix()).getPath("annotations.csv"); | ||
ImageAnnotationExportResult save = new CSVSaveStrategy().save(annotations, destination, new SimpleDoubleProperty(0)); | ||
assertTrue(save.getErrorTableEntries().isEmpty()); | ||
|
||
String content = Files.readString(destination); | ||
assertEquals(""" | ||
"name","id","label","xMin","xMax","yMin","yMax" | ||
"sample.png","0","category","0","50","0","100" | ||
"sample.png","1","category","0","25","0","50" | ||
""", content); | ||
} | ||
|
||
/** | ||
* Two images with each one annotation gets exported. | ||
*/ | ||
@Test | ||
void multipleImages() throws IOException { | ||
ObjectCategory objectCategory = new ObjectCategory("category", Color.YELLOW); | ||
|
||
ImageAnnotation imageAnnotation1 = new ImageAnnotation( | ||
new ImageMetaData("sample1.png", "folderName", "url", 100, 200, 0), | ||
List.of(new BoundingBoxData(objectCategory, 0,0,0.5,0.5, List.of("tag")))); | ||
|
||
ImageAnnotation imageAnnotation2 = new ImageAnnotation( | ||
new ImageMetaData("sample2.png", "folderName", "url", 100, 200, 0), | ||
List.of(new BoundingBoxData(objectCategory, 0,0,0.25,0.25, List.of("tag")))); | ||
|
||
ImageAnnotationData annotations = new ImageAnnotationData(List.of(imageAnnotation1, imageAnnotation2), | ||
Map.of("object", 1), | ||
Map.of("object", objectCategory)); | ||
Path destination = Jimfs.newFileSystem(Configuration.unix()).getPath("annotations.csv"); | ||
ImageAnnotationExportResult save = new CSVSaveStrategy().save(annotations, destination, new SimpleDoubleProperty(0)); | ||
assertTrue(save.getErrorTableEntries().isEmpty()); | ||
|
||
String content = Files.readString(destination); | ||
assertEquals(""" | ||
"name","id","label","xMin","xMax","yMin","yMax" | ||
"sample1.png","0","category","0","50","0","100" | ||
"sample2.png","1","category","0","25","0","50" | ||
""", content); | ||
} | ||
|
||
/** | ||
* One image with one annotation should be saved. The annotation uses an unsupported Bounding Shape, so a ErrorTableEntry is expected. | ||
*/ | ||
@Test | ||
void wrongBoundingShape() throws IOException { | ||
ObjectCategory objectCategory = new ObjectCategory("category", Color.YELLOW); | ||
|
||
ImageAnnotation imageAnnotation1 = new ImageAnnotation( | ||
new ImageMetaData("sample1.png", "folderName", "url", 100, 200, 0), | ||
List.of(new BoundingPolygonData(objectCategory, List.of(0.0, 0.0, 0.5, 0.5), List.of("tag")))); | ||
|
||
ImageAnnotationData annotations = new ImageAnnotationData(List.of(imageAnnotation1), | ||
Map.of("object", 1), | ||
Map.of("object", objectCategory)); | ||
Path destination = Jimfs.newFileSystem(Configuration.unix()).getPath("annotations.csv"); | ||
ImageAnnotationExportResult save = new CSVSaveStrategy().save(annotations, destination, new SimpleDoubleProperty(0)); | ||
assertEquals(1, save.getErrorTableEntries().size()); | ||
|
||
String content = Files.readString(destination); | ||
assertEquals(""" | ||
"name","id","label","xMin","xMax","yMin","yMax" | ||
""", content); | ||
} | ||
} |