Skip to content

Commit 73fbdab

Browse files
feat: suggested changes / improve comments
1 parent 429bc47 commit 73fbdab

File tree

9 files changed

+35
-23
lines changed

9 files changed

+35
-23
lines changed

apps/computer-vision/app/object_detection_live/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ const styles = StyleSheet.create({
182182
fontWeight: '600',
183183
letterSpacing: 0.3,
184184
},
185-
186-
// Bottom stats bar
187185
bottomBarWrapper: {
188186
position: 'absolute',
189187
bottom: 0,

packages/react-native-executorch/common/rnexecutorch/host_objects/JsiConversions.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ inline jsi::Value getJsiValue(const std::vector<char> &vec,
346346
return {runtime, array};
347347
}
348348

349+
inline jsi::Value getJsiValue(const std::vector<int64_t> &vec,
350+
jsi::Runtime &runtime) {
351+
jsi::Array array(runtime, vec.size());
352+
for (size_t i = 0; i < vec.size(); i++) {
353+
array.setValueAtIndex(runtime, i, jsi::Value(static_cast<double>(vec[i])));
354+
}
355+
return {runtime, array};
356+
}
357+
349358
// Conditional as on android, size_t and uint64_t reduce to the same type,
350359
// introducing ambiguity
351360
template <typename T,
@@ -360,15 +369,6 @@ inline jsi::Value getJsiValue(uint64_t val, jsi::Runtime &runtime) {
360369
return {runtime, bigInt};
361370
}
362371

363-
inline jsi::Value getJsiValue(const std::vector<int64_t> &vec,
364-
jsi::Runtime &runtime) {
365-
jsi::Array array(runtime, vec.size());
366-
for (size_t i = 0; i < vec.size(); i++) {
367-
array.setValueAtIndex(runtime, i, jsi::Value(static_cast<double>(vec[i])));
368-
}
369-
return {runtime, array};
370-
}
371-
372372
inline jsi::Value getJsiValue(int val, jsi::Runtime &runtime) {
373373
return {runtime, val};
374374
}

packages/react-native-executorch/common/rnexecutorch/host_objects/ModelHostObject.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
4646
"getInputShape"));
4747
}
4848

49+
if constexpr (meta::HasGenerate<Model>) {
50+
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
51+
promiseHostFunction<&Model::generate>,
52+
"generate"));
53+
}
54+
4955
if constexpr (meta::HasEncode<Model>) {
5056
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
5157
promiseHostFunction<&Model::encode>,

packages/react-native-executorch/common/rnexecutorch/metaprogramming/TypeConcepts.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ concept DerivedFromOrSameAs = std::is_base_of_v<Base, T>;
1111
template <typename T, typename Base>
1212
concept SameAs = std::is_same_v<Base, T>;
1313

14+
template <typename T>
15+
concept HasGenerate = requires(T t) {
16+
{ &T::generate };
17+
};
18+
1419
template <typename T>
1520
concept HasGenerateFromString = requires(T t) {
1621
{ &T::generateFromString };

packages/react-native-executorch/common/rnexecutorch/tests/integration/ObjectDetectionTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ TEST(ObjectDetectionGenerateTests, DetectionsHaveValidLabels) {
122122
// ============================================================================
123123
TEST(ObjectDetectionPixelTests, ValidPixelDataReturnsResults) {
124124
ObjectDetection model(kValidObjectDetectionModelPath, nullptr);
125-
constexpr int width = 4, height = 4, channels = 3;
125+
constexpr int32_t width = 4, height = 4, channels = 3;
126126
std::vector<uint8_t> pixelData(width * height * channels, 128);
127127
JSTensorViewIn tensorView{pixelData.data(),
128128
{height, width, channels},
@@ -142,7 +142,7 @@ TEST(ObjectDetectionPixelTests, WrongSizesLengthThrows) {
142142

143143
TEST(ObjectDetectionPixelTests, WrongChannelCountThrows) {
144144
ObjectDetection model(kValidObjectDetectionModelPath, nullptr);
145-
constexpr int width = 4, height = 4, channels = 4;
145+
constexpr int32_t width = 4, height = 4, channels = 4;
146146
std::vector<uint8_t> pixelData(width * height * channels, 0);
147147
JSTensorViewIn tensorView{pixelData.data(),
148148
{height, width, channels},
@@ -153,7 +153,7 @@ TEST(ObjectDetectionPixelTests, WrongChannelCountThrows) {
153153

154154
TEST(ObjectDetectionPixelTests, WrongScalarTypeThrows) {
155155
ObjectDetection model(kValidObjectDetectionModelPath, nullptr);
156-
constexpr int width = 4, height = 4, channels = 3;
156+
constexpr int32_t width = 4, height = 4, channels = 3;
157157
std::vector<uint8_t> pixelData(width * height * channels, 0);
158158
JSTensorViewIn tensorView{pixelData.data(),
159159
{height, width, channels},
@@ -164,7 +164,7 @@ TEST(ObjectDetectionPixelTests, WrongScalarTypeThrows) {
164164

165165
TEST(ObjectDetectionPixelTests, NegativeThresholdThrows) {
166166
ObjectDetection model(kValidObjectDetectionModelPath, nullptr);
167-
constexpr int width = 4, height = 4, channels = 3;
167+
constexpr int32_t width = 4, height = 4, channels = 3;
168168
std::vector<uint8_t> pixelData(width * height * channels, 128);
169169
JSTensorViewIn tensorView{pixelData.data(),
170170
{height, width, channels},
@@ -175,7 +175,7 @@ TEST(ObjectDetectionPixelTests, NegativeThresholdThrows) {
175175

176176
TEST(ObjectDetectionPixelTests, ThresholdAboveOneThrows) {
177177
ObjectDetection model(kValidObjectDetectionModelPath, nullptr);
178-
constexpr int width = 4, height = 4, channels = 3;
178+
constexpr int32_t width = 4, height = 4, channels = 3;
179179
std::vector<uint8_t> pixelData(width * height * channels, 128);
180180
JSTensorViewIn tensorView{pixelData.data(),
181181
{height, width, channels},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class ObjectDetectionModule extends VisionModule<Detection[]> {
2828
onDownloadProgressCallback,
2929
model.modelSource
3030
);
31+
3132
if (!paths?.[0]) {
3233
throw new RnExecutorchError(
3334
RnExecutorchErrorCode.DownloadInterrupted,

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ export abstract class VisionModule<TOutput> extends BaseModule {
4343
* const model = new ClassificationModule();
4444
* await model.load({ modelSource: MODEL });
4545
*
46+
* // Use the functional form of setState to store the worklet — passing it
47+
* // directly would cause React to invoke it immediately as an updater fn.
48+
* const [runOnFrame, setRunOnFrame] = useState(null);
49+
* setRunOnFrame(() => model.runOnFrame);
50+
*
4651
* const frameOutput = useFrameOutput({
4752
* onFrame(frame) {
4853
* 'worklet';
49-
* if (!model.runOnFrame) return;
50-
* const result = model.runOnFrame(frame);
54+
* if (!runOnFrame) return;
55+
* const result = runOnFrame(frame);
5156
* frame.dispose();
5257
* }
5358
* });

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,6 @@ export interface PixelData extends Omit<TensorPtr, 'dataPtr' | 'scalarType'> {
179179

180180
/**
181181
* Frame data for vision model processing.
182-
* Supports two modes:
183-
* 1. ArrayBuffer mode (with memory copy) - Compatible with all platforms
184-
* 2. NativeBuffer mode (zero-copy) - Better performance with Vision Camera v5
185182
*/
186183
export interface Frame {
187184
/**
@@ -194,7 +191,7 @@ export interface Frame {
194191
getNativeBuffer(): { pointer: bigint; release(): void };
195192
}
196193

197-
/** *
194+
/**
198195
* A readonly record mapping string keys to numeric or string values.
199196
* Used to represent enum-like label maps for models.
200197
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export interface ObjectDetectionType {
176176
* 1. **String path/URI**: File path, URL, or Base64-encoded string
177177
* 2. **PixelData**: Raw pixel data from image libraries (e.g., NitroImage)
178178
*
179-
* **Note**: For VisionCamera frame processing, use `processFrame` instead.
179+
* **Note**: For VisionCamera frame processing, use `runOnFrame` instead.
180180
*
181181
* @param input - Image source (string or PixelData object)
182182
* @param detectionThreshold - An optional number between 0 and 1 representing the minimum confidence score. Default is 0.5.

0 commit comments

Comments
 (0)