@@ -6,37 +6,18 @@ import {
66 PolygonSpec ,
77 RectangleSpec ,
88} from "components/Annotation/Editor/type" ;
9- import { loadImage } from "components/UploadFile" ;
109import { DrawObject , DrawType } from "reduxes/annotation/type" ;
1110
1211import {
1312 AnnotationDaitaFormatter ,
1413 AnnotationFormatter ,
1514 AnnotationImportInfo ,
16- FileAndAnnotationImportInfo ,
1715 ID_2_LABEL_DAITA ,
18- LABEL_2_ID_DAITA ,
1916 PolygonShape ,
2017 Shape ,
2118 ShapeType ,
2219} from "./type" ;
2320
24- const readImage = async ( url : string ) => {
25- const response = await fetch ( url ) ;
26- const data = await response . blob ( ) ;
27- const metadata = {
28- type : "image/png" ,
29- } ;
30- const split = url . split ( "/" ) ;
31- const filename = split [ split . length - 1 ] ;
32- const file = new File ( [ data ] , filename , metadata ) ;
33- const image = await loadImage ( file ) ;
34- return {
35- height : image . image . height ,
36- width : image . image . width ,
37- image : file ,
38- } ;
39- } ;
4021export const importFileAndAnnotationFromDaitaAI = (
4122 file : File
4223) : Promise < AnnotationImportInfo > =>
@@ -69,42 +50,48 @@ export const importFileAndAnnotationFromDaitaAI = (
6950 } ) ;
7051 } ;
7152 } ) ;
72- export const importFileAndAnnotation = (
73- file : File
74- ) : Promise < FileAndAnnotationImportInfo > =>
75- new Promise < FileAndAnnotationImportInfo > ( ( resolve ) => {
76- const reader = new FileReader ( ) ;
77- reader . readAsText ( file ) ;
78- const drawObjectById : Record < string , DrawObject > = { } ;
79- reader . onloadend = async ( ) => {
80- const annotationFormatter : AnnotationFormatter = JSON . parse (
81- reader . result as string
82- ) ;
83- const property = await readImage ( annotationFormatter . image_path ) ;
53+ // export const importFileAndAnnotation = (
54+ // file: File
55+ // ): Promise<FileAndAnnotationImportInfo> =>
56+ // new Promise<FileAndAnnotationImportInfo>((resolve) => {
57+ // const reader = new FileReader();
58+ // reader.readAsText(file);
59+ // const drawObjectById: Record<string, DrawObject> = {};
60+ // reader.onloadend = async () => {
61+ // const annotationFormatter: AnnotationFormatter = JSON.parse(
62+ // reader.result as string
63+ // );
64+ // let property = null;
65+ // if (annotationFormatter.image_path) {
66+ // property = await readImage(annotationFormatter.image_path);
67+ // }
8468
85- annotationFormatter . annotations . forEach ( ( annotation ) => {
86- const drawObject = createPolygon ( { x : 0 , y : 0 } ) ;
87- const formatedPoints = annotation . coordinates . map ( ( arr ) => ( {
88- x : arr [ 0 ] ,
89- y : arr [ 1 ] ,
90- } ) ) ;
91- drawObjectById [ drawObject . data . id ] = {
92- type : drawObject . type ,
93- data : {
94- ...drawObject . data ,
95- points : formatedPoints ,
96- polygonState : { isFinished : true } ,
97- label : { label : ID_2_LABEL_DAITA [ annotation . category_id ] } ,
98- } ,
99- } ;
100- } ) ;
101- resolve ( {
102- annotationImagesProperty : property ,
103- drawObjectById,
104- } ) ;
105- } ;
106- } ) ;
107- export const importAnnotation = ( file : File ) : Promise < AnnotationImportInfo > =>
69+ // annotationFormatter.annotations.forEach((annotation) => {
70+ // const drawObject = createPolygon({ x: 0, y: 0 });
71+ // const formatedPoints = annotation.coordinates.map((arr) => ({
72+ // x: arr[0],
73+ // y: arr[1],
74+ // }));
75+ // drawObjectById[drawObject.data.id] = {
76+ // type: drawObject.type,
77+ // data: {
78+ // ...drawObject.data,
79+ // points: formatedPoints,
80+ // polygonState: { isFinished: true },
81+ // label: { label: ID_2_LABEL_DAITA[annotation.category_id] },
82+ // },
83+ // };
84+ // });
85+ // resolve({
86+ // annotationImagesProperty: property,
87+ // drawObjectById,
88+ // });
89+ // };
90+ // });
91+ export const importAnnotation = (
92+ file : File ,
93+ idToLabelStr : Record < string , string >
94+ ) : Promise < AnnotationImportInfo > =>
10895 new Promise < AnnotationImportInfo > ( ( resolve ) => {
10996 const reader = new FileReader ( ) ;
11097 reader . readAsText ( file ) ;
@@ -129,7 +116,7 @@ export const importAnnotation = (file: File): Promise<AnnotationImportInfo> =>
129116 isFinished : true ,
130117 isLineStrip : annotation . shapeType === ShapeType . LINE_STRIP ,
131118 } ,
132- label : { label : ID_2_LABEL_DAITA [ annotation . categoryId ] } ,
119+ label : { label : idToLabelStr [ annotation . categoryId ] } ,
133120 } as PolygonSpec ,
134121 } ;
135122 } else if ( annotation . shapeType === ShapeType . RECTANGLE ) {
@@ -140,7 +127,7 @@ export const importAnnotation = (file: File): Promise<AnnotationImportInfo> =>
140127 data : {
141128 ...drawObject . data ,
142129 ...spec ,
143- label : { label : ID_2_LABEL_DAITA [ annotation . categoryId ] } ,
130+ label : { label : idToLabelStr [ annotation . categoryId ] } ,
144131 } as RectangleSpec ,
145132 } ;
146133 } else if ( annotation . shapeType === ShapeType . ELLIPSE ) {
@@ -151,7 +138,7 @@ export const importAnnotation = (file: File): Promise<AnnotationImportInfo> =>
151138 data : {
152139 ...drawObject . data ,
153140 ...spec ,
154- label : { label : ID_2_LABEL_DAITA [ annotation . categoryId ] } ,
141+ label : { label : idToLabelStr [ annotation . categoryId ] } ,
155142 } as EllipseSpec ,
156143 } ;
157144 }
@@ -164,7 +151,8 @@ export const importAnnotation = (file: File): Promise<AnnotationImportInfo> =>
164151 } ) ;
165152
166153export const convert = (
167- drawObjectById : Record < string , DrawObject >
154+ drawObjectById : Record < string , DrawObject > ,
155+ labelStrToId : Record < string , string >
168156) : Shape [ ] => {
169157 const shapes : Shape [ ] = [ ] ;
170158 Object . entries ( drawObjectById ) . forEach ( ( [ , value ] ) => {
@@ -176,40 +164,42 @@ export const convert = (
176164 value . type === DrawType . POLYGON
177165 ? ShapeType . POLYGON
178166 : ShapeType . LINE_STRIP ,
179- categoryId : LABEL_2_ID_DAITA [ label . label ] ,
167+ categoryId : labelStrToId [ label . label ] ,
180168 } ) ;
181169 } else if ( value . type === DrawType . RECTANGLE ) {
182170 const { width, height, x, y, label } = value . data as RectangleSpec ;
183171 shapes . push ( {
184172 shapeSpec : { width, height, x, y } ,
185173 shapeType : ShapeType . RECTANGLE ,
186- categoryId : LABEL_2_ID_DAITA [ label . label ] ,
174+ categoryId : labelStrToId [ label . label ] ,
187175 } ) ;
188176 } else if ( value . type === DrawType . ELLIPSE ) {
189177 const { x, y, radiusX, radiusY, label } = value . data as EllipseSpec ;
190178 shapes . push ( {
191179 shapeSpec : { x, y, radiusX, radiusY } ,
192180 shapeType : ShapeType . ELLIPSE ,
193- categoryId : LABEL_2_ID_DAITA [ label . label ] ,
181+ categoryId : labelStrToId [ label . label ] ,
194182 } ) ;
195183 }
196184 } ) ;
197185 return shapes ;
198186} ;
199187export const exportAnnotationToJson = (
200- drawObjectById : Record < string , DrawObject >
188+ drawObjectById : Record < string , DrawObject > ,
189+ labelStrToId : Record < string , string >
201190) => {
202- const shapes : Shape [ ] = convert ( drawObjectById ) ;
203- const annotationFormatter : AnnotationDaitaFormatter = {
191+ const shapes : Shape [ ] = convert ( drawObjectById , labelStrToId ) ;
192+ const annotationDaitaFormatter : AnnotationDaitaFormatter = {
204193 shapes,
205194 } ;
206- return annotationFormatter ;
195+ return annotationDaitaFormatter ;
207196} ;
208197export const exportAnnotation = (
209198 drawObjectById : Record < string , DrawObject > ,
210- imageName : string
199+ imageName : string ,
200+ labelStrToId : Record < string , string >
211201) => {
212- const json = exportAnnotationToJson ( drawObjectById ) ;
202+ const json = exportAnnotationToJson ( drawObjectById , labelStrToId ) ;
213203 const jsonString = `data:text/json;chatset=utf-8,${ encodeURIComponent (
214204 JSON . stringify ( json , null , 2 )
215205 ) } `;
0 commit comments