1+ /* eslint-disable no-use-before-define */
12// Copyright (c) 2018 ml5
23//
34// This software is released under the MIT License.
45// https://opensource.org/licenses/MIT
5- import bodyPix from "../index" ;
6- import * as tf from "@tensorflow/tfjs" ;
76import * as tfBodyPix from "@tensorflow-models/body-pix" ;
7+ import bodyPix from "../index" ;
88
99jest . mock ( "@tensorflow-models/body-pix" ) ;
1010
@@ -14,6 +14,70 @@ const BODYPIX_DEFAULTS = {
1414 segmentationThreshold : 0.5 ,
1515} ;
1616
17+ const mockBodyPix = {
18+ mobileNet : {
19+ predict : jest . fn ( ) ,
20+ convToOutput : jest . fn ( ) ,
21+ dispose : jest . fn ( ) ,
22+ } ,
23+ predictForSegmentation : jest . fn ( ) ,
24+ predictForPartMap : jest . fn ( ) ,
25+ estimatePersonSegmentationActivation : jest . fn ( ) ,
26+ estimatePersonSegmentation : jest . fn ( ) ,
27+ estimatePartSegmentationActivation : jest . fn ( ) ,
28+ estimatePartSegmentation : jest . fn ( ) ,
29+ dispose : jest . fn ( ) ,
30+ } ;
31+
32+ describe ( "bodyPix" , ( ) => {
33+ let bp ;
34+
35+ beforeAll ( async ( ) => {
36+ tfBodyPix . load = jest . fn ( ) . mockResolvedValue ( mockBodyPix ) ;
37+ bp = await bodyPix ( ) ;
38+ } ) ;
39+
40+ afterAll ( async ( ) => {
41+ jest . clearAllMocks ( ) ;
42+ } ) ;
43+
44+ it ( "Should create bodyPix with all the defaults" , async ( ) => {
45+ expect ( bp . config . multiplier ) . toBe ( BODYPIX_DEFAULTS . multiplier ) ;
46+ expect ( bp . config . outputStride ) . toBe ( BODYPIX_DEFAULTS . outputStride ) ;
47+ expect ( bp . config . segmentationThreshold ) . toBe ( BODYPIX_DEFAULTS . segmentationThreshold ) ;
48+ } ) ;
49+
50+ it ( "segment calls segmentInternal and returns" , async ( ) => {
51+ bp . segmentInternal = jest . fn ( ) . mockResolvedValue ( {
52+ segmentation : {
53+ data : [ ] ,
54+ width : 200 ,
55+ height : 50 ,
56+ } ,
57+ } ) ;
58+ const img = await getImageData ( ) ;
59+ const results = await bp . segment ( img ) ;
60+ expect ( bp . segmentInternal ) . toHaveBeenCalledTimes ( 1 ) ;
61+ expect ( results . segmentation . width ) . toBe ( 200 ) ;
62+ expect ( results . segmentation . height ) . toBe ( 50 ) ;
63+ } ) ;
64+
65+ it ( "segmentWithParts calls segmentWithPartsInternal and returns" , async ( ) => {
66+ bp . segmentWithPartsInternal = jest . fn ( ) . mockResolvedValue ( {
67+ segmentation : {
68+ data : [ ] ,
69+ width : 200 ,
70+ height : 50 ,
71+ } ,
72+ } ) ;
73+ const img = await getImageData ( ) ;
74+ const results = await bp . segmentWithParts ( img ) ;
75+ expect ( bp . segmentWithPartsInternal ) . toHaveBeenCalledTimes ( 1 ) ;
76+ expect ( results . segmentation . width ) . toBe ( 200 ) ;
77+ expect ( results . segmentation . height ) . toBe ( 50 ) ;
78+ } ) ;
79+ } ) ;
80+
1781async function getImage ( ) {
1882 const img = new Image ( ) ;
1983 img . crossOrigin = true ;
@@ -49,43 +113,3 @@ async function getImageData() {
49113 const img = new ImageData ( arr , 200 ) ;
50114 return img ;
51115}
52-
53- describe ( "bodyPix" , ( ) => {
54- let bp ;
55-
56- beforeAll ( async ( ) => {
57- tfBodyPix . load = jest . fn ( ) . mockResolvedValue ( { } ) ;
58- bp = await bodyPix ( ) ;
59- } ) ;
60-
61- it ( "Should create bodyPix with all the defaults" , async ( ) => {
62- console . log ( bp ) ;
63- expect ( bp . config . multiplier ) . toBe ( BODYPIX_DEFAULTS . multiplier ) ;
64- expect ( bp . config . outputStride ) . toBe ( BODYPIX_DEFAULTS . outputStride ) ;
65- expect ( bp . config . segmentationThreshold ) . toBe ( BODYPIX_DEFAULTS . segmentationThreshold ) ;
66- } ) ;
67-
68- // it("segment takes ImageData", async () => {
69- // const img = await getImageData();
70- // const results = await bp.segment(img);
71- // // 200 * 50 == 10,000 * 4 == 40,000 the size of the array
72- // expect(results.segmentation.width).toBe(200);
73- // expect(results.segmentation.height).toBe(50);
74- // });
75-
76- // describe("segmentation", () => {
77- // it("Should segment an image of a Harriet Tubman with a width and height of 128", async () => {
78- // const img = await getImage();
79- // await bp.segment(img).then(results => {
80- // expect(results.segmentation.width).toBe(128);
81- // expect(results.segmentation.height).toBe(128);
82-
83- // expect(results.segmentation.width).toBe(128);
84- // expect(results.segmentation.height).toBe(128);
85-
86- // expect(results.segmentation.width).toBe(128);
87- // expect(results.segmentation.height).toBe(128);
88- // });
89- // });
90- // });
91- } ) ;
0 commit comments