Skip to content

Commit 9eb6061

Browse files
committed
Update api reference docs
1 parent 1dcd27d commit 9eb6061

4 files changed

Lines changed: 96 additions & 24 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/models/instance_segmentation/Types.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44

55
namespace rnexecutorch::models::instance_segmentation::types {
66

7+
/**
8+
* Represents a single detected instance in instance segmentation output.
9+
*
10+
* Contains bounding box coordinates, binary segmentation mask, class label,
11+
* confidence score, and a unique instance identifier.
12+
*/
713
struct InstanceMask {
8-
float x1;
9-
float y1;
10-
float x2;
11-
float y2;
12-
std::vector<uint8_t> mask;
13-
int maskWidth;
14-
int maskHeight;
15-
int label;
16-
float score;
17-
int instanceId;
14+
float x1; ///< Bounding box left coordinate
15+
float y1; ///< Bounding box top coordinate
16+
float x2; ///< Bounding box right coordinate
17+
float y2; ///< Bounding box bottom coordinate
18+
std::vector<uint8_t> mask; ///< Binary mask (0 or 1) for the instance
19+
int maskWidth; ///< Width of the mask array
20+
int maskHeight; ///< Height of the mask array
21+
int label; ///< Class label index
22+
float score; ///< Confidence score [0, 1]
23+
int instanceId; ///< Unique identifier for this instance
1824
};
1925

2026
} // namespace rnexecutorch::models::instance_segmentation::types

packages/react-native-executorch/src/hooks/computer_vision/useInstanceSegmentation.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@ import { RnExecutorchError, parseUnknownError } from '../../errors/errorUtils';
1616
/**
1717
* React hook for managing an Instance Segmentation model instance.
1818
*
19-
* @typeParam C - A {@link InstanceSegmentationModelSources} config specifying which built-in model to load.
19+
* @typeParam C - A {@link InstanceSegmentationModelSources} config specifying which model to load.
2020
* @param props - Configuration object containing `model` config and optional `preventLoad` flag.
2121
* @returns An object with model state (`error`, `isReady`, `isGenerating`, `downloadProgress`) and a typed `forward` function.
2222
*
2323
* @example
2424
* ```ts
25-
* const { isReady, forward } = useInstanceSegmentation({
26-
* model: { modelName: 'yolo26n-seg', modelSource: YOLO26N_SEG },
25+
* const { isReady, isGenerating, forward, error, downloadProgress } =
26+
* useInstanceSegmentation({
27+
* model: {
28+
* modelName: 'yolo26n-seg',
29+
* modelSource: 'https://huggingface.co/.../yolo26n-seg.pte',
30+
* },
31+
* });
32+
*
33+
* if (!isReady) {
34+
* return <Text>Loading: {(downloadProgress * 100).toFixed(0)}%</Text>;
35+
* }
36+
*
37+
* const results = await forward('path/to/image.jpg', {
38+
* confidenceThreshold: 0.5,
39+
* inputSize: 640,
2740
* });
2841
* ```
2942
*

packages/react-native-executorch/src/modules/computer_vision/InstanceSegmentationModule.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,31 @@ type ResolveLabels<T extends InstanceSegmentationModelName | LabelEnum> =
6060

6161
/**
6262
* Generic instance segmentation module with type-safe label maps.
63-
* Use a model name (e.g. `'yolo26n-seg'`) as the generic parameter for built-in models,
63+
* Use a model name (e.g. `'yolo26n-seg'`) as the generic parameter for pre-configured models,
6464
* or a custom label enum for custom configs.
6565
*
66-
* @typeParam T - Either a built-in model name or a custom {@link LabelEnum} label map.
66+
* Supported models (download from HuggingFace):
67+
* - `yolo26n-seg`, `yolo26s-seg`, `yolo26m-seg`, `yolo26l-seg`, `yolo26x-seg` - YOLO models with COCO labels (80 classes)
68+
*
69+
* @typeParam T - Either a pre-configured model name from {@link InstanceSegmentationModelName}
70+
* or a custom {@link LabelEnum} label map.
6771
*
6872
* @category Typescript API
73+
*
74+
* @example
75+
* ```ts
76+
* const segmentation = await InstanceSegmentationModule.fromModelName({
77+
* modelName: 'yolo26n-seg',
78+
* modelSource: 'https://huggingface.co/.../yolo26n-seg.pte',
79+
* });
80+
*
81+
* const results = await segmentation.forward('path/to/image.jpg', {
82+
* confidenceThreshold: 0.5,
83+
* iouThreshold: 0.45,
84+
* maxInstances: 20,
85+
* inputSize: 640,
86+
* });
87+
* ```
6988
*/
7089
export class InstanceSegmentationModule<
7190
T extends InstanceSegmentationModelName | LabelEnum,
@@ -88,7 +107,7 @@ export class InstanceSegmentationModule<
88107
override async load() {}
89108

90109
/**
91-
* Creates an instance segmentation module for a built-in model.
110+
* Creates an instance segmentation module for a pre-configured model.
92111
* The config object is discriminated by `modelName` — each model can require different fields.
93112
*
94113
* @param config - A {@link InstanceSegmentationModelSources} object specifying which model to load and where to fetch it from.
@@ -99,7 +118,7 @@ export class InstanceSegmentationModule<
99118
* ```ts
100119
* const segmentation = await InstanceSegmentationModule.fromModelName({
101120
* modelName: 'yolo26n-seg',
102-
* modelSource: 'https://example.com/yolo26n-seg.pte',
121+
* modelSource: 'https://huggingface.co/.../yolo26n-seg.pte',
103122
* });
104123
* ```
105124
*/
@@ -145,19 +164,29 @@ export class InstanceSegmentationModule<
145164

146165
/**
147166
* Creates an instance segmentation module with a user-provided label map and custom config.
148-
* Use this when working with a custom-exported segmentation model that is not one of the built-in models.
167+
* Use this when working with a custom-exported segmentation model that is not one of the pre-configured models.
149168
*
150169
* @param modelSource - A fetchable resource pointing to the model binary.
151-
* @param config - A {@link InstanceSegmentationConfig} object with the label map.
170+
* @param config - A {@link InstanceSegmentationConfig} object with the label map and optional preprocessing parameters.
152171
* @param onDownloadProgress - Optional callback to monitor download progress, receiving a value between 0 and 1.
153172
* @returns A Promise resolving to an `InstanceSegmentationModule` instance typed to the provided label map.
154173
*
155174
* @example
156175
* ```ts
157176
* const MyLabels = { PERSON: 0, CAR: 1 } as const;
158177
* const segmentation = await InstanceSegmentationModule.fromCustomConfig(
159-
* 'https://example.com/custom_model.pte',
160-
* { labelMap: MyLabels },
178+
* 'https://huggingface.co/.../custom_model.pte',
179+
* {
180+
* labelMap: MyLabels,
181+
* availableInputSizes: [640],
182+
* defaultInputSize: 640,
183+
* postprocessorConfig: {
184+
* type: 'yolo',
185+
* defaultConfidenceThreshold: 0.5,
186+
* defaultIouThreshold: 0.45,
187+
* applyNMS: true,
188+
* },
189+
* },
161190
* );
162191
* ```
163192
*/
@@ -201,9 +230,25 @@ export class InstanceSegmentationModule<
201230
* Executes the model's forward pass to perform instance segmentation on the provided image.
202231
*
203232
* @param imageSource - A string representing the image source (e.g., a file path, URI, or Base64-encoded string).
204-
* @param options - Optional configuration for the segmentation process.
205-
* @returns A Promise resolving to an array of instance masks.
206-
* @throws {RnExecutorchError} If the model is not loaded.
233+
* @param options - Optional configuration for the segmentation process. Includes `confidenceThreshold`, `iouThreshold`, `maxInstances`, `classesOfInterest`, `returnMaskAtOriginalResolution`, and `inputSize`.
234+
* @returns A Promise resolving to an array of {@link SegmentedInstance} objects with `bbox`, `mask`, `maskWidth`, `maskHeight`, `label`, `score`, and `instanceId`.
235+
* @throws {RnExecutorchError} If the model is not loaded or if an invalid `inputSize` is provided.
236+
*
237+
* @example
238+
* ```ts
239+
* const results = await segmentation.forward('path/to/image.jpg', {
240+
* confidenceThreshold: 0.6,
241+
* iouThreshold: 0.5,
242+
* maxInstances: 10,
243+
* inputSize: 640,
244+
* classesOfInterest: ['PERSON', 'CAR'],
245+
* returnMaskAtOriginalResolution: true,
246+
* });
247+
*
248+
* results.forEach((inst) => {
249+
* console.log(`${inst.label}: ${(inst.score * 100).toFixed(1)}%`);
250+
* });
251+
* ```
207252
*/
208253
async forward(
209254
imageSource: string,

packages/react-native-executorch/src/types/instanceSegmentation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ export type InstanceSegmentationModelName =
152152
export type ModelNameOf<C extends InstanceSegmentationModelSources> =
153153
C['modelName'];
154154

155+
/**
156+
* Alias for {@link ModelNameOf}. Extracts the instance segmentation model name from a config object.
157+
*
158+
* @category Types
159+
*/
160+
export type InstanceModelNameOf<C extends InstanceSegmentationModelSources> =
161+
ModelNameOf<C>;
162+
155163
/**
156164
* Props for the `useInstanceSegmentation` hook.
157165
*

0 commit comments

Comments
 (0)