diff --git a/packages/react-native-executorch/__tests__/tasks/classification.test.ts b/packages/react-native-executorch/__tests__/tasks/classification.test.ts index 20f48bc510..74d4be0ad3 100644 --- a/packages/react-native-executorch/__tests__/tasks/classification.test.ts +++ b/packages/react-native-executorch/__tests__/tasks/classification.test.ts @@ -54,6 +54,21 @@ describe('createClassifier — model acceptance', () => { ); }); + it('names the OpenCV lib when the app opted out of it (#1618)', async () => { + registerBatched(); + const jsi = (globalThis as Record).__rnexecutorch_jsi__; + const { cv } = jsi; + delete jsi.cv; + try { + await expect(createClassifier(config())).rejects.toMatchObject({ + code: 'INVALID_STATE', + message: expect.stringContaining("Add the 'opencv' lib"), + }); + } finally { + jsi.cv = cv; + } + }); + it('surfaces a load failure', async () => { await expect( createClassifier({ ...config(), modelPath: '/models/absent.pte' }) diff --git a/packages/react-native-executorch/src/extensions/cv/ops/box.ts b/packages/react-native-executorch/src/extensions/cv/ops/box.ts index 61da7dae1e..9979e134e6 100644 --- a/packages/react-native-executorch/src/extensions/cv/ops/box.ts +++ b/packages/react-native-executorch/src/extensions/cv/ops/box.ts @@ -3,7 +3,7 @@ * Suppression (NMS). */ -import { rnexecutorchJsi } from '../../../native/bridge'; +import { tryInitJsiCv } from '../../../native/bridge'; import type { Tensor } from '../../../core/tensor'; import type { ResizeMode } from './image'; import { scalePoint } from './point'; @@ -187,7 +187,7 @@ export function nms( ): number[][]; export function nms(boxes: Tensor, scores: Tensor, options: NmsOptions): number[] | number[][] { 'worklet'; - return rnexecutorchJsi.cv.nms(boxes, scores, options); + return tryInitJsiCv().nms(boxes, scores, options); } /** @@ -223,5 +223,5 @@ export function restrictToBox(src: Tensor, dst: Tensor, box: BoundingBox [p.x, p.y]); - return rnexecutorchJsi.cv.rectifyQuad(src, dst, flat, { + return tryInitJsiCv().rectifyQuad(src, dst, flat, { contentWidth: options.contentWidth, align: options.align ?? 'left', padValue: options.padValue ?? 0, diff --git a/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts b/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts index e2a5a75a80..0b6e8a7410 100644 --- a/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts +++ b/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts @@ -17,6 +17,7 @@ import { normalize, } from '../ops/image'; import { RnExecuTorchError } from '../../../core/error'; +import { tryInitJsiCv } from '../../../native/bridge'; /** * Options for configuring the image preprocessor pipeline. @@ -79,6 +80,8 @@ export function createImagePreprocessor( outputShape: number[] ): ImagePreprocessor { 'worklet'; + // Fail when the task is created, not on its first frame. + tryInitJsiCv(); const numRgbChannels = 3; const isRank3 = outputShape.length === 3 && outputShape[0] === numRgbChannels; const isRank4 = outputShape.length === 4 && outputShape[1] === numRgbChannels; diff --git a/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts b/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts index 388cf065dd..94c82ce6a2 100644 --- a/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts +++ b/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts @@ -2,7 +2,7 @@ * DBNet contour tracing and text quad extraction utilities for PP-OCRv6. */ -import { rnexecutorchJsi } from '../../../native/bridge'; +import { tryInitJsiCv } from '../../../native/bridge'; import type { Tensor } from '../../../core/tensor'; import { RnExecuTorchError } from '../../../core/error'; import type { Quad } from '../ops/quad'; @@ -60,6 +60,6 @@ export type DbnetDecodeOptions = { */ export function extractDbnetTextQuads(probabilityMap: Tensor, options: DbnetDecodeOptions): Quad[] { 'worklet'; - const flat = rnexecutorchJsi.cv.extractDbnetTextQuads(probabilityMap, options) as Float32Array; + const flat = tryInitJsiCv().extractDbnetTextQuads(probabilityMap, options) as Float32Array; return quadsFromFlat(flat); } diff --git a/packages/react-native-executorch/src/native/bridge.ts b/packages/react-native-executorch/src/native/bridge.ts index 3d17ab8fc0..00a9c0820e 100644 --- a/packages/react-native-executorch/src/native/bridge.ts +++ b/packages/react-native-executorch/src/native/bridge.ts @@ -21,3 +21,24 @@ export const rnexecutorchJsi = globalObj.__rnexecutorch_jsi__; if (!rnexecutorchJsi) { throw RnExecuTorchError('UNKNOWN', "JSI global object '__rnexecutorch_jsi__' is not registered."); } + +/** + * The OpenCV-backed `cv` namespace of {@link rnexecutorchJsi}. Native code only + * installs it when the app builds with OpenCV, so an app that opted out gets an + * actionable error here instead of a `TypeError` on `undefined`. + * @returns The native `cv` namespace. + * @throws {RnExecuTorchError} With code `INVALID_STATE` if the native build + * has no OpenCV. + * @internal + */ +export function tryInitJsiCv() { + 'worklet'; + if (!rnexecutorchJsi.cv) { + throw RnExecuTorchError( + 'INVALID_STATE', + "The native build has no OpenCV. Add the 'opencv' lib (or a feature that uses it, such as " + + "'classification') to the app's react-native-executorch config and rebuild." + ); + } + return rnexecutorchJsi.cv; +}