mlkit
一个 Flutter 插件,用于使用 Firebase ML Kit。
这不是官方包
Flutter团队现在有firebase_ml_vision或firebase_ml_custom用于Firebase ML Kit的包。请考虑尝试使用firebase_ml_vision。
注意:此插件仍在开发中,某些API可能尚不可用。反馈和Pull Requests都非常受欢迎!
功能
| 功能 | Android | iOS |
|---|---|---|
| 识别文本(设备上) | ✅ | ✅ |
| 识别文本(云端) | 尚 | 尚 |
| 检测人脸(设备上) | ✅ | ✅ |
| 扫描条形码(设备上) | ✅ | ✅ |
| 标记图片(设备上) | ✅ | ✅ |
| 标记图片(云端) | 尚 | 尚 |
| 对象检测与跟踪 | 尚 | 尚 |
| 识别地标(云端) | 尚 | 尚 |
| 语言识别 | ✅ | ✅ |
| 翻译 | 尚 | 尚 |
| 智能回复 | 尚 | 尚 |
| AutoML 模型推理 | 尚 | 尚 |
| 自定义模型(设备上) | ✅ | ✅ |
| 自定义模型(云端) | ✅ | ✅ |
用法
要使用此插件,请将mlkit添加为pubspec.yaml文件中的依赖项。
入门
查看example目录,了解使用Firebase Cloud Messaging的示例应用。
Android 集成
要将插件集成到您应用程序的Android部分,请遵循以下步骤
- 使用Firebase控制台将Android应用添加到您的项目中:按照助手操作,下载生成的
google-services.json文件并将其放在android/app内。接下来,修改android/build.gradle文件和android/app/build.gradle文件,根据Firebase助手的说明添加Google服务插件。
iOS 集成
要将插件集成到您应用程序的iOS部分,请遵循以下步骤
- 使用Firebase控制台将iOS应用添加到您的项目中:按照助手操作,下载生成的
GoogleService-Info.plist文件,使用Xcode打开ios/Runner.xcworkspace,然后在Xcode中将该文件放入ios/Runner。不要按照Firebase助手中的“添加Firebase SDK”和“添加初始化代码”步骤进行操作。
Dart/Flutter集成
从您的Dart代码中,您需要导入插件并实例化它
import 'package:mlkit/mlkit.dart';
FirebaseVisionTextDetector detector = FirebaseVisionTextDetector.instance;
// Detect form file/image by path
var currentLabels = await detector.detectFromPath(_file?.path);
// Detect from binary data of a file/image
var currentLabels = await detector.detectFromBinary(_file?.readAsBytesSync());
自定义模型解释器
import 'package:mlkit/mlkit.dart';
import 'package:image/image.dart' as img;
FirebaseModelInterpreter interpreter = FirebaseModelInterpreter.instance;
FirebaseModelManager manager = FirebaseModelManager.instance;
//Register Cloud Model
manager.registerRemoteModelSource(
FirebaseRemoteModelSource(modelName: "mobilenet_v1_224_quant"));
//Register Local Backup
manager.registerLocalModelSource(FirebaseLocalModelSource(modelName: 'mobilenet_v1_224_quant', assetFilePath: 'ml/mobilenet_v1_224_quant.tflite');
var imageBytes = (await rootBundle.load("assets/mountain.jpg")).buffer;
img.Image image = img.decodeJpg(imageBytes.asUint8List());
image = img.copyResize(image, 224, 224);
//The app will download the remote model. While the remote model is being downloaded, it will use the local model.
var results = await interpreter.run(
remoteModelName: "mobilenet_v1_224_quant",
localModelName: "mobilenet_v1_224_quant",
inputOutputOptions: FirebaseModelInputOutputOptions([
FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [1, 224, 224, 3])
], [
FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [1, 1001])
]),
inputBytes: imageToByteList(image));
// int model
Uint8List imageToByteList(img.Image image) {
var _inputSize = 224;
var convertedBytes = new Uint8List(1 * _inputSize * _inputSize * 3);
var buffer = new ByteData.view(convertedBytes.buffer);
int pixelIndex = 0;
for (var i = 0; i < _inputSize; i++) {
for (var j = 0; j < _inputSize; j++) {
var pixel = image.getPixel(i, j);
buffer.setUint8(pixelIndex, (pixel >> 16) & 0xFF);
pixelIndex++;
buffer.setUint8(pixelIndex, (pixel >> 8) & 0xFF);
pixelIndex++;
buffer.setUint8(pixelIndex, (pixel) & 0xFF);
pixelIndex++;
}
}
return convertedBytes;
}
// float model
Uint8List imageToByteList(img.Image image) {
var _inputSize = 224;
var convertedBytes = Float32List(1 * _inputSize * _inputSize * 3);
var buffer = Float32List.view(convertedBytes.buffer);
int pixelIndex = 0;
for (var i = 0; i < _inputSize; i++) {
for (var j = 0; j < _inputSize; j++) {
var pixel = image.getPixel(i, j);
buffer[pixelIndex] = ((pixel >> 16) & 0xFF) / 255;
pixelIndex += 1;
buffer[pixelIndex] = ((pixel >> 8) & 0xFF) / 255;
pixelIndex += 1;
buffer[pixelIndex] = ((pixel) & 0xFF) / 255;
pixelIndex += 1;
}
}
return convertedBytes.buffer.asUint8List();
}