Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit f156ea3

Browse files
Add custom (TensorFlow Lite) models support to the ML Kit feature #702
1 parent 57969d0 commit f156ea3

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

demo-ng/app/tabs/mlkit/mlkit.component.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { Component, NgZone } from "@angular/core";
22
import { RouterExtensions } from "nativescript-angular";
3-
import { fromFile, ImageSource } from "tns-core-modules/image-source";
4-
import * as fileSystemModule from "tns-core-modules/file-system";
5-
import { action } from "tns-core-modules/ui/dialogs";
6-
import { ImageAsset } from "tns-core-modules/image-asset";
7-
import { isIOS } from "tns-core-modules/platform";
8-
import * as ImagePicker from "nativescript-imagepicker";
93
import * as Camera from "nativescript-camera";
4+
import * as ImagePicker from "nativescript-imagepicker";
105
import { BarcodeFormat, MLKitScanBarcodesOnDeviceResult } from "nativescript-plugin-firebase/mlkit/barcodescanning";
11-
import { MLKitLandmarkRecognitionCloudResult } from "nativescript-plugin-firebase/mlkit/landmarkrecognition";
6+
import { MLKitCustomModelResult } from "nativescript-plugin-firebase/mlkit/custommodel";
127
import { MLKitDetectFacesOnDeviceResult } from "nativescript-plugin-firebase/mlkit/facedetection";
13-
import { MLKitRecognizeTextResult } from "nativescript-plugin-firebase/mlkit/textrecognition";
148
import {
159
MLKitImageLabelingCloudResult,
1610
MLKitImageLabelingOnDeviceResult
1711
} from "nativescript-plugin-firebase/mlkit/imagelabeling";
18-
import { MLKitCustomModelResult } from "nativescript-plugin-firebase/mlkit/custommodel";
12+
import { MLKitLandmarkRecognitionCloudResult } from "nativescript-plugin-firebase/mlkit/landmarkrecognition";
13+
import { MLKitRecognizeTextResult } from "nativescript-plugin-firebase/mlkit/textrecognition";
14+
import * as fileSystemModule from "tns-core-modules/file-system";
15+
import { ImageAsset } from "tns-core-modules/image-asset";
16+
import { fromFile, ImageSource } from "tns-core-modules/image-source";
17+
import { isIOS } from "tns-core-modules/platform";
18+
import { action } from "tns-core-modules/ui/dialogs";
1919

2020
const firebase = require("nativescript-plugin-firebase");
2121

docs/ML_KIT.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ To be able to use Cloud features you need to do two things:
8484
|[Barcode scanning](#barcode-scanning)|✅|
8585
|[Image labeling](#image-labeling)|✅|✅
8686
|[Landmark recognition](#landmark-recognition)||✅
87-
|[Custom model inference](#custom-model-inference)||
87+
|[Custom model inference](#custom-model-inference)|✅|✅
8888

8989
### Text recognition
9090
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/mlkit_text_recognition.png" height="153px" alt="ML Kit - Text recognition"/>
@@ -363,6 +363,60 @@ firebase.mlkit.landmarkrecognition.recognizeLandmarksCloud({
363363
```
364364
365365
### Custom model inference
366+
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/mlkit_custom_model_tflit.png" height="153px" alt="ML Kit - Custom Model (TensorFlow Lite)"/>
367+
366368
[Firebase documentation 🌎](https://firebase.google.com/docs/ml-kit/use-custom-models)
367369
368-
Coming soon. See issue #702.
370+
⚠️ Please take note of the following:
371+
372+
- Currently only models bundled with your app can be used (not ones hosted on Firebase). That may change in the future.
373+
- Prefix the `localModelFile` and `labelsFile` below with `~/` so they point to your `app/` folder. This is for future compatibility, because I'd like to support loading models from the native bundle as well.
374+
- On Android, make sure the model is not compressed by adding [your model's file extension to app.gradle](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/57969d0a62d761bffb98b19db85af88bfae858dd/demo-ng/app/App_Resources/Android/app.gradle#L22).
375+
- Only "Quantized" models can be used. Not "Float" models, so `modelInput.type` below must be set to `QUANT`.
376+
- The `modelInput.shape` parameter below must specify your model's dimensions. If you're not sure, use the script in the paragraph "Specify the model's input and output" at [the Firebase docs](https://firebase.google.com/docs/ml-kit/ios/use-custom-models).
377+
378+
#### Still image (on-device)
379+
380+
```typescript
381+
import { MLKitCustomModelResult } from "nativescript-plugin-firebase/mlkit/custommodel";
382+
const firebase = require("nativescript-plugin-firebase");
383+
384+
firebase.mlkit.custommodel.useCustomModel({
385+
image: imageSource, // a NativeScript Image or ImageSource, see the demo for examples
386+
maxResults: 10, // default 5 (limit numbers to this amount of results)
387+
localModelFile: "~/custommodel/inception/inception_v3_quant.tflite", // see the demo, where the model lives in app/custommodel/etc..
388+
labelsFile: "~/custommodel/inception/inception_labels.txt",
389+
modelInput: [{ // Array<TNSCustomModelInput>
390+
shape: [1, 299, 299, 3], // see the tips above
391+
type: "QUANT" // for now, must be "QUANT" (and you should use a 'quantized' model (not 'float'))
392+
}]
393+
})
394+
.then((result: MLKitCustomModelResult) => console.log(JSON.stringify(result.result)))
395+
.catch(errorMessage => console.log("ML Kit error: " + errorMessage));
396+
```
397+
398+
#### Live camera feed
399+
The basics are explained above for 'Text recognition'.
400+
401+
Make sure to specify `modelInputShape` without the `[` and `]` characters.
402+
403+
```typescript
404+
import { registerElement } from "nativescript-angular/element-registry";
405+
registerElement("MLKitCustomModel", () => require("nativescript-plugin-firebase/mlkit/custommodel").MLKitCustomModel);
406+
```
407+
408+
```html
409+
<MLKitCustomModel
410+
width="100%"
411+
height="100%"
412+
localModelFile="~/custommodel/inception/inception_v3_quant.tflite"
413+
labelsFile="~/custommodel/inception/inception_labels.txt"
414+
modelInputShape="1, 299, 299, 3"
415+
modelInputType="QUANT"
416+
processEveryNthFrame="30"
417+
maxResults="5"
418+
[torchOn]="torchOn"
419+
(scanResult)="onCustomModelResult($event)">
420+
</MLKitCustomModel>
421+
```
422+
13.8 KB
Loading

0 commit comments

Comments
 (0)