npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@westwood-dev/expo-segmentation

v0.1.0

Published

Native background removal and subject segmentation for Expo and React Native

Readme

expo-segmentation

Native background removal and subject segmentation for Expo and React Native.

Detects foreground subjects in photos and removes their backgrounds, returning cropped PNG cutouts with optional greyscale masks.

Platform support

| Platform | Engine | Min version | |----------|--------|-------------| | iOS | Apple Vision (VNGenerateForegroundInstanceMaskRequest) | iOS 17 | | Android | Google ML Kit Subject Segmentation | API 24 | | Web | — | isAvailable() returns false |


Installation

npm install expo-segmentation
# or
yarn add expo-segmentation

This module contains native code. Managed Expo projects must use a development build or EAS Build — it will not work in Expo Go.

iOS — install pods after adding the package:

npx pod-install
# or
cd ios && pod install

Android — no extra steps. ML Kit is fetched automatically via Gradle.


Quick start

import ExpoSegmentation from 'expo-segmentation';
import * as ImagePicker from 'expo-image-picker';

// 1. Check availability
if (!ExpoSegmentation.isAvailable()) {
  console.warn('Segmentation not available on this device / OS version');
  return;
}

// 2. Pick an image
const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ['images'] });
if (result.canceled) return;
const { uri } = result.assets[0];

// 3. Detect individual subjects
const subjects = await ExpoSegmentation.detectSubjects(uri);
// subjects: [{ index: 0, previewUri: 'file://...' }, ...]

// 4. Remove background for selected subjects
const cutouts = await ExpoSegmentation.removeBackground(uri, {
  subjectIndices: [0],   // omit to composite all subjects into one result
  returnMask: false,     // set true to also receive a greyscale mask PNG
});
// cutouts: [{ uri, width, height, cropRect: { x, y, width, height } }]

API reference

ExpoSegmentation.isAvailable(): boolean

Synchronous check. Returns true when the native segmentation API is available on the current device and OS version (iOS 17+, Android API 24+).


ExpoSegmentation.detectSubjects(imageUri: string): Promise<SubjectInfo[]>

Runs the segmentation model and returns one entry per detected foreground subject.

Parameters

| Name | Type | Description | |------|------|-------------| | imageUri | string | file:// URI or http(s):// URL of the source image |

Returns Promise<SubjectInfo[]>

Each SubjectInfo:

interface SubjectInfo {
  index: number;      // zero-based subject identifier
  previewUri: string; // file:// URI — PNG of this subject on a transparent bg
}

ExpoSegmentation.removeBackground(imageUri: string, options?: SegmentationOptions): Promise<SegmentationResult[]>

Removes the background and returns one cropped PNG per requested subject.

Parameters

| Name | Type | Description | |------|------|-------------| | imageUri | string | file:// URI or http(s):// URL of the source image | | options | SegmentationOptions | Optional — see below |

SegmentationOptions

interface SegmentationOptions {
  subjectIndices?: number[];  // indices from detectSubjects(); omit = all subjects combined
  returnMask?: boolean;       // also return a greyscale mask PNG (default: false)
}

Returns Promise<SegmentationResult[]>

Each SegmentationResult:

interface SegmentationResult {
  uri: string;          // file:// URI — PNG cutout with transparent background
  maskUri?: string;     // file:// URI — greyscale mask PNG (only when returnMask: true)
  width: number;        // pixel width of the cropped output image
  height: number;       // pixel height of the cropped output image
  cropRect: {           // position of the crop inside the original image
    x: number;
    y: number;
    width: number;
    height: number;
  };
}

Example app

A full demo is included at example/. It shows the three-step flow: pick/take a photo → review detected subjects → remove background and share results.

cd example
npm install
npx expo start

To use native features on a real device, run via EAS Build or npx expo run:ios / npx expo run:android after npx expo prebuild.


Notes

  • iOS: requires iOS 17 or later. On iOS 16 and below, isAvailable() returns false and both async methods will throw.
  • Android: ML Kit Subject Segmentation requires at least API level 24 (Android 7.0). isAvailable() always returns true on supported Android versions.
  • Output images are written to the device's temporary cache directory. Clean them up when no longer needed if your app processes many images.
  • For very large photos (12 MP+), the module automatically downscales before processing to keep memory usage reasonable while preserving output quality.

Contributing

Contributions are welcome. Please open an issue or pull request on GitHub.

License

MIT