|
| 1 | +--- |
| 2 | +title: useInstanceSegmentation |
| 3 | +--- |
| 4 | + |
| 5 | +Instance segmentation is a computer vision technique that detects individual objects within an image and produces a per-pixel segmentation mask for each one. Unlike object detection (which only returns bounding boxes), instance segmentation provides precise object boundaries. React Native ExecuTorch offers a dedicated hook `useInstanceSegmentation` for this task. |
| 6 | + |
| 7 | +:::warning |
| 8 | +It is recommended to use models provided by us, which are available at our [Hugging Face repository](https://huggingface.co/collections/software-mansion/instance-segmentation-68d0ea936cd0906843cbba7d). |
| 9 | +::: |
| 10 | + |
| 11 | +## API Reference |
| 12 | + |
| 13 | +- For detailed API Reference for `useInstanceSegmentation` see: [`useInstanceSegmentation` API Reference](../../06-api-reference/functions/useInstanceSegmentation.md). |
| 14 | + |
| 15 | +## High Level Overview |
| 16 | + |
| 17 | +```typescript |
| 18 | +import { useInstanceSegmentation } from 'react-native-executorch'; |
| 19 | + |
| 20 | +const model = useInstanceSegmentation({ |
| 21 | + model: { |
| 22 | + modelName: 'yolo26n-seg', |
| 23 | + modelSource: 'https://huggingface.co/.../yolo26n-seg.pte', |
| 24 | + }, |
| 25 | +}); |
| 26 | + |
| 27 | +const imageUri = 'file:///Users/.../photo.jpg'; |
| 28 | + |
| 29 | +try { |
| 30 | + const instances = await model.forward(imageUri); |
| 31 | + // instances is an array of SegmentedInstance objects |
| 32 | +} catch (error) { |
| 33 | + console.error(error); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Arguments |
| 38 | + |
| 39 | +`useInstanceSegmentation` takes [`InstanceSegmentationProps`](../../06-api-reference/interfaces/InstanceSegmentationProps.md) that consists of: |
| 40 | + |
| 41 | +- `model` - An object containing: |
| 42 | + - `modelName` - The name of a built-in model. See [`InstanceSegmentationModelSources`](../../06-api-reference/interfaces/InstanceSegmentationProps.md) for the list of supported models. |
| 43 | + - `modelSource` - The location of the model binary (a URL or a bundled resource). |
| 44 | +- An optional flag [`preventLoad`](../../06-api-reference/interfaces/InstanceSegmentationProps.md#preventload) which prevents auto-loading of the model. |
| 45 | + |
| 46 | +The hook is generic over the model config — TypeScript automatically infers the correct label type based on the `modelName` you provide. No explicit generic parameter is needed. |
| 47 | + |
| 48 | +For more information on loading resources, take a look at [loading models](../../01-fundamentals/02-loading-models.md) page. |
| 49 | + |
| 50 | +### Returns |
| 51 | + |
| 52 | +`useInstanceSegmentation` returns an [`InstanceSegmentationType`](../../06-api-reference/interfaces/InstanceSegmentationType.md) object containing: |
| 53 | + |
| 54 | +- `isReady` - Whether the model is loaded and ready to process images. |
| 55 | +- `isGenerating` - Whether the model is currently processing an image. |
| 56 | +- `error` - An error object if the model failed to load or encountered a runtime error. |
| 57 | +- `downloadProgress` - A value between 0 and 1 representing the download progress of the model binary. |
| 58 | +- `forward` - A function to run inference on an image. |
| 59 | + |
| 60 | +## Running the model |
| 61 | + |
| 62 | +To run the model, use the [`forward`](../../06-api-reference/interfaces/InstanceSegmentationType.md#forward) method. It accepts two arguments: |
| 63 | + |
| 64 | +- `imageSource` (required) - The image to process. Can be a remote URL, a local file URI, or a base64-encoded image (whole URI or only raw base64). |
| 65 | +- `options` (optional) - An [`InstanceSegmentationOptions`](../../06-api-reference/interfaces/InstanceSegmentationOptions.md) object with the following fields: |
| 66 | + - `confidenceThreshold` - Minimum confidence score for including instances. Defaults to the model's configured threshold (typically `0.5`). |
| 67 | + - `iouThreshold` - IoU threshold for non-maximum suppression. Defaults to `0.5`. |
| 68 | + - `maxInstances` - Maximum number of instances to return. Defaults to `100`. |
| 69 | + - `classesOfInterest` - Filter results to include only specific classes (e.g. `['PERSON', 'CAR']`). |
| 70 | + - `returnMaskAtOriginalResolution` - Whether to resize masks to the original image resolution. Defaults to `true`. |
| 71 | + - `inputSize` - Input size for the model (e.g. `384`, `512`, `640`). Must be one of the model's available input sizes. If the model has only one forward method (i.e. no `availableInputSizes` configured), this option is not needed. |
| 72 | + |
| 73 | +`forward` returns a promise resolving to an array of [`SegmentedInstance`](../../06-api-reference/interfaces/SegmentedInstance.md) objects, each containing: |
| 74 | + |
| 75 | +- `bbox` - A [`Bbox`](../../06-api-reference/interfaces/Bbox.md) object with `x1`, `y1` (top-left corner) and `x2`, `y2` (bottom-right corner) coordinates in the original image's pixel space. |
| 76 | +- `label` - The class name of the detected instance, typed to the label map of the chosen model. |
| 77 | +- `score` - The confidence score of the detection, between 0 and 1. |
| 78 | +- `mask` - A `Uint8Array` binary mask (0 or 1) representing the instance's segmentation. |
| 79 | +- `maskWidth` - Width of the mask array. |
| 80 | +- `maskHeight` - Height of the mask array. |
| 81 | +- `instanceId` - Unique identifier for this instance. |
| 82 | + |
| 83 | +## Example |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { useInstanceSegmentation } from 'react-native-executorch'; |
| 87 | + |
| 88 | +function App() { |
| 89 | + const model = useInstanceSegmentation({ |
| 90 | + model: { |
| 91 | + modelName: 'yolo26n-seg', |
| 92 | + modelSource: 'https://huggingface.co/.../yolo26n-seg.pte', |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + const handleSegment = async () => { |
| 97 | + if (!model.isReady) return; |
| 98 | + |
| 99 | + const imageUri = 'file:///Users/.../photo.jpg'; |
| 100 | + |
| 101 | + try { |
| 102 | + const instances = await model.forward(imageUri, { |
| 103 | + confidenceThreshold: 0.5, |
| 104 | + inputSize: 640, |
| 105 | + }); |
| 106 | + |
| 107 | + for (const instance of instances) { |
| 108 | + console.log('Label:', instance.label); |
| 109 | + console.log('Score:', instance.score); |
| 110 | + console.log('Bounding box:', instance.bbox); |
| 111 | + console.log('Mask size:', instance.maskWidth, 'x', instance.maskHeight); |
| 112 | + } |
| 113 | + } catch (error) { |
| 114 | + console.error(error); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + // ... |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Supported models |
| 123 | + |
| 124 | +| Model | Number of classes | Class list | Available input sizes | |
| 125 | +| ----------- | ----------------- | -------------------------------------------------------- | --------------------- | |
| 126 | +| yolo26n-seg | 80 | [COCO](../../06-api-reference/enumerations/CocoLabel.md) | 384, 512, 640 | |
| 127 | +| yolo26s-seg | 80 | [COCO](../../06-api-reference/enumerations/CocoLabel.md) | 384, 512, 640 | |
| 128 | +| yolo26m-seg | 80 | [COCO](../../06-api-reference/enumerations/CocoLabel.md) | 384, 512, 640 | |
| 129 | +| yolo26l-seg | 80 | [COCO](../../06-api-reference/enumerations/CocoLabel.md) | 384, 512, 640 | |
| 130 | +| yolo26x-seg | 80 | [COCO](../../06-api-reference/enumerations/CocoLabel.md) | 384, 512, 640 | |
0 commit comments