react-native - 无法调用 TfliteReactNative.loadModel
问题描述
我正在尝试将 Tensorflow-Lite 与我的 React-Native 应用程序一起使用。基本上我使用的是 npm pakage,称为tflite-react-native
. 遵循文档后,我收到错误消息。
Could not invoke TfliteReactNative.loadModel
null
./models/mobilenet_v1_1.0_224.tflite
我并没有做错什么,但是当我通过单击它来选择模型时,它会显示上述错误。
以下是我尝试过的一些代码。
import React, { Component } from 'react';
import { Platform, StyleSheet, Image, Text, View, TouchableOpacity } from 'react-native';
import Tflite from 'tflite-react-native';
import ImagePicker from 'react-native-image-picker';
let tflite = new Tflite();
const height = 350;
const width = 350;
const blue = "#25d5fd";
const mobile = "mobilenet_v1_1.0_224";
const ssd = "ssd_mobilenet";
const yolo = "yolov2_tiny";
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
model: null,
source: null,
imageHeight: height,
imageWidth: width,
recognitions: []
};
}
onSelectModel(model) {
this.setState({ model:model });
switch (model) {
case ssd:
var modelFile = './models/ssd_mobilenet.tflite';
var labelsFile = './models/ssd_mobilenet.txt';
break;
case yolo:
var modelFile = './models/yolov2_tiny.tflite';
var labelsFile = './models/yolov2_tiny.txt';
break;
default:
var modelFile = './models/mobilenet_v1_1.0_224.tflite';
var labelsFile = './models/mobilenet_v1_1.0_224.txt';
}
tflite.loadModel({
model: modelFile,
labels: labelsFile,
},
(err, res) => {
if (err)
console.log(err);
else
console.log(res);
});
}
onSelectImage() {
const options = {
title: 'Select Avatar',
customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
storageOptions: {
skipBackup: true,
path: 'images',
},
};
ImagePicker.launchImageLibrary(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
var path = Platform.OS === 'ios' ? response.uri : 'file://' + response.path;
var w = response.width;
var h = response.height;
this.setState({
source: { uri: path },
imageHeight: h * width / w,
imageWidth: width
});
switch (this.state.model) {
case ssd:
tflite.detectObjectOnImage({
path,
threshold: 0.2,
numResultsPerClass: 1,
},
(err, res) => {
if (err)
console.log(err);
else
this.setState({ recognitions: res });
});
break;
case yolo:
tflite.detectObjectOnImage({
path,
model: 'YOLO',
imageMean: 0.0,
imageStd: 255.0,
threshold: 0.4,
numResultsPerClass: 1,
},
(err, res) => {
if (err)
console.log(err);
else
this.setState({ recognitions: res });
});
break;
default:
tflite.runModelOnImage({
path,
imageMean: 128.0,
imageStd: 128.0,
numResults: 3,
threshold: 0.05
},
(err, res) => {
if (err)
console.log(err);
else
this.setState({ recognitions: res });
});
}
}
});
}
renderBoxes() {
const { model, recognitions, imageHeight, imageWidth } = this.state;
if (model == mobile)
return recognitions.map((res, id) => {
return (
<Text key={id} style={{ color: 'black' }}>
{res["label"] + "-" + (res["confidence"] * 100).toFixed(0) + "%"}
</Text>
)
});
else
return recognitions.map((res, id) => {
var left = res["rect"]["x"] * imageWidth;
var top = res["rect"]["y"] * imageHeight;
var width = res["rect"]["w"] * imageWidth;
var height = res["rect"]["h"] * imageHeight;
return (
<View key={id} style={[styles.box, { top, left, width, height }]}>
<Text style={{ color: 'white', backgroundColor: blue }}>
{res["detectedClass"] + " " + (res["confidenceInClass"] * 100).toFixed(0) + "%"}
</Text>
</View>
)
});
}
render() {
const { model, source, imageHeight, imageWidth } = this.state;
var renderButton = (m) => {
return (
<TouchableOpacity style={styles.button} onPress={this.onSelectModel.bind(this, m)}>
<Text style={styles.buttonText}>{m}</Text>
</TouchableOpacity>
);
}
return (
<View style={styles.container}>
{model ?
<TouchableOpacity style={
[styles.imageContainer, {
height: imageHeight,
width: imageWidth,
borderWidth: source ? 0 : 2
}]} onPress={this.onSelectImage.bind(this)}>
{
source ?
<Image source={source} style={{
height: imageHeight, width: imageWidth
}} resizeMode="contain" /> :
<Text style={styles.text}>Select Picture</Text>
}
<View style={styles.boxes}>
{this.renderBoxes()}
</View>
</TouchableOpacity>
:
<View>
{renderButton(mobile)}
{renderButton(ssd)}
{renderButton(yolo)}
</View>
}
</View>
);
}
}
解决方案
当您没有包含以下脚本时会发生这种情况:在android/app/build.gradle中,在 android 块中添加以下设置。
aaptOptions {
noCompress 'tflite'
}
此链接中的更多详细信息:在此处输入链接描述
推荐阅读
- angular - Angular NGRX RXJS - 订阅其他可观察的内部效果不起作用
- nuget - 将整个解决方案打包成一个 nupkg?
- c# - 字符串定义中的条件
- react-native - 如何在 React Native 中使用 NativeModules 将图像设置为适合主屏幕的主屏幕壁纸
- azure-devops - 在队列中运行不同的发布管道
- sql - SQL Server 根据列的总和获取随机行
- opencv - 使用 VLC 接收 OpenCV GStreamer UDP 流
- python - del on field from vars(instance) 从对象中删除属性
- cassandra - 添加新节点后连接到 cassandra 集群
- vue.js - 重复的发出事件是否在父组件中重叠/一起调用?