我不知道您是在使用Flutter 构建android 应用还是iOS。
无论如何,要能够在您的 Flutter 应用上使用经过自定义训练的 Yolov3 模型,请执行以下两个步骤。
1.首先需要将训练好的yolov3模型转换为tflite版本:
您可以为此目的使用this repo。
将自定义训练的 Yolov3 darknet 权重保存到 tfmodel,这是 tflite 转换所需的:
python save_model.py --weights yolov3.weights --output ./checkpoints/yolov3-416 --input_size 416 --model yolov3 --framework tflite
将Yolov3模型转换为tflite版本:
python convert_tflite.py --weights ./checkpoints/yolov3-416 --output ./checkpoints/yolov3-416.tflite
2。然后你使用Flutter 插件访问TensorFlow-Lite API,它适用于android 和iOS - https://github.com/shaqian/flutter_tflite
a) 创建一个 assets 文件夹并将你的标签文件和模型文件放在
它。在pubspec.yaml 添加:
assets:
assets/labels.txt
assets/yolov3-416.tflite
b) 导入库:
import 'package:tflite/tflite.dart';
c) 加载模型和标签:
String res = await Tflite.loadModel(
model: "assets/yolov3-416.tflite",
labels: "assets/labels.txt",
numThreads: 1, // defaults to 1
isAsset: true, // defaults to true, set to false to load resources outside assets
useGpuDelegate: false // defaults to false, set to true to use GPU delegate
);
d) 在图像上运行:
var recognitions = await Tflite.detectObjectOnImage(
path: filepath, // required
model: "YOLOv3",
imageMean: 0.0,
imageStd: 255.0,
threshold: 0.3, // defaults to 0.1
numResultsPerClass: 2,// defaults to 5
anchors: anchors, // defaults to [0.57273,0.677385,1.87446,2.06253,3.33843,5.47434,7.88282,3.52778,9.77052,9.16828]
blockSize: 32, // defaults to 32
numBoxesPerBlock: 5, // defaults to 5
asynch: true // defaults to true
);
e) 释放资源:
await Tflite.close();