Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial working version of VOC XML import #280

Merged
merged 7 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ You can find examples of export files along with a description and schema on our
|:-------------:|:---:|:----:|:-------:|:--------:|:---------:|:----------:|
| **Point** | ☐ | ✗ | ☐ | ☐ | ☐ | ✗ |
| **Line** | ☐ | ✗ | ✗ | ✗ | ✗ | ✗ |
| **Rect** | ☐ | ✓ | | ☐ | ✓ | ✗ |
| **Rect** | ☐ | ✓ | | ☐ | ✓ | ✗ |
| **Polygon** | ☐ | ✗ | ☐ | ☐ | ✓ | ☐ |
| **Label** | ☐ | ✗ | ✗ | ✗ | ✗ | ✗ |

Expand Down
4 changes: 4 additions & 0 deletions src/data/ImportFormatData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const ImportFormatData: ImportFormatDataMap = {
{
type: AnnotationFormatType.YOLO,
label: 'Multiple files in YOLO format along with labels names definition - labels.txt file.'
},
{
type: AnnotationFormatType.VOC,
label: 'Multiple files in VOC XML format.'
}
],
[LabelType.POINT]: [],
Expand Down
3 changes: 2 additions & 1 deletion src/data/ImporterSpecData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {AnnotationFormatType} from './enums/AnnotationFormatType';
import {AnnotationImporter} from '../logic/import/AnnotationImporter';
import {COCOImporter} from '../logic/import/coco/COCOImporter';
import {YOLOImporter} from '../logic/import/yolo/YOLOImporter';
import {VOCImporter} from '../logic/import/voc/VOCImporter';

export type ImporterSpecDataMap = Record<AnnotationFormatType, typeof AnnotationImporter>;

Expand All @@ -11,6 +12,6 @@ export const ImporterSpecData: ImporterSpecDataMap = {
[AnnotationFormatType.CSV]: undefined,
[AnnotationFormatType.JSON]: undefined,
[AnnotationFormatType.VGG]: undefined,
[AnnotationFormatType.VOC]: undefined,
[AnnotationFormatType.VOC]: VOCImporter,
[AnnotationFormatType.YOLO]: YOLOImporter
}
3 changes: 2 additions & 1 deletion src/data/enums/AcceptedFileType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum AcceptedFileType {
IMAGE = 'image/jpeg, image/png',
TEXT = 'text/plain',
JSON = 'application/json'
JSON = 'application/json',
XML = 'application/xml',
}
105 changes: 105 additions & 0 deletions src/logic/import/voc/VOCImporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {ImageData, LabelName, LabelRect} from '../../../store/labels/types';
import {LabelUtil} from "../../../utils/LabelUtil";
import {AnnotationImporter} from '../AnnotationImporter';
import {LabelsSelector} from '../../../store/selectors/LabelsSelector';

type FileParseResult = {
filename: string,
labeledBoxes: LabelRect[]
};

type VOCImportResult = {
labelNames: Record<string, LabelName>,
fileParseResults: FileParseResult[],
};

export class VOCImporter extends AnnotationImporter {
public import(
filesData: File[],
onSuccess: (imagesData: ImageData[], labelNames: LabelName[]) => any,
onFailure: (error?:Error) => any
): void {
try {
const inputImagesData: Record<string, ImageData> = VOCImporter.mapImageData();

this.loadAndParseFiles(filesData).then(results => {
for (const result of results.fileParseResults) {
if (inputImagesData[result.filename]) {
inputImagesData[result.filename].labelRects = result.labeledBoxes;
}
}

onSuccess(
Array.from(Object.values(inputImagesData)),
Array.from(Object.values(results.labelNames))
);
}).catch((error: Error) => onFailure(error));
} catch (error) {
onFailure(error as Error)
}
}

private loadAndParseFiles(files: File[]): Promise<VOCImportResult> {
const parser = new DOMParser();

return Promise.all(files.map(file => file.text())).then(textFiles =>
hartmannr76 marked this conversation as resolved.
Show resolved Hide resolved
textFiles.reduce((current, fileData) =>
VOCImporter.parseDocumentIntoImageData(parser.parseFromString(fileData, 'application/xml'), current),
{
labelNames: {},
fileParseResults: [],
} as VOCImportResult)
);
}

protected static parseDocumentIntoImageData(document: Document, { fileParseResults, labelNames }: VOCImportResult): VOCImportResult {
const root = document.getElementsByTagName('annotation')[0];
const filename = root.getElementsByTagName('filename')[0].textContent;

const [labeledBoxes, newLabelNames] = this.parseAnnotationsFromFileString(document, labelNames);

return {
labelNames: newLabelNames,
fileParseResults: fileParseResults.concat({
filename,
labeledBoxes
}),
};
}

protected static parseAnnotationsFromFileString(document: Document, labelNames: Record<string, LabelName>):
[LabelRect[], Record<string, LabelName>] {
const newLabelNames: Record<string, LabelName> = Object.assign(labelNames);
return [Array.from(document.getElementsByTagName('object')).map(d => {
const labelName = d.getElementsByTagName('name')[0].textContent;
const bbox = d.getElementsByTagName('bndbox')[0];
const xmin = parseInt(bbox.getElementsByTagName('xmin')[0].textContent);
const xmax = parseInt(bbox.getElementsByTagName('xmax')[0].textContent);
const ymin = parseInt(bbox.getElementsByTagName('ymin')[0].textContent);
const ymax = parseInt(bbox.getElementsByTagName('ymax')[0].textContent);
const rect = {
x: xmin,
y: ymin,
height: ymax - ymin,
width: xmax - xmin,
};

if (!newLabelNames[labelName]) {
newLabelNames[labelName] = LabelUtil.createLabelName(labelName);
}

const labelId = newLabelNames[labelName].id;

return LabelUtil.createLabelRect(labelId, rect);
}), newLabelNames];
}

private static mapImageData(): Record<string, ImageData> {
return LabelsSelector.getImagesData().reduce(
(c: Record<string, ImageData>, i: ImageData) => {
hartmannr76 marked this conversation as resolved.
Show resolved Hide resolved
c[i.fileData.name] = i;
return c;
}, {}
);
}
}
3 changes: 2 additions & 1 deletion src/views/PopupView/ImportLabelPopup/ImportLabelPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const ImportLabelPopup: React.FC<IProps> = (
const { getRootProps, getInputProps } = useDropzone({
accept: {
"application/json": [".json" ],
"text/plain": [".txt"]
"text/plain": [".txt"],
"application/xml": [".xml"],
},
multiple: true,
onDrop: (acceptedFiles) => {
Expand Down