In Lesson 13, we talked about module overrides for platform customization, but for smaller customizations, like selecting assets, we can use the Platform
module from React Native to add conditional logic to cross-platform modules.
Add a ternary expression to choose the ONNX model when Platform.OS === 'windows'
.
this.recognizer = new ImageRecognizer({
model: Platform.OS === 'windows'
? require('../../assets/model.onnx')
: require('../../assets/model.pb'),
labels: require('../../assets/labels.txt'),
});
The ternary or conditional expressions can get verbose when dealing with more than two platforms, so another option is to use Platform.select
.
this.recognizer = new ImageRecognizer({
model: Platform.select({
windows: require('../../assets/model.onnx'),
default: require('../../assets/model.pb'),
}),
labels: require('../../assets/labels.txt'),
});
- A fully working app that uses the camera and Windows ML to classify images captured by the device.