I am trying to use the Tensorflow Lite ML Model with my project, and, unfortunately, I face an error while running my project:
↳
** BUILD FAILED **
Xcode's output:
↳
/Users/tejasravishankar/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/tflite-1.1.1/ios/Classes/TflitePlugin.mm:21:9: fatal error: 'metal_delegate.h' file not found
#import "metal_delegate.h"
^~~~~~~~~~~~~~~~~~
1 error generated.
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description
Could not build the application for the simulator.
Error launching application on iPhone 11 Pro Max.
I have tried flutter clean
, and have tried removing the Podfile
and Podfile.lock
from the ios
directory, though that didn't change anything.
Here is my code:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:tflite/tflite.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(TensorflowApp());
const String pet = 'Pet Recognizer';
class TensorflowApp extends StatefulWidget {
@override
_TensorflowAppState createState() => _TensorflowAppState();
}
class _TensorflowAppState extends State<TensorflowApp> {
String _model = pet;
File _image;
double _imageWidth;
double _imageHeight;
// ignore: unused_field
bool _isLoading = false;
List _predictions;
_selectFromImagePicker() async {
PickedFile _pickedImage =
await ImagePicker().getImage(source: ImageSource.gallery);
File _pickedImageFile = _pickedFileFormatter(_pickedImage);
if (_pickedImage == null) {
return;
} else {
setState(() {
_isLoading = true;
});
_predictImage(_pickedImageFile);
}
}
_predictImage(File image) async {
await _petRecognizerV1(image);
FileImage(image).resolve(ImageConfiguration()).addListener(
ImageStreamListener(
(ImageInfo info, bool _) {
setState(() {
_imageWidth = info.image.height.toDouble();
_imageHeight = info.image.height.toDouble();
});
},
),
);
setState(() {
_image = image;
_isLoading = false;
});
}
_petRecognizerV1(File image) async {
List<dynamic> _modelPredictions = await Tflite.detectObjectOnImage(
path: image.path,
model: pet,
threshold: 0.3,
imageMean: 0.0,
imageStd: 255.0,
numResultsPerClass: 1,
);
setState(() {
_predictions = _modelPredictions;
});
}
_pickedFileFormatter(PickedFile pickedFile) {
File formattedFile = File(pickedFile.path);
return formattedFile;
}
renderBoxes(Size screen) {
if (_predictions == null) {
return [];
} else {
if (_imageHeight == null || _imageWidth == null) {
return [];
}
double factorX = screen.width;
double factorY = _imageHeight / _imageHeight * screen.width;
return _predictions.map((prediction) {
return Positioned(
left: prediction['rect']['x'] * factorX,
top: prediction['rect']['y'] * factorY,
width: prediction['rect']['w'] * factorX,
height: prediction['rect']['h'] * factorY,
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.green, width: 3.0),
),
child: Text(
'${prediction["detectedClass"]} ${(prediction["confidenceInClass"]) * 100.toStringAsFixed(0)}',
style: TextStyle(
background: Paint()..color = Colors.green,
color: Colors.white,
fontSize: 15.0,
),
),
),
);
}).toList();
}
}
@override
void initState() {
super.initState();
_isLoading = true;
_loadModel().then((value) {
setState(() {
_isLoading = false;
});
});
}
_loadModel() async {
Tflite.close();
try {
String response;
if (_model == pet) {
response = await Tflite.loadModel(
model: 'assets/pet_recognizer.tflite',
labels: 'assets/pet_recognizer.txt',
);
}
} catch (error) {
print(error);
}
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Text('TFLite Test'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.image),
tooltip: 'Pick Image From Gallery',
onPressed: () => _selectFromImagePicker,
),
body: Stack(
children: <Widget>[
Positioned(
top: 0.0,
left: 0.0,
width: size.width,
child: _image == null
? Text('No Image Selected')
: Image.file(_image),
),
renderBoxes(size),
],
),
),
);
}
}
I personally don't think that there is a problem with my code, and I tried running
flutter pub get
which has worked successfully with success code 0 , a few more times, though it hasn't fixed the problem...
I am not very sure what to do in order to move on with this and would really appreciate any help I receive! Thanks, cheers and I appreciate your help :)
Flutter run gave me a Could not locate configuration file: 'GoogleService-Info.plist' error, this was easily solved by getting the latest GooglService file from Firebase and adding in through X-code. Then flutter run, ran on my physical device and voila ! Or you can simply delete the ios directory and do "flutter create ."
Run flutter create (your project name). in the previous folder where you have your project ( cd users/user/"projects_folder" ) (this will recreate your ios folder). Paste your Runner backup in the ios folder (into the project). Open Runner.xcworkspace (into ios folder) and there, check the Version, the Bundle ID, all the info.
Yet another approach... If you stumble across this issue in a Flutter project that uses Firebase, and non of the above steps worked, try the following: Backup your project (just in case). Delete the whole ios folder insider your Flutter project. Call flutter create <projectname> at the parent folder of your flutter project (i.e. one folder above).
The API is similar to the TFLite Java and Swift APIs. It directly binds to TFLite C API making it efficient (low-latency). Offers acceleration support using NNAPI, GPU delegates on Android, Metal and CoreML delegates on iOS, and XNNPack delegate on Desktop platforms.
Downgrading TensorFlowLiteC to 2.2.0 worked for me
See https://github.com/shaqian/flutter_tflite/issues/139#issuecomment-668252599
In your Podfile
add this to the end:
pod 'TensorFlowLiteC', '2.2.0'
Delete Podfile.lock
Delete Pods
folder
Delete .symlinks
folder
flutter run
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With