Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>).__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' })
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-executorch/src/extensions/cv/ops/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -223,5 +223,5 @@ export function restrictToBox(src: Tensor, dst: Tensor, box: BoundingBox<BoxForm
[a, b, c, d] = [box.cx, box.cy, box.w, box.h];
break;
}
return rnexecutorchJsi.cv.restrictToBox(src, dst, [a, b, c, d], box.format);
return tryInitJsiCv().restrictToBox(src, dst, [a, b, c, d], box.format);
}
16 changes: 8 additions & 8 deletions packages/react-native-executorch/src/extensions/cv/ops/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* pixel normalization, and colormap application.
*/

import { rnexecutorchJsi } from '../../../native/bridge';
import { tryInitJsiCv } from '../../../native/bridge';
import type { Tensor } from '../../../core/tensor';
import type { ImageFormat } from '../image';

Expand Down Expand Up @@ -130,7 +130,7 @@ export type NormalizeOptions = {
*/
export function resize(src: Tensor, dst: Tensor, options?: ResizeOptions): Tensor {
'worklet';
return rnexecutorchJsi.cv.resize(src, dst, {
return tryInitJsiCv().resize(src, dst, {
mode: options?.mode ?? 'stretch',
interpolation: options?.interpolation ?? 'lanczos',
padValue: options?.padValue ?? 0,
Expand All @@ -157,7 +157,7 @@ export function resize(src: Tensor, dst: Tensor, options?: ResizeOptions): Tenso
*/
export function cvtColor(src: Tensor, dst: Tensor, code: ColorConversionCode): Tensor {
'worklet';
return rnexecutorchJsi.cv.cvtColor(src, dst, code);
return tryInitJsiCv().cvtColor(src, dst, code);
}

/**
Expand All @@ -178,7 +178,7 @@ export function cvtColor(src: Tensor, dst: Tensor, code: ColorConversionCode): T
*/
export function toChannelsFirst(src: Tensor, dst: Tensor): Tensor {
'worklet';
return rnexecutorchJsi.cv.toChannelsFirst(src, dst);
return tryInitJsiCv().toChannelsFirst(src, dst);
}

/**
Expand All @@ -200,7 +200,7 @@ export function toChannelsFirst(src: Tensor, dst: Tensor): Tensor {
*/
export function toChannelsLast(src: Tensor, dst: Tensor): Tensor {
'worklet';
return rnexecutorchJsi.cv.toChannelsLast(src, dst);
return tryInitJsiCv().toChannelsLast(src, dst);
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ export function toChannelsLast(src: Tensor, dst: Tensor): Tensor {
*/
export function normalize(src: Tensor, dst: Tensor, options?: NormalizeOptions): Tensor {
'worklet';
return rnexecutorchJsi.cv.normalize(src, dst, {
return tryInitJsiCv().normalize(src, dst, {
alpha: options?.alpha ?? 1 / 255.0,
beta: options?.beta ?? 0.0,
});
Expand Down Expand Up @@ -262,7 +262,7 @@ export function applyColormap(
colormap: [number, number, number, number][]
): Tensor {
'worklet';
return rnexecutorchJsi.cv.applyColormap(src, dst, colormap);
return tryInitJsiCv().applyColormap(src, dst, colormap);
}

/**
Expand Down Expand Up @@ -310,7 +310,7 @@ export function blendMasked(
options?: BlendMaskedOptions
): Tensor {
'worklet';
return rnexecutorchJsi.cv.blendMasked(src, overlay, mask, dst, {
return tryInitJsiCv().blendMasked(src, overlay, mask, dst, {
shrink: options?.shrink ?? 0,
feather: options?.feather ?? 0,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* utilities.
*/

import { rnexecutorchJsi } from '../../../native/bridge';
import { tryInitJsiCv } from '../../../native/bridge';
import type { Tensor } from '../../../core/tensor';
import { RnExecuTorchError } from '../../../core/error';
import { distance, scalePoint, type Point } from './point';
Expand Down Expand Up @@ -180,7 +180,7 @@ export function rectifyQuad(
'worklet';
// The native op takes the corners as a flat [x0,y0,..,x3,y3] array.
const flat = quad.flatMap((p) => [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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
21 changes: 21 additions & 0 deletions packages/react-native-executorch/src/native/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading