@@ -12,29 +12,29 @@ const img = cv.imread('lenna.png')
1212const augmenter = new ia.PerspectiveTransform(0.2)
1313* @example
1414// Run the augmenter once
15- const {image } = augmenter.runAugmenter(img)
16- cv.imread ('output.png', image );
15+ const {images } = augmenter.runAugmenter([ img] )
16+ cv.imwrite ('output.png', images[0] );
1717* @example
18- // Run the augmenter multiple times
19- augmenter.run({image, times: 10 });
20- // => [{ image : Mat }, { image : Mat } , ...]
18+ // Run the augmenter 4 times
19+ augmenter.run({images: [img, img, img, img] });
20+ // => { images : [ Mat, Mat, ...]}
2121* @example
2222// follow a point in the augmentation workflow
23- augmenter.run({image , points: [[25, 90], [12, 32]]})
24- // => [{ image : Mat, points: [<first point new position>, <second point new position>] }]
23+ augmenter.run({images: [img] , points: [[[ 25, 90], [12, 32] ]]})
24+ // => { images : [ Mat] , points: [[ <first point new position>, <second point new position>]] }
2525**/
2626class AbstractAugmenter extends Abstract {
2727 constructor ( opts ) {
2828 super ( opts )
2929 this . _augmenter = true ;
3030 }
3131 /**
32- * @typedef {AugmenterFormat | Image } OneRunOption
32+ * @typedef {AugmenterFormat | Images } OneRunOption
3333 **/
3434
3535 /**
3636 * @typedef {Object } AugmenterFormat
37- * @property {Image } image the image to augment
37+ * @property {Images } images the image to augment
3838 * @property {Array.<Point> } points the points to augment
3939 * @property {Array.<Box> } boxes bournding boxes to augment
4040 */
@@ -46,33 +46,41 @@ class AbstractAugmenter extends Abstract{
4646 */
4747 runAugmenter ( runOpts ) {
4848 let params1 ;
49- if ( runOpts && runOpts . image && this . backend . isImage ( runOpts . image ) ) {
49+ if ( runOpts && runOpts . images && this . backend . isImages ( runOpts . images ) ) {
5050 params1 = runOpts ;
51- } else if ( this . backend . isImage ( runOpts ) ) {
52- params1 = { image : runOpts } ;
51+ } else if ( this . backend . isImages ( runOpts ) ) {
52+ params1 = { images : runOpts } ;
5353 } else {
54- console . log ( runOpts . image , this . backend . key )
55- throw ( new Error ( 'runOnce must have an image in it' ) ) ;
54+ throw ( new Error ( 'runOnce must have images in it' ) ) ;
5655 }
57- const metadata = this . backend . getMetadata ( params1 . image ) ;
58-
59- const points = ( params1 . points || [ ] ) . map ( p => {
60- if ( Array . isArray ( p ) ) {
61- return this . backend . point ( p [ 0 ] , p [ 1 ] ) ;
62- }
63- return p ;
64- } ) ;
56+ const metadata = this . backend . getMetadata ( params1 . images ) ;
57+
6558 debug ( `buildHasard ${ this . _name } ` ) ;
6659
67- const o2 = Object . assign ( { } , { boxes : [ ] } , metadata , params1 , { points} ) ;
60+ const o2 = Object . assign ( { } , metadata , params1 ) ;
61+ const nImages = metadata . nImages ;
62+
6863 const params = this . buildHasard ( o2 ) ;
64+
6965 debug ( `runOnce ${ this . _name } ` ) ;
70- const resolved = hasard . isHasard ( params ) ? params . runOnce ( runOpts ) : params ;
66+ const resolved = [ ] ;
67+
68+ // every image hasard is generated independantly
69+ for ( var i = 0 ; i < nImages ; i ++ ) {
70+ const resolvedParams = hasard . isHasard ( params ) ? params . runOnce ( runOpts ) : params
71+ resolved . push ( resolvedParams ) ;
72+ }
7173 //console.log({resolved, params})
7274 debug ( `augment ${ this . _name } ` ) ;
7375 return Promise . resolve ( this . augment ( o2 , resolved , runOpts ) ) ;
7476 }
7577
78+ fromFilenames ( { filenames} ) {
79+ return this . backend . readImages ( filenames ) . then ( images => {
80+ this . runAugmenter ( { images : images } )
81+ } ) ;
82+ }
83+
7684 buildHasard ( o ) {
7785 return this . buildParams ( o ) ;
7886 }
@@ -98,19 +106,51 @@ class AbstractAugmenter extends Abstract{
98106 // return this.runAugmenter();
99107 // }
100108 // }
101- augment ( attr , opts ) {
102- this . checkParams ( opts ) ;
109+ augment ( attrs , opts , runOpts ) {
110+ opts . forEach ( o => {
111+ this . checkParams ( o ) ;
112+ } )
113+
114+ const res = this . backend . splitImages ( attrs . images ) . map ( ( image , index ) => {
115+ const points = ( ( attrs . points && attrs . points [ index ] ) || [ ] ) . map ( p => {
116+ if ( Array . isArray ( p ) ) {
117+ return this . backend . point ( p [ 0 ] , p [ 1 ] ) ;
118+ }
119+ return p ;
120+ } ) ;
121+
122+ const newAttrs = Object . assign (
123+ { } ,
124+ attrs ,
125+ { image} , { images : null } ,
126+ { points} ,
127+ { boxes : ( attrs . boxes && attrs . boxes [ index ] ) || [ ] }
128+ )
129+ return this . augmentOne (
130+ newAttrs ,
131+ opts [ index ] ,
132+ runOpts
133+ )
134+ } ) ;
135+
103136 return {
104- image : this . augmentImage ( attr , opts ) ,
105- boxes : this . augmentBoxes ( attr , opts ) ,
106- points : this . augmentPoints ( attr , opts )
137+ images : res . map ( r => r . image ) ,
138+ boxes : res . map ( r => r . boxes ) ,
139+ points : res . map ( r => r . points )
107140 } ;
108141 }
109-
110- augmentImage ( { image} ) {
142+ augmentOne ( attr , opts , runOpts ) {
143+ return {
144+ image : this . augmentImage ( attr , opts , runOpts ) ,
145+ boxes : this . augmentBoxes ( attr , opts , runOpts ) ,
146+ points : this . augmentPoints ( attr , opts , runOpts )
147+ } ;
148+ }
149+
150+ augmentImage ( { images} , opts , runOpts ) {
151+ // by default do nothing
111152 return image ;
112153 }
113-
114154 augmentPoints ( { points} ) {
115155 return points ;
116156 }
0 commit comments