Skip to content

Commit 69456e3

Browse files
committed
Add conversion in library
1 parent 8bfece0 commit 69456e3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ResourceFetcher } from '../../utils/ResourceFetcher';
2-
import { ResourceSource } from '../../types/common';
2+
import { getArrayConstructor, ResourceSource } from '../../types/common';
33
import { TensorPtr } from '../../types/common';
44
import { ETError, getError } from '../../Error';
55
import { BaseNonStaticModule } from '../BaseNonStaticModule';
@@ -16,9 +16,13 @@ export class ImageEmbeddingsModule extends BaseNonStaticModule {
1616
this.nativeModule = global.loadImageEmbeddings(paths[0] || '');
1717
}
1818

19-
async forward(imageSource: string): Promise<TensorPtr> {
19+
async forward(imageSource: string): Promise<number[]> {
2020
if (this.nativeModule == null)
2121
throw new Error(getError(ETError.ModuleNotLoaded));
22-
return await this.nativeModule.generate(imageSource);
22+
const tensor: TensorPtr = await this.nativeModule.generate(imageSource);
23+
24+
const resultArray = getArrayConstructor(tensor.scalarType)(tensor.dataPtr);
25+
26+
return Array.from(resultArray);
2327
}
2428
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ export const getTypeIdentifier = (input: ETInput): number => {
77
return -1;
88
};
99

10+
export const getArrayConstructor = (
11+
scalarType: ScalarType
12+
): ((arr: TensorBuffer) => Float32Array) => {
13+
if (scalarType === ScalarType.FLOAT)
14+
return (arr: TensorBuffer) => new Float32Array(arr as Float32Array);
15+
// todo add more types?
16+
17+
throw Error("We don't handle this type of output array");
18+
};
19+
1020
export type ResourceSource = string | number | object;
1121

1222
export type ETInput =

0 commit comments

Comments
 (0)