Skip to content

Commit f90dad3

Browse files
committed
Add background-removal unit test
1 parent 9c2b8d6 commit f90dad3

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tests/asset_cache.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const TEST_IMAGES = Object.freeze({
2424
book_cover: BASE_URL + "book-cover.png",
2525
corgi: BASE_URL + "corgi.jpg",
2626
man_on_car: BASE_URL + "young-man-standing-and-leaning-on-car.jpg",
27+
portrait_of_woman: BASE_URL + "portrait-of-woman_small.jpg",
2728
});
2829

2930
const TEST_AUDIOS = {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { pipeline, BackgroundRemovalPipeline, RawImage } from "../../src/transformers.js";
2+
3+
import { MAX_MODEL_LOAD_TIME, MAX_TEST_EXECUTION_TIME, MAX_MODEL_DISPOSE_TIME, DEFAULT_MODEL_OPTIONS } from "../init.js";
4+
import { load_cached_image } from "../asset_cache.js";
5+
6+
const PIPELINE_ID = "background-removal";
7+
8+
export default () => {
9+
describe("Background Removal", () => {
10+
describe("Portrait Segmentation", () => {
11+
const model_id = "Xenova/modnet";
12+
/** @type {BackgroundRemovalPipeline} */
13+
let pipe;
14+
beforeAll(async () => {
15+
pipe = await pipeline(PIPELINE_ID, model_id, DEFAULT_MODEL_OPTIONS);
16+
}, MAX_MODEL_LOAD_TIME);
17+
18+
it("should be an instance of BackgroundRemovalPipeline", () => {
19+
expect(pipe).toBeInstanceOf(BackgroundRemovalPipeline);
20+
});
21+
22+
it(
23+
"single",
24+
async () => {
25+
const image = await load_cached_image("portrait_of_woman");
26+
27+
const output = await pipe(image);
28+
expect(output).toHaveLength(1);
29+
expect(output[0]).toBeInstanceOf(RawImage);
30+
expect(output[0].width).toEqual(image.width);
31+
expect(output[0].height).toEqual(image.height);
32+
expect(output[0].channels).toEqual(4); // With alpha channel
33+
},
34+
MAX_TEST_EXECUTION_TIME,
35+
);
36+
37+
afterAll(async () => {
38+
await pipe.dispose();
39+
}, MAX_MODEL_DISPOSE_TIME);
40+
});
41+
42+
describe("Selfie Segmentation", () => {
43+
const model_id = "onnx-community/mediapipe_selfie_segmentation";
44+
/** @type {BackgroundRemovalPipeline } */
45+
let pipe;
46+
beforeAll(async () => {
47+
pipe = await pipeline(PIPELINE_ID, model_id, DEFAULT_MODEL_OPTIONS);
48+
}, MAX_MODEL_LOAD_TIME);
49+
50+
it(
51+
"single",
52+
async () => {
53+
const image = await load_cached_image("portrait_of_woman");
54+
55+
const output = await pipe(image);
56+
expect(output).toHaveLength(1);
57+
expect(output[0]).toBeInstanceOf(RawImage);
58+
expect(output[0].width).toEqual(image.width);
59+
expect(output[0].height).toEqual(image.height);
60+
expect(output[0].channels).toEqual(4); // With alpha channel
61+
},
62+
MAX_TEST_EXECUTION_TIME,
63+
);
64+
65+
afterAll(async () => {
66+
await pipe.dispose();
67+
}, MAX_MODEL_DISPOSE_TIME);
68+
});
69+
});
70+
};

0 commit comments

Comments
 (0)