我已经使用Expo创建了一个React Native项目,并拥有一个来自keras的功能齐全的转换tflite模型。我正在尝试使用react-native-tensorflow-lite包API将我的模型识别为JPEG图片。
NPM package for React Native Tensorflow lite 我在expo项目中使用了完全相同的模板,但是由于某些原因,API无法识别model.tflite文件路径。 (请参阅图像附件中的错误消息)。 Red Error Screen
下面是我尝试使用Expo-Assets库实现此API的副本:
import {TFLiteImageRecognition} from 'react-native-tensorflow-lite';
import React, {Component} from "react";
import {Text, View, StyleSheet} from "react-native";
import {Asset} from "expo-asset";
//Assets
import Anna from '../../assets/images/anna.jpg';
import Model from '../../android/app/src/main/assets/converted_model.tflite';
import Labels from '../../android/app/src/main/assets/labels.txt';
class MyImageClassifier extends Component {
constructor() {
super();
this.state = {};
try {
// Initialize TensorFlow Lite Image Recognizer
this.tfLiteImageRecognition = new TFLiteImageRecognition({
labels: Asset.fromModule(Labels).downloadAsync(),// Your label file in assets folder
model: Asset.fromModule(Model).downloadAsync() // Your tflite model in assets folder.
})
} catch (err) {
alert(err)
}
}
/*init (callback) {
// do something async and call the callback:
callback.bind(this)();
}*/
componentWillMount() {
let imagePath = Asset.fromModule(require("../../assets/images/anna.jpg"));
console.warn(imagePath);
this.classifyImage().then(console.log("Image Classified!"));
}
async classifyImage() {
try {
const results = await this.tfLiteImageRecognition.recognize({
image: Asset.fromModule(Anna).downloadAsync(), // Your image path.
inputShape: (1, 100, 100, 1), // the input shape of your model. If none given, it will be default to 224.
});
const resultObj = {
name: "Name: " + results[0].name,
confidence: "Confidence: " + results[0].confidence,
inference: "Inference: " + results[0].inference + "ms"
};
this.setState(resultObj)
} catch (err) {
alert(err)
}
}
componentWillUnmount() {
this.tfLiteImageRecognition.close() // Must close the classifier when destroying or unmounting component to release object.
}
render() {
return (
<View style={styles.container}>
<View>
<Text style={styles.results}>
{this.state.name}
</Text>
<Text style={styles.results}>
{this.state.confidence}
</Text>
<Text style={styles.results}>
{this.state.inference}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: 15,
backgroundColor: '#fff',
},
results: {
fontSize: 22,
fontFamily: "serif",
textAlign: 'center'
}
});
export default MyImageClassifier;
我已经测试过文件路径准确指向相关文件,因此在这里绝对不是问题。我还验证了我的TFLite模型在传递正确形状的输入数据时可以做出预测。
以下是我用作参考以帮助实现API的视频 Geeky Ants Tensorflow lite into React Native。
任何帮助将不胜感激。