Skip to content

Commit 2cf108f

Browse files
committed
add not working jsi bindings
1 parent c927c73 commit 2cf108f

7 files changed

Lines changed: 51 additions & 12 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ REGISTER_CONSTRUCTOR(TextEmbeddings, std::string, std::string,
3636
std::shared_ptr<react::CallInvoker>);
3737
REGISTER_CONSTRUCTOR(LLM, std::string, std::string,
3838
std::shared_ptr<react::CallInvoker>);
39-
39+
REGISTER_CONSTRUCTOR(OCR, std::string, std::string, std::string, std::string,
40+
std::string, std::shared_ptr<react::CallInvoker>);
4041
using namespace facebook;
4142

4243
class RnExecutorchInstaller {
@@ -55,7 +56,6 @@ class RnExecutorchInstaller {
5556
loadModel(jsi::Runtime *jsiRuntime,
5657
std::shared_ptr<react::CallInvoker> jsCallInvoker,
5758
const std::string &loadFunctionName) {
58-
5959
return jsi::Function::createFromHostFunction(
6060
*jsiRuntime, jsi::PropNameID::forAscii(*jsiRuntime, loadFunctionName),
6161
0,

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <rnexecutorch/metaprogramming/TypeConcepts.h>
1414
#include <rnexecutorch/models/object_detection/Constants.h>
1515
#include <rnexecutorch/models/object_detection/Utils.h>
16+
#include <rnexecutorch/models/ocr/Types.h>
1617

1718
namespace rnexecutorch::jsiconversion {
1819

@@ -266,4 +267,25 @@ inline jsi::Value getJsiValue(const std::vector<Detection> &detections,
266267
return array;
267268
}
268269

270+
inline jsi::Value getJsiValue(const std::vector<float> &vec,
271+
jsi::Runtime &runtime) {
272+
jsi::Array array(runtime, vec.size());
273+
for (size_t i = 0; i < vec.size(); i++) {
274+
array.setValueAtIndex(runtime, i, jsi::Value(static_cast<float>(vec[i])));
275+
}
276+
return jsi::Value(runtime, array);
277+
}
278+
279+
inline jsi::Value getJsiValue(const std::vector<OCRDetection> &detections,
280+
jsi::Runtime &runtime) {
281+
jsi::Array array(runtime, detections.size());
282+
for (std::size_t i = 0; i < detections.size(); ++i) {
283+
jsi::Object detection(runtime);
284+
jsi::Object bbox(runtime);
285+
detection.setProperty(runtime, "text", detections[i].text);
286+
detection.setProperty(runtime, "score", detections[i].score);
287+
array.setValueAtIndex(runtime, i, detection);
288+
}
289+
return array;
290+
}
269291
} // namespace rnexecutorch::jsiconversion

packages/react-native-executorch/src/controllers/OCRController.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { symbols } from '../constants/ocr/symbols';
22
import { ETError, getError } from '../Error';
3-
import { OCRNativeModule } from '../native/RnExecutorchModules';
43
import { ResourceSource } from '../types/common';
54
import { OCRLanguage } from '../types/ocr';
65
import { ResourceFetcher } from '../utils/ResourceFetcher';
76

87
export class OCRController {
9-
private nativeModule: typeof OCRNativeModule;
8+
private nativeModule: any; //typeof OCRNativeModule;
109
public isReady: boolean = false;
1110
public isGenerating: boolean = false;
1211
public error: string | null = null;
@@ -21,14 +20,14 @@ export class OCRController {
2120
isGeneratingCallback = (_isGenerating: boolean) => {},
2221
errorCallback = (_error: string) => {},
2322
}) {
24-
this.nativeModule = OCRNativeModule;
23+
//this.nativeModule = OCRNativeModule;
2524
this.modelDownloadProgressCallback = modelDownloadProgressCallback;
2625
this.isReadyCallback = isReadyCallback;
2726
this.isGeneratingCallback = isGeneratingCallback;
2827
this.errorCallback = errorCallback;
2928
}
3029

31-
public loadModel = async (
30+
public load = async (
3231
detectorSource: ResourceSource,
3332
recognizerSources: {
3433
recognizerLarge: ResourceSource;
@@ -58,14 +57,23 @@ export class OCRController {
5857
if (paths === null || paths?.length < 4) {
5958
throw new Error('Download interrupted!');
6059
}
61-
await this.nativeModule.loadModule(
60+
console.log('fetching ocr finished!', paths[3]);
61+
//load OCR from cpp model
62+
this.nativeModule = global.loadOCR(
6263
paths[0]!,
6364
paths[1]!,
6465
paths[2]!,
6566
paths[3]!,
6667
symbols[language]
6768
);
68-
69+
// await this.nativeModule.loadModule(
70+
// paths[0]!,
71+
// paths[1]!,
72+
// paths[2]!,
73+
// paths[3]!,
74+
// symbols[language]
75+
// );
76+
console.log('loaded ocr finished!,');
6977
this.isReady = true;
7078
this.isReadyCallback(this.isReady);
7179
} catch (e) {
@@ -88,7 +96,7 @@ export class OCRController {
8896
try {
8997
this.isGenerating = true;
9098
this.isGeneratingCallback(this.isGenerating);
91-
return await this.nativeModule.forward(input);
99+
return await this.nativeModule.generate(input);
92100
} catch (e) {
93101
throw new Error(getError(e));
94102
} finally {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const useOCR = ({
4444

4545
useEffect(() => {
4646
const loadModel = async () => {
47-
await model.loadModel(detectorSource, recognizerSources, language);
47+
await model.load(detectorSource, recognizerSources, language);
4848
};
4949

5050
if (!preventLoad) {

packages/react-native-executorch/src/hooks/useNonStaticModule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const useNonStaticModule = <
2929
const [isReady, setIsReady] = useState(false);
3030
const [isGenerating, setIsGenerating] = useState(false);
3131
const [downloadProgress, setDownloadProgress] = useState(0);
32+
console.log(module);
3233
const model = useMemo(() => new module(), [module]);
3334

3435
useEffect(() => {

packages/react-native-executorch/src/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ declare global {
1313
var loadImageEmbeddings: (source: string) => any;
1414
var loadTextEmbeddings: (modelSource: string, tokenizerSource: string) => any;
1515
var loadLLM: (modelSource: string, tokenizerSource: string) => any;
16+
var loadOCR: (
17+
detectorSource: string,
18+
recognizerLarge: string,
19+
recognizerMedium: string,
20+
recognizerSmall: string,
21+
symbols: string
22+
) => any;
1623
}
1724
// eslint-disable no-var
1825
if (
@@ -24,7 +31,8 @@ if (
2431
global.loadTokenizerModule == null ||
2532
global.loadTextEmbeddings == null ||
2633
global.loadImageEmbeddings == null ||
27-
global.loadLLM == null
34+
global.loadLLM == null ||
35+
global.loadOCR == null
2836
) {
2937
if (!ETInstallerNativeModule) {
3038
throw new Error(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class OCRModule {
2020
modelDownloadProgressCallback: this.onDownloadProgressCallback,
2121
});
2222

23-
await this.module.loadModel(detectorSource, recognizerSources, language);
23+
await this.module.load(detectorSource, recognizerSources, language);
2424
}
2525

2626
static async forward(input: string) {

0 commit comments

Comments
 (0)