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
Address PR comments for no state and readme
  • Loading branch information
hartmannr76 committed Oct 5, 2022
commit 0502c3301b19d56f042946804288a4981519bbc1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ You can find examples of export files along with a description and schema on our
|:-------------:|:---:|:----:|:-------:|:--------:|:---------:|:----------:|
| **Point** | ☐ | ✗ | ☐ | ☐ | ☐ | ✗ |
| **Line** | ☐ | ✗ | ✗ | ✗ | ✗ | ✗ |
| **Rect** | ☐ | ✓ | | ☐ | ✓ | ✗ |
| **Rect** | ☐ | ✓ | | ☐ | ✓ | ✗ |
| **Polygon** | ☐ | ✗ | ☐ | ☐ | ✓ | ☐ |
| **Label** | ☐ | ✗ | ✗ | ✗ | ✗ | ✗ |

Expand Down
59 changes: 32 additions & 27 deletions src/logic/import/voc/VOCImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,71 @@ import {LabelUtil} from "../../../utils/LabelUtil";
import {AnnotationImporter} from '../AnnotationImporter';
import {LabelsSelector} from '../../../store/selectors/LabelsSelector';

export type FileParseResult = {
type FileParseResult = {
filename: string,
labeledBoxes: LabelRect[]
};
export type VOCImportResult = {
labelNames: LabelName[],

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

export class VOCImporter extends AnnotationImporter {
public static requiredKeys = ['annotation', 'filename', 'categories'];
private labelNames: Map<string, LabelName>;

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

Promise.all(this.loadAndParseFiles(filesData)).then(results => {
for (const result of results) {
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(this.labelNames.values())
Array.from(Object.values(results.labelNames))
);
}).catch((error: Error) => onFailure(error));
} catch (error) {
onFailure(error as Error)
}
}

private loadAndParseFiles(files: File[]): Promise<FileParseResult>[] {
private loadAndParseFiles(files: File[]): Promise<VOCImportResult> {
const parser = new DOMParser();
return files.map(fileData => fileData.text().then(text =>
this.parseDocumentIntoImageData(parser.parseFromString(text, 'application/xml'))
));

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)
);
}

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

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

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

private parseAnnotationsFromFileString(document: Document): LabelRect[] {
protected static parseAnnotationsFromFileString(document: Document, labelNames: Record<string, LabelName>): LabelRect[] {
return Array.from(document.getElementsByTagName('object')).map(d => {
const labelName = d.getElementsByTagName('name')[0].textContent;
const bbox = d.getElementsByTagName('bndbox')[0];
Expand All @@ -76,23 +82,22 @@ export class VOCImporter extends AnnotationImporter {
width: xmax - xmin,
};

if (!this.labelNames.has(labelName)) {
this.labelNames.set(labelName, LabelUtil.createLabelName(labelName));
if (!labelNames[labelName]) {
labelNames[labelName] = LabelUtil.createLabelName(labelName);
}

const labelId = this.labelNames.get(labelName).id;
const labelId = labelNames[labelName].id;

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

private static mapImageData(): Map<string, ImageData> {
private static mapImageData(): Record<string, ImageData> {
return LabelsSelector.getImagesData().reduce(
(c: Map<string, ImageData>, i: ImageData) => {
(c: Record<string, ImageData>, i: ImageData) => {
hartmannr76 marked this conversation as resolved.
Show resolved Hide resolved
c[i.fileData.name] = i;
return c;
},
{} as Map<string, ImageData>
}, {}
);
}
}