@@ -13,7 +13,6 @@ export default class MediaProcessor {
1313 mean : number [ ] = [ 0 , 0 , 0 ] ;
1414 std : number [ ] = [ 1 , 1 , 1 ] ;
1515 bgr : boolean = false ;
16- result : Float32Array | number [ ] = [ ] ;
1716 pixelWidth : number = 1 ;
1817 pixelHeight : number = 1 ;
1918 inputFeed : InputFeed [ ] = [ ] ;
@@ -45,17 +44,13 @@ export default class MediaProcessor {
4544 targetShape : [ 1 , fc , fh , fw ]
4645 } ;
4746
48- if ( this . result . length === 0 ) {
49- const [ , c , h , w ] = params . targetShape ;
50- // 计算确定targetShape所需Float32Array占用空间
51- this . result = new Float32Array ( h * w * c ) ;
52- }
5347 return this . fromPixels ( input , params ) || [ ] ;
5448 }
5549
5650 fromPixels ( pixels , opt ) : InputFeed [ ] {
5751 let data : ImageData | number [ ] | Float32Array = [ ] ;
5852 const imageDataInfo = {
53+ gapFillWith : opt . gapFillWith ,
5954 dx : 0 ,
6055 dy : 0 ,
6156 dWidth : opt . targetSize . width ,
@@ -76,21 +71,22 @@ export default class MediaProcessor {
7671 this . pixelWidth = pixels . width ;
7772 this . pixelHeight = pixels . height ;
7873
79- this . fitToTargetSize ( pixels , opt ) ;
80- data = this . getImageData ( imageDataInfo ) ;
8174
75+ this . fitToTargetSize ( pixels , imageDataInfo , env . get ( 'webgl_feed_process' ) ) ;
76+ data = this . getImageData ( imageDataInfo ) ;
8277 // process imageData in webgl
8378 if ( env . get ( 'webgl_feed_process' ) ) {
8479 data = Float32Array . from ( ( data as ImageData ) . data ) ;
8580 return [ {
8681 data,
87- shape : [ 1 , 4 , opt . targetShape [ 2 ] , opt . targetShape [ 3 ] ] ,
82+ shape : [ 1 , 1 , imageDataInfo . dHeight , imageDataInfo . dWidth ] ,
8883 name : 'image' ,
8984 persistable : true
9085 } ] as InputFeed [ ] ;
9186 }
9287
93- data = this . allReshapeToRGB ( data , opt ) as number [ ] ;
88+
89+ data = this . allReshapeToRGB ( data , opt ) as Float32Array ;
9490 return [ {
9591 data,
9692 shape : opt . targetShape || opt . shape ,
@@ -113,7 +109,7 @@ export default class MediaProcessor {
113109 const { mean, std, targetShape, bgr } = opt ;
114110 const [ , c , h , w ] = targetShape ;
115111 const data = imageData . data || imageData ;
116- const result = this . result ;
112+ const result = new Float32Array ( h * w * c ) ;
117113 let offset = 0 ;
118114 // 将数据映射为0~1, 1:映射为-1~1之间
119115 const normalizeType = 0 ;
@@ -138,30 +134,47 @@ export default class MediaProcessor {
138134 /**
139135 * 缩放成目标尺寸并居中
140136 */
141- fitToTargetSize ( image , params ) {
137+ fitToTargetSize ( image , imageDataInfo , inGPU = false ) {
142138 // 目标尺寸
143- const targetWidth = params . targetSize . width ;
144- const targetHeight = params . targetSize . height ;
145- this . targetContext . canvas . width = targetWidth ;
146- this . targetContext . canvas . height = targetHeight ;
147- this . targetContext . fillStyle = params . gapFillWith ;
148- this . targetContext . fillRect ( 0 , 0 , targetHeight , targetWidth ) ;
139+ const targetWidth = imageDataInfo . dWidth ;
140+ const targetHeight = imageDataInfo . dHeight ;
141+
142+ let canvasWidth = inGPU ? this . pixelWidth : targetWidth ;
143+ let canvasHeight = inGPU ? this . pixelHeight : targetHeight ;
149144 // 缩放后的宽高
150- let sw = targetWidth ;
151- let sh = targetHeight ;
145+ let sw = inGPU ? this . pixelWidth : targetWidth ;
146+ let sh = inGPU ? this . pixelHeight : targetHeight ;
152147 let x = 0 ;
153148 let y = 0 ;
154149 // target的长宽比大些 就把原图的高变成target那么高
155150 if ( targetWidth / targetHeight * this . pixelHeight / this . pixelWidth >= 1 ) {
156- sw = Math . round ( sh * this . pixelWidth / this . pixelHeight ) ;
157- x = Math . floor ( ( targetWidth - sw ) / 2 ) ;
151+ if ( inGPU ) {
152+ canvasWidth = Math . round ( sh * targetWidth / targetHeight ) ;
153+ x = Math . floor ( ( canvasWidth - sw ) / 2 ) ;
154+ }
155+ else {
156+ sw = Math . round ( sh * this . pixelWidth / this . pixelHeight ) ;
157+ x = Math . floor ( ( targetWidth - sw ) / 2 ) ;
158+ }
158159 }
159160 // target的长宽比小些 就把原图的宽变成target那么宽
160161 else {
161- sh = Math . round ( sw * this . pixelHeight / this . pixelWidth ) ;
162- y = Math . floor ( ( targetHeight - sh ) / 2 ) ;
162+ if ( inGPU ) {
163+ canvasHeight = Math . round ( sw * targetHeight / targetWidth ) ;
164+ y = Math . floor ( ( canvasHeight - sh ) / 2 ) ;
165+ }
166+ else {
167+ sh = Math . round ( sw * this . pixelHeight / this . pixelWidth ) ;
168+ y = Math . floor ( ( targetHeight - sh ) / 2 ) ;
169+ }
163170 }
164171
172+ imageDataInfo . dWidth = canvasWidth ;
173+ imageDataInfo . dHeight = canvasHeight ;
174+ this . targetContext . canvas . width = canvasWidth ;
175+ this . targetContext . canvas . height = canvasHeight ;
176+ this . targetContext . fillStyle = imageDataInfo . gapFillWith ;
177+ this . targetContext . fillRect ( 0 , 0 , canvasHeight , canvasWidth ) ;
165178 this . targetContext . drawImage ( image , x , y , sw , sh ) ;
166179 }
167180
0 commit comments