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 1 commit
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
Prev Previous commit
Next Next commit
added test for VOCImporter parseAnnotationsFromFileString
  • Loading branch information
hartmannr76 committed Oct 6, 2022
commit e4c250d7d16c64907b71b07de2c82f2976a8aad5
137 changes: 137 additions & 0 deletions src/logic/__tests__/import/voc/VOCImporter.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

import { ImageData, LabelName, LabelRect} from '../../../../store/labels/types';
import { AcceptedFileType } from '../../../../data/enums/AcceptedFileType';
import { v4 as uuidv4 } from 'uuid';
import { VOCImporter } from '../../../import/voc/VOCImporter';
import { isEqual } from 'lodash';

const getDummyImageData = (fileName: string): ImageData => {
return {
id: uuidv4(),
fileData: new File([''], fileName, { type: AcceptedFileType.IMAGE }),
loadStatus: true,
labelRects: [],
labelPoints: [],
labelLines: [],
labelPolygons: [],
labelNameIds: [],
isVisitedByYOLOObjectDetector: false,
isVisitedBySSDObjectDetector: false,
isVisitedByPoseDetector: false
};
};

const getDummyFileData = (fileName: string): File => {
return new File([''], fileName, { type: AcceptedFileType.TEXT });
};

class TestableVOCImporter extends VOCImporter {
public static testableParseAnnotationsFromFileString(document: Document, labelNames: Record<string, LabelName>)
:[LabelRect[], Record<string, LabelName>] {
return TestableVOCImporter.parseAnnotationsFromFileString(document, labelNames);
}
}

const parser = new DOMParser();
const validTestDocument = parser.parseFromString(
`
<annotation>
<filename>test1.jpeg</filename>
<path>\\some-test-path\\test1.jpeg</path>
<size>
<width>100</width>
<height>200</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>annotation1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>10</xmin>
<ymin>20</ymin>
<xmax>30</xmax>
<ymax>50</ymax>
</bndbox>
</object>
<object>
<name>annotation2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>20</xmin>
<ymin>30</ymin>
<xmax>30</xmax>
<ymax>50</ymax>
</bndbox>
</object>
</annotation>
`,
'application/xml'
);

describe('VOCImporter parseAnnotationsFromFileString method', () => {
it('should return correctly for multiple annotations', () => {
// given
const emptyRecordSet: Record<string, LabelName> = {};

// when
const [annotations, newRecordSet] = TestableVOCImporter.testableParseAnnotationsFromFileString(validTestDocument, emptyRecordSet);

// then
expect(Object.keys(emptyRecordSet).length).toBe(0);
expect(newRecordSet).toEqual(expect.objectContaining({
'annotation1': expect.objectContaining({ name: 'annotation1'}),
'annotation2': expect.objectContaining({ name: 'annotation2'}),
}))
expect(annotations).toEqual([
expect.objectContaining({
rect: {
x: 10,
y: 20,
height: 30,
width: 20
},
labelId: newRecordSet['annotation1'].id
}),
expect.objectContaining({
rect: {
x: 20,
y: 30,
height: 20,
width: 10
},
labelId: newRecordSet['annotation2'].id
})
]);
});

it('should reuse existing labels', () => {
// given
const existingRecordSet: Record<string, LabelName> = {
'annotation2': {
id: 'foobar',
name: 'annotation2'
}
};

// when
const [annotations, newRecordSet] = TestableVOCImporter.testableParseAnnotationsFromFileString(validTestDocument, existingRecordSet);

// then
expect(Object.keys(existingRecordSet).length).toBe(1);
expect(newRecordSet).toEqual(expect.objectContaining({
'annotation1': expect.objectContaining({ name: 'annotation1' }),
'annotation2': expect.objectContaining({ name: 'annotation2', id: 'foobar' }),
}));
expect(annotations.length).toBe(2);
expect(annotations).toEqual(expect.arrayContaining([
expect.objectContaining({
labelId: 'foobar'
})
]));
});
});
15 changes: 7 additions & 8 deletions src/logic/import/voc/VOCImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class VOCImporter extends AnnotationImporter {
private loadAndParseFiles(files: File[]): Promise<VOCImportResult> {
const parser = new DOMParser();

return Promise.all(files.map(file => file.text())).then(textFiles =>
textFiles.reduce((current, fileData) =>
VOCImporter.parseDocumentIntoImageData(parser.parseFromString(fileData, 'application/xml'), current),
return Promise.all(files.map((file: File) => file.text())).then((fileTexts: string[]) =>
fileTexts.reduce((current: VOCImportResult, fileText: string) =>
VOCImporter.parseDocumentIntoImageData(parser.parseFromString(fileText, 'application/xml'), current),
{
labelNames: {},
fileParseResults: [],
Expand All @@ -69,7 +69,7 @@ export class VOCImporter extends AnnotationImporter {

protected static parseAnnotationsFromFileString(document: Document, labelNames: Record<string, LabelName>):
[LabelRect[], Record<string, LabelName>] {
const newLabelNames: Record<string, LabelName> = Object.assign(labelNames);
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];
Expand All @@ -89,16 +89,15 @@ export class VOCImporter extends AnnotationImporter {
}

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) => {
c[i.fileData.name] = i;
return c;
(imageDataMap: Record<string, ImageData>, imageData: ImageData) => {
imageDataMap[imageData.fileData.name] = imageData;
return imageDataMap;
}, {}
);
}
Expand Down