|
| 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