|
| 1 | +# Firebase Machine Learning Custom |
| 2 | + |
| 3 | +[](https://pub.dartlang.org/packages/firebase_ml_custom) |
| 4 | + |
| 5 | +A Flutter plugin to use the [Firebase ML Custom Models API](https://firebase.google.com/docs/ml/use-custom-models). |
| 6 | + |
| 7 | +For Flutter plugins for other Firebase products, see [README.md](https://github.com/FirebaseExtended/flutterfire/blob/master/README.md). |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +To use this plugin, add `firebase_ml_custom` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/). You must also configure Firebase for each platform project: Android and iOS (see the example folder or https://codelabs.developers.google.com/codelabs/flutter-firebase/#4 for step by step details). |
| 12 | + |
| 13 | +### Android |
| 14 | + |
| 15 | +In order to use methods referencing those of [Firebase Model Manager](https://firebase.google.com/docs/reference/android/com/google/firebase/ml/common/modeldownload/FirebaseModelManager) minimum SDK version required is 24. |
| 16 | +Otherwise minimum SDK version is 21. |
| 17 | +This can be specified in your app-level `build.gradle` file. |
| 18 | + |
| 19 | +### iOS |
| 20 | + |
| 21 | +A minimum deployment target of 9.0 is required. You can add the line `platform :ios, '9.0'` in your iOS project `Podfile`. |
| 22 | + |
| 23 | +You may also need to update your app's deployment target to 9.0 using Xcode. Otherwise, you may see |
| 24 | +compilation errors. |
| 25 | + |
| 26 | +## Using Firebase Model Manager |
| 27 | + |
| 28 | +### 1. Create a `FirebaseCustomRemoteModel`. |
| 29 | + |
| 30 | +Create a `FirebaseCustomRemoteModel` object. |
| 31 | +You should already have a model in your Firebase console available for download. Use the name that you gave your model in the Firebase console. |
| 32 | + |
| 33 | +```dart |
| 34 | +FirebaseCustomRemoteModel remoteModel = FirebaseCustomRemoteModel('myModelName'); |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Create a `FirebaseModelDownloadConditions`. |
| 38 | + |
| 39 | +Create a `FirebaseModelDownloadConditions` object. |
| 40 | +Specify optional platform-specific conditions for the model download. |
| 41 | + |
| 42 | +```dart |
| 43 | +FirebaseModelDownloadConditions conditions = |
| 44 | + FirebaseModelDownloadConditions( |
| 45 | + androidRequireWifi: true, |
| 46 | + androidRequireDeviceIdle: true, |
| 47 | + androidRequiredCharging: true, |
| 48 | + iosAllowCellularAccess: false, |
| 49 | + iosAllowBackgroundDownloading: true); |
| 50 | +``` |
| 51 | +All of these parameters except `iosAllowCellularAccess` default to `false` if not specified. `iosAllowCellularAccess` defaults to `true`. |
| 52 | +Each platform looks only at its platform-specific parameters and ignores the rest. |
| 53 | + |
| 54 | +### 3. Create an instance of `FirebaseModelManager`. |
| 55 | + |
| 56 | +Create a `FirebaseModelManager` object corresponding to the default `FirebaseApp` instance. |
| 57 | +```dart |
| 58 | +FirebaseModelManager modelManager = FirebaseModelManager.instance; |
| 59 | +``` |
| 60 | + |
| 61 | +### 4. Call `download()` with `FirebaseCustomRemoteModel` and `FirebaseModelDownloadConditions`. |
| 62 | + |
| 63 | +Initiate the download of a remote model if the download hasn't begun. |
| 64 | +If the model's download is already in progress, the current download task will continue executing. |
| 65 | +If the model is already downloaded to the device, and there is no update, the call will immediately succeed. |
| 66 | +If the model is already downloaded to the device, and there is update, a download for the updated version will be attempted. |
| 67 | +```dart |
| 68 | +await modelManager.download(remoteModel, conditions); |
| 69 | +``` |
| 70 | + |
| 71 | +### 5. Call `isModelDownloaded()` with `FirebaseCustomRemoteModel`. |
| 72 | + |
| 73 | +Return whether the given remote model is currently downloaded. |
| 74 | +```dart |
| 75 | +if (await modelManager.isModelDownloaded(model) == true) ) { |
| 76 | + // do something with this model |
| 77 | +} else { |
| 78 | + // fall back on a locally-bundled model or do something else |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +You can also check if download was successfully completed by surrounding download method with `try` and `catch`. |
| 83 | + |
| 84 | +### 5. Call `getLatestModelFile()` with `FirebaseCustomRemoteModel`. |
| 85 | + |
| 86 | +Return the `File` containing the latest model for the remote model name. This will fail if the model is not yet downloaded on the device or valid custom remote model is not provided. |
| 87 | + |
| 88 | +```dart |
| 89 | +File modelFile = await modelManager.getLatestModelFile(model); |
| 90 | +``` |
| 91 | + |
| 92 | +You can feed this file directly into an interpreter or preprocess it, depending on the interpreter of your choice. |
| 93 | + |
| 94 | +Possible Flutter TF Lite interpreters: |
| 95 | +- [tflite](https://pub.dev/packages/tflite) |
| 96 | +- [tflite_flutter](https://pub.dev/packages/tflite_flutter) |
| 97 | + |
| 98 | +Google does not recommend usage of any specific interpreter and leaves it up to the user to decide. |
| 99 | + |
| 100 | +## Getting Started |
| 101 | + |
| 102 | +See the `example` directory for a complete sample app using Firebase Machine Learning Custom. |
| 103 | + |
| 104 | +## Issues and feedback |
| 105 | + |
| 106 | +Please file Flutterfire specific issues, bugs, or feature requests in our [issue tracker](https://github.com/FirebaseExtended/flutterfire/issues/new). |
| 107 | + |
| 108 | +Plugin issues that are not specific to Flutterfire can be filed in the [Flutter issue tracker](https://github.com/flutter/flutter/issues/new). |
| 109 | + |
| 110 | +To contribute a change to this plugin, |
| 111 | +please review our [contribution guide](https://github.com/FirebaseExtended/flutterfire/blob/master/CONTRIBUTING.md), |
| 112 | +and send a [pull request](https://github.com/FirebaseExtended/flutterfire/pulls). |
0 commit comments