File tree Expand file tree Collapse file tree 9 files changed +50
-47
lines changed
packages/react-native-executorch/src Expand file tree Collapse file tree 9 files changed +50
-47
lines changed Original file line number Diff line number Diff line change @@ -89,8 +89,11 @@ export abstract class BaseOCRController {
8989 } ;
9090
9191 get runOnFrame ( ) : ( ( frame : Frame ) => OCRDetection [ ] ) | null {
92- if ( ! this . nativeModule ?. generateFromFrame ) {
93- return null ;
92+ if ( ! this . isReady ) {
93+ throw new RnExecutorchError (
94+ RnExecutorchErrorCode . ModuleNotLoaded ,
95+ 'The model is currently not loaded. Please load the model before calling runOnFrame().'
96+ ) ;
9497 }
9598
9699 const nativeGenerateFromFrame = this . nativeModule . generateFromFrame ;
Original file line number Diff line number Diff line change @@ -14,6 +14,10 @@ import { VisionModule } from './VisionModule';
1414export class ClassificationModule extends VisionModule < {
1515 [ category : string ] : number ;
1616} > {
17+ private constructor ( nativeModule : unknown ) {
18+ super ( ) ;
19+ this . nativeModule = nativeModule ;
20+ }
1721 /**
1822 * Creates a classification instance for a built-in model.
1923 *
@@ -41,9 +45,9 @@ export class ClassificationModule extends VisionModule<{
4145 ) ;
4246 }
4347
44- const instance = new ClassificationModule ( ) ;
45- instance . nativeModule = await global . loadClassification ( paths [ 0 ] ) ;
46- return instance ;
48+ return new ClassificationModule (
49+ await global . loadClassification ( paths [ 0 ] )
50+ ) ;
4751 } catch ( error ) {
4852 Logger . error ( 'Load failed:' , error ) ;
4953 throw parseUnknownError ( error ) ;
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ import { VisionModule } from './VisionModule';
1212 * @category Typescript API
1313 */
1414export class ImageEmbeddingsModule extends VisionModule < Float32Array > {
15+ private constructor ( nativeModule : unknown ) {
16+ super ( ) ;
17+ this . nativeModule = nativeModule ;
18+ }
1519 /**
1620 * Creates an image embeddings instance for a built-in model.
1721 *
@@ -39,9 +43,9 @@ export class ImageEmbeddingsModule extends VisionModule<Float32Array> {
3943 ) ;
4044 }
4145
42- const instance = new ImageEmbeddingsModule ( ) ;
43- instance . nativeModule = await global . loadImageEmbeddings ( paths [ 0 ] ) ;
44- return instance ;
46+ return new ImageEmbeddingsModule (
47+ await global . loadImageEmbeddings ( paths [ 0 ] )
48+ ) ;
4549 } catch ( error ) {
4650 Logger . error ( 'Load failed:' , error ) ;
4751 throw parseUnknownError ( error ) ;
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ import { VisionModule } from './VisionModule';
1212 * @category Typescript API
1313 */
1414export class StyleTransferModule extends VisionModule < PixelData | string > {
15+ private constructor ( nativeModule : unknown ) {
16+ super ( ) ;
17+ this . nativeModule = nativeModule ;
18+ }
1519 /**
1620 * Creates a style transfer instance for a built-in model.
1721 *
@@ -39,9 +43,7 @@ export class StyleTransferModule extends VisionModule<PixelData | string> {
3943 ) ;
4044 }
4145
42- const instance = new StyleTransferModule ( ) ;
43- instance . nativeModule = await global . loadStyleTransfer ( paths [ 0 ] ) ;
44- return instance ;
46+ return new StyleTransferModule ( await global . loadStyleTransfer ( paths [ 0 ] ) ) ;
4547 } catch ( error ) {
4648 Logger . error ( 'Load failed:' , error ) ;
4749 throw parseUnknownError ( error ) ;
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import { RnExecutorchErrorCode } from '../../errors/ErrorCodes';
44import { RnExecutorchError } from '../../errors/errorUtils' ;
55import { VisionModule } from './VisionModule' ;
66
7+ export { ResolveLabels } from '../../types/computerVision' ;
8+
79/**
810 * Fetches a model binary and returns its local path, throwing if the download
911 * was interrupted (paused or cancelled).
@@ -24,22 +26,6 @@ export async function fetchModelPath(
2426 return paths [ 0 ] ;
2527}
2628
27- /**
28- * Given a model configs record (mapping model names to `{ labelMap }`) and a
29- * type `T` (either a model name key or a raw {@link LabelEnum}), resolves to
30- * the label map for that model or `T` itself.
31- *
32- * @internal
33- */
34- export type ResolveLabels <
35- T ,
36- Configs extends Record < string , { labelMap : LabelEnum } > ,
37- > = T extends keyof Configs
38- ? Configs [ T ] [ 'labelMap' ]
39- : T extends LabelEnum
40- ? T
41- : never ;
42-
4329/**
4430 * Base class for computer vision modules that carry a type-safe label map
4531 * and support the full VisionModule API (string/PixelData forward + runOnFrame).
Original file line number Diff line number Diff line change @@ -59,8 +59,11 @@ export abstract class VisionModule<TOutput> extends BaseModule {
5959 * ```
6060 */
6161 get runOnFrame ( ) : ( ( frame : Frame , ...args : any [ ] ) => TOutput ) | null {
62- if ( ! this . nativeModule ?. generateFromFrame ) {
63- return null ;
62+ if ( ! this . nativeModule ) {
63+ throw new RnExecutorchError (
64+ RnExecutorchErrorCode . ModuleNotLoaded ,
65+ 'The model is currently not loaded. Please load the model before calling runOnFrame().'
66+ ) ;
6467 }
6568
6669 // Extract pure JSI function reference (runs on JS thread)
@@ -73,9 +76,7 @@ export abstract class VisionModule<TOutput> extends BaseModule {
7376 let nativeBuffer : any = null ;
7477 try {
7578 nativeBuffer = frame . getNativeBuffer ( ) ;
76- const frameData = {
77- nativeBuffer : nativeBuffer . pointer ,
78- } ;
79+ const frameData = { nativeBuffer : nativeBuffer . pointer } ;
7980 return nativeGenerateFromFrame ( frameData , ...args ) ;
8081 } finally {
8182 if ( nativeBuffer ?. release ) {
Original file line number Diff line number Diff line change @@ -77,20 +77,6 @@ export interface ClassificationType {
7777 *
7878 * Available after model is loaded (`isReady: true`).
7979 *
80- * @example
81- * ```typescript
82- * const { runOnFrame, isReady } = useClassification({ model: MODEL });
83- *
84- * const frameOutput = useFrameOutput({
85- * onFrame(frame) {
86- * 'worklet';
87- * if (!runOnFrame) return;
88- * const result = runOnFrame(frame);
89- * frame.dispose();
90- * }
91- * });
92- * ```
93- *
9480 * @param frame - VisionCamera Frame object
9581 * @returns Object mapping class labels to confidence scores.
9682 */
Original file line number Diff line number Diff line change 1+ import { LabelEnum } from './common' ;
2+
3+ /**
4+ * Given a model configs record (mapping model names to `{ labelMap }`) and a
5+ * type `T` (either a model name key or a raw {@link LabelEnum}), resolves to
6+ * the label map for that model or `T` itself.
7+ *
8+ * @internal
9+ */
10+ export type ResolveLabels <
11+ T ,
12+ Configs extends Record < string , { labelMap : LabelEnum } > ,
13+ > = T extends keyof Configs
14+ ? Configs [ T ] [ 'labelMap' ]
15+ : T extends LabelEnum
16+ ? T
17+ : never ;
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ export interface ImageEmbeddingsType {
6060 *
6161 * **Note**: For VisionCamera frame processing, use `runOnFrame` instead.
6262 *
63- * @param input - Image source (string or PixelData object)
63+ * @param input - Image source (string or { @link PixelData} object)
6464 * @returns A Promise that resolves to a `Float32Array` containing the generated embedding vector.
6565 * @throws {RnExecutorchError } If the model is not loaded or is currently processing another image.
6666 */
You can’t perform that action at this time.
0 commit comments