|
| 1 | +# ONNX Runtime React Native Expo Example Application |
| 2 | + |
| 3 | +This is a [React Native](https://reactnative.dev/docs/getting-started) application for [ONNX Runtime](https://github.com/microsoft/onnxruntime) via [Expo](https://docs.expo.dev/) platform. The demo app demonstrates how to accomplish simple tasks such as loading onnx models and creating inference sessions, etc. in a react native expo application. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. Install [Node.js](https://nodejs.org/en) |
| 8 | +2. Install [Expo-CLI](https://docs.expo.dev/more/expo-cli/) |
| 9 | + ```sh |
| 10 | + npm install -g expo-cli |
| 11 | + ``` |
| 12 | +3. Install [Yarn](https://classic.yarnpkg.com/en/docs/install#mac-stable) (Recommended) |
| 13 | + ```sh |
| 14 | + npm install -g yarn |
| 15 | + ``` |
| 16 | + |
| 17 | + |
| 18 | +## Validate your React Native Environment |
| 19 | + |
| 20 | +Run the example expo app as-is for validating local React Native environment setup. |
| 21 | + |
| 22 | +**Steps:** |
| 23 | + |
| 24 | +1. Run `yarn install` to set up JavaScript dependencies. |
| 25 | + ```sh |
| 26 | + yarn install |
| 27 | + ``` |
| 28 | + |
| 29 | +2. Install NPM `onnxruntime-react-native` package. |
| 30 | + ```sh |
| 31 | + expo install onnxruntime-react-native@latest |
| 32 | + ``` |
| 33 | + |
| 34 | +3. Run the following command in `<PROJECT_ROOT>` to generate Android and iOS project. |
| 35 | + ```sh |
| 36 | + expo prebuild |
| 37 | + ``` |
| 38 | + The generated Android and iOS project code will be updated automatically. |
| 39 | + |
| 40 | +4. Build and run the app. |
| 41 | + |
| 42 | + Run the following command to build and launch the app: |
| 43 | + |
| 44 | + - In `<PROJECT_ROOT>`, run the following command to launch for Android: |
| 45 | + |
| 46 | + ```sh |
| 47 | + expo run:android |
| 48 | + ``` |
| 49 | + |
| 50 | + - In `<PROJECT_ROOT>`, run the following command to launch for iOS: |
| 51 | + ```sh |
| 52 | + expo run:ios |
| 53 | + ``` |
| 54 | + |
| 55 | +## Steps that were done to add onnxruntime-react-native to the example app |
| 56 | + |
| 57 | +The following steps were done in this sample for using onxnruntime-react-native. These can be replicated as a reference when setting up your own react native expo application. |
| 58 | + |
| 59 | +1. NPM `onnxruntime-react-native` package. |
| 60 | + |
| 61 | + Note: By default, we install the current latest release version of ORT react native npm package(Recommended). Can also update to dev version npm package if necessary. |
| 62 | + |
| 63 | + [Link to npm `onnxruntime-react-native` packages](https://www.npmjs.com/package/onnxruntime-react-native?activeTab=versions) |
| 64 | + |
| 65 | +2. Prepare the model. |
| 66 | + |
| 67 | + - Model files are usually placed under `<PROJECT_ROOT>/assets`. |
| 68 | + |
| 69 | + In this sample application, a simple ONNX MNIST model (`mnist.onnx`) is provided by default. |
| 70 | + |
| 71 | + - Add file `metro.config.js` under `<PROJECT_ROOT>`. This file adds the extension `.onnx` to the bundler's asset extension list, which allows the bundler to include the model into assets. |
| 72 | + |
| 73 | + `metro.config.js` file in this sample application looks like: |
| 74 | +
|
| 75 | + ```js |
| 76 | + const { getDefaultConfig } = require('@expo/metro-config'); |
| 77 | + const defaultConfig = getDefaultConfig(__dirname); |
| 78 | + defaultConfig.resolver.assetExts.push('onnx'); |
| 79 | + module.exports = defaultConfig; |
| 80 | + ``` |
| 81 | +
|
| 82 | +3. Generate Android and iOS directories native code to run your React Native app. |
| 83 | + |
| 84 | + In this sample project, it generates the native code automatically by using package `onnxruntime-react-native` as an Expo plugin. (Recommended approach.) |
| 85 | + |
| 86 | + - In `<PROJECT_ROOT>/app.json`, add the following line to section `expo`: |
| 87 | + ``` |
| 88 | + "plugins": ["onnxruntime-react-native"], |
| 89 | + ``` |
| 90 | + Note: The plugin is added by default in `app.json` in the sample. |
| 91 | +
|
| 92 | + - Run the following command in `<PROJECT_ROOT>` to generate Android and iOS project: More info about [Expo CLI Prebuild](https://docs.expo.dev/workflow/prebuild/). |
| 93 | + ```sh |
| 94 | + expo prebuild |
| 95 | + ``` |
| 96 | + The generated Android and iOS project code will be updated automatically. |
| 97 | +
|
| 98 | +
|
| 99 | + [Optional] Set up manually. |
| 100 | +
|
| 101 | + 1. First run `expo prebuild` to generate Android and iOS Native code. |
| 102 | +
|
| 103 | + Note: In this tutorial we use `ai.onnxruntime.example.reactnative.basicusage` as Android package name/iOS bundle identifier. |
| 104 | + Expo requires a manual configuration on package name and bundle identifier. |
| 105 | +
|
| 106 | + 2. [Android]: Add `onnxruntime-react-native` as Gradle dependencies. |
| 107 | +
|
| 108 | + In `<PROJECT_ROOT>/android/app/build.gradle`, add the following line to section `dependencies`: |
| 109 | +
|
| 110 | + ``` |
| 111 | + implementation project(':onnxruntime-react-native') |
| 112 | + ``` |
| 113 | +
|
| 114 | + 3. [iOS]: Add `onnxruntime-react-native` as CocoaPods dependencies. |
| 115 | +
|
| 116 | + In `<PROJECT_ROOT>/ios/Podfile`, add the following line to section `target 'OrtReactNativeBasicUsage'`: |
| 117 | +
|
| 118 | + ```sh |
| 119 | + pod 'onnxruntime-react-native', :path => '../node_modules/onnxruntime-react-native' |
| 120 | + ``` |
| 121 | +
|
| 122 | + Run the following command in `<PROJECT_ROOT>/ios` to install pod: |
| 123 | +
|
| 124 | + ```sh |
| 125 | + pod install |
| 126 | + ``` |
| 127 | +**NOTE:** |
| 128 | + If you are interested in creating a new react native expo project from scratch, refer to instructions: https://docs.expo.dev/get-started/create-a-project/ |
0 commit comments