@@ -21,7 +21,8 @@ const DEFAULTS = {
21
21
"multiplier" : 0.75 ,
22
22
"outputStride" : 16 ,
23
23
"segmentationThreshold" : 0.5 ,
24
- "palette" : BODYPIX_PALETTE
24
+ "palette" : BODYPIX_PALETTE ,
25
+ "returnTensors" : false ,
25
26
}
26
27
27
28
class BodyPix {
@@ -40,7 +41,8 @@ class BodyPix {
40
41
multiplier : options . multiplier || DEFAULTS . multiplier ,
41
42
outputStride : options . outputStride || DEFAULTS . outputStride ,
42
43
segmentationThreshold : options . segmentationThreshold || DEFAULTS . segmentationThreshold ,
43
- palette : options . palette || DEFAULTS . palette
44
+ palette : options . palette || DEFAULTS . palette ,
45
+ returnTensors : options . returnTensors || DEFAULTS . returnTensors
44
46
}
45
47
46
48
this . ready = callCallback ( this . loadModel ( ) , callback ) ;
@@ -126,7 +128,6 @@ class BodyPix {
126
128
this . config . outputStride = segmentationOptions . outputStride || this . config . outputStride ;
127
129
this . config . segmentationThreshold = segmentationOptions . segmentationThreshold || this . config . segmentationThreshold ;
128
130
129
- // const segmentation = await this.model.estimatePersonSegmentation(imgToSegment, this.config.outputStride, this.config.segmentationThreshold)
130
131
const segmentation = await this . model . estimatePartSegmentation ( imgToSegment , this . config . outputStride , this . config . segmentationThreshold ) ;
131
132
132
133
const bodyPartsMeta = this . bodyPartsSpec ( this . config . palette ) ;
@@ -153,13 +154,12 @@ class BodyPix {
153
154
result . raw . personMask = bp . toMaskImageData ( segmentation , false ) ;
154
155
result . raw . partMask = bp . toColoredPartImageData ( segmentation , colorsArray ) ;
155
156
156
- let normTensor = await tf . browser . fromPixels ( imgToSegment ) ;
157
-
158
157
const {
159
158
personMask,
160
159
backgroundMask,
161
160
partMask,
162
161
} = tf . tidy ( ( ) => {
162
+ let normTensor = tf . browser . fromPixels ( imgToSegment ) ;
163
163
// create a tensor from the input image
164
164
const alpha = tf . ones ( [ segmentation . height , segmentation . width , 1 ] ) . tile ( [ 1 , 1 , 1 ] ) . mul ( 255 )
165
165
normTensor = normTensor . concat ( alpha , 2 )
@@ -198,6 +198,17 @@ class BodyPix {
198
198
result . partMask = await this . convertToP5Image ( partMaskPixels , segmentation . width , segmentation . height )
199
199
}
200
200
201
+ if ( ! this . config . returnTensors ) {
202
+ personMask . dispose ( ) ;
203
+ backgroundMask . dispose ( ) ;
204
+ partMask . dispose ( ) ;
205
+ } else {
206
+ // return tensors
207
+ result . tensor . personMask = personMask ;
208
+ result . tensor . backgroundMask = backgroundMask ;
209
+ result . tensor . partMask = partMask ;
210
+ }
211
+
201
212
return result ;
202
213
203
214
}
@@ -309,12 +320,11 @@ class BodyPix {
309
320
// result.backgroundMask = bgMaskCanvas;
310
321
// result.featureMask = featureMaskCanvas;
311
322
312
- let normTensor = await tf . browser . fromPixels ( imgToSegment ) ;
313
-
314
323
const {
315
324
personMask,
316
325
backgroundMask
317
326
} = tf . tidy ( ( ) => {
327
+ let normTensor = tf . browser . fromPixels ( imgToSegment ) ;
318
328
// create a tensor from the input image
319
329
const alpha = tf . ones ( [ segmentation . height , segmentation . width , 1 ] ) . tile ( [ 1 , 1 , 1 ] ) . mul ( 255 )
320
330
normTensor = normTensor . concat ( alpha , 2 )
@@ -337,16 +347,25 @@ class BodyPix {
337
347
const personMaskPixels = await tf . browser . toPixels ( personMask ) ;
338
348
const bgMaskPixels = await tf . browser . toPixels ( backgroundMask ) ;
339
349
340
- // otherwise, return the pixels
341
- result . personMask = personMaskPixels ;
342
- result . backgroundMask = bgMaskPixels ;
343
-
344
350
// if p5 exists, convert to p5 image
345
351
if ( p5Utils . checkP5 ( ) ) {
346
352
result . personMask = await this . convertToP5Image ( personMaskPixels , segmentation . width , segmentation . height )
347
353
result . backgroundMask = await this . convertToP5Image ( bgMaskPixels , segmentation . width , segmentation . height )
354
+ } else {
355
+ // otherwise, return the pixels
356
+ result . personMask = personMaskPixels ;
357
+ result . backgroundMask = bgMaskPixels ;
348
358
}
349
359
360
+ if ( ! this . config . returnTensors ) {
361
+ personMask . dispose ( ) ;
362
+ backgroundMask . dispose ( ) ;
363
+ } else {
364
+ result . tensor . personMask = personMask ;
365
+ result . tensor . backgroundMask = backgroundMask ;
366
+ }
367
+
368
+
350
369
return result ;
351
370
352
371
}
0 commit comments