Firebase_ml_vision not working properly on iOS
Steps to Reproduce
I've been struggling with this for about 24 hours now, and have come to the end of my ideas on how to address it.
I've written code which loads an image from Firebase, and detects a face in it - no problem at all. But when I then look to take a photo, using the standard imagePicker class in Flutter, despite me actually displaying the image on screen to check it's a valid file, the faceDetector can't find any faces in the image.
An edited down version of the code is below. Won't compile as there are things from my project missing, but hopefully you get the idea. The test platform is iOS (device, not emulator). Plugin is firebase_ml_vision 0.9.6+2, and the version of flutter is the latest from a few days ago.
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:image_picker/image_picker.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'dart:io';
import 'dart:async';
import 'package:camera/camera.dart';
class imageComparison extends StatefulWidget {
@override
_imageComparison createState() => _imageComparison();
}
class _imageComparison extends State<imageComparison> {
CameraController controller;
List<CameraDescription> cameras;
File chosenImage;
bool gotImage = false;
File grabbedImage;
FaceDetector faceDetector;
File tempImg;
@override
void initState() {
super.initState();
faceDetector = FirebaseVision.instance.faceDetector(FaceDetectorOptions(enableLandmarks: true, enableContours: true, enableClassification: true));
availableCameras().then((value) {
cameras = value;
controller = CameraController(cameras[1], ResolutionPreset.medium);
controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
});
});
}
@override
Widget build(BuildContext context) {
if (controller == null) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(),
);
}
if (!gotImage) {
File img;
downloadImage(imageURLs[0], 'verificationImage.jpg').then((value) {
img = tempImg = value;
extractFace(img).then((value2) {
print('got image');
gotImage = true;
setState(() {});
});
});
}
return Scaffold(
backgroundColor: Colors.white,
body: ModalProgressHUD(
inAsyncCall: globals.showSpinner,
child: Container(
width: globals.SizeConfig.blockSizeHorizontal * 100,
height: globals.SizeConfig.blockSizeVertical * 100,
decoration: BoxDecoration(
gradient: gradient,
),
child: Stack(
children: <Widget>[
Positioned(
left: globals.SizeConfig.blockSizeHorizontal * 10,
top: globals.SizeConfig.blockSizeVertical * 5,
child: Column(
children: [
FlatButton(
child: Text(
'check',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: globals.SizeConfig.blockSizeHorizontal * 4),
),
color: Colors.red,
onPressed: () async {
bool res = await takeAndAnalyseImage();
//other stuff
},
),
],
),
),
],
),
),
),
);
}
Future<bool> takeAndAnalyseImage() async {
//String filepath = await takePicture();
grabbedImage = File((await ImagePicker().getImage(source: ImageSource.camera)).path);
//grabbedImage = File(filepath);
setState(() {
print('setting state');
});
while (grabbedImage == null) {}
if (mounted) {
extractFace(grabbedImage).then((value) {
//irrelevant stuff
});
}
return null;
}
Future<Map<String, dynamic>> extractFace(File img) async {
FirebaseVisionImage visionImage;
visionImage = FirebaseVisionImage.fromFile(img);
Map<String, dynamic> facevals = Map<String, dynamic>();
List<Face> faces;
try {
faces = await faceDetector.processImage(visionImage);
} catch (e) {
print('can\'t extract face from image. e = ' + e.toString());
}
for (Face face in faces) {
//do irrelevant stuff to question.
}
return facevals;
}
}
Expected results: Capture image and detect face in picture.
Actual results: On Android, works as expected (see StackOverflow thread here: https://stackoverflow.com/questions/63971363/cant-get-face-detection-to-work-properly-using-flutter-and-firebase-ml-vision), but on iOS, face content always returns 0.
Logs
will submit privately, but too much sensitive information contained within the log.
Nothing of any concern here:
info • Name non-constant identifiers using lowerCamelCase •
lib/globals.dart:409:10 • non_constant_identifier_names
info • Name non-constant identifiers using lowerCamelCase •
lib/globals.dart:411:10 • non_constant_identifier_names
info • Name non-constant identifiers using lowerCamelCase •
lib/globals.dart:412:10 • non_constant_identifier_names
info • The value of the field '_consumables' isn't used •
lib/screens/shop_screen.dart:41:16 • unused_field
info • Avoid using braces in interpolation when not needed •
lib/screens/welcome_screen.dart:106:137 •
unnecessary_brace_in_string_interps
/Users/Col/Library/flutter/bin/flutter doctor --verbose [✓] Flutter (Channel stable, 1.20.4, on Mac OS X 10.15.6 19G2021, locale en-GB) • Flutter version 1.20.4 at /Users/Col/Library/flutter • Framework revision fba99f6cf9 (5 days ago), 2020-09-14 15:32:52 -0700 • Engine revision d1bc06f032 • Dart version 2.9.2
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc4) • Android SDK at /Users/Col/Library/Android/sdk • Platform android-29, build-tools 30.0.0-rc4 • ANDROID_HOME = /Users/Col/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.0) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.0, Build version 12A7209 • CocoaPods version 1.9.3
[✓] Android Studio (version 4.0) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 48.1.2 • Dart plugin version 193.7547 • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.45.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.10.2
[✓] Connected device (2 available) • Col’s work phone (mobile) • 9cb74a1c22fcfc4b60111933bff2defb5c7c64d8 • ios • iOS 13.7 • iPad (mobile) • 00008027-000878180E31002E • ios • iOS 13.6.1
• No issues found! Process finished with exit code 0
Same code works fine on android.
Hi @BigRedSwitch, Making sure, the iOS setup is per the documentation, right ?
Versions 0.7.0+ use the latest ML Kit for Firebase version which requires a minimum deployment target of 9.0. You can add the line platform :ios, '9.0' in your iOS project Podfile.
You may also need to update your app's deployment target to 9.0 using Xcode. Otherwise, you may see compilation errors.
If you're using one of the on-device APIs, include the corresponding ML Kit library model in your Podfile. Then run pod update in a terminal within the same directory as your Podfile.
pod 'Firebase/MLVisionBarcodeModel'
pod 'Firebase/MLVisionFaceModel'
pod 'Firebase/MLVisionLabelModel'
pod 'Firebase/MLVisionTextModel'
@darshankawar - that's all fine - the environment is set up correctly and the code compiles and runs on the device without error. The issue is that the on-device API will not work with photos taken or chosen by the imagePicker.
I would also appreciate it if you would re-add the 'bug' tag, as it is exactly that. Please see the StackOverflow thread for details.
@BigRedSwitch could it be related to #150? if you are on an iOS device and the camera is taking heic images, it is probably the reason why you do not get anything.
@jherrmann - no, I don't think so. I'm using the front facing camera and generating standard JPG images.
@jherrmann - verified as not the case. Used a package which converts HEICtoJPG from pub.dev and no difference in the resultant test.
facing the same issue tried installing all plugins for ml vision in pod. non of those works. read something about imagePicker plugin might cause the error. Let me know if removing imagePicker works and also sunstitute for the plugin.
same issue, with imagePicker, when image in iOS is taken by camera, no recognition. When taken from iOS folders, works fine
UPDATE: could be solved using this pub flutter_exif_rotation
Hey, :wave:, the firebase_ml_vision package is now discontinued since its APIs have been deprecated and removed from the Android & iOS Firebase SDKs.
I'd recommend switching to the alternatives now;
- for on-device vision APIs; switch to Google's standalone ML Kit library via google_ml_kit
- for cloud vision APIs; the recommended approach now is to use Firebase Authentication and Functions, which gives you a managed, serverless gateway to the Google Cloud Vision APIs. For an example Functions project see the vision-annotate-images sample project. You'd deploy this project for example and then call the cloud function from your app with your image data to get the annotation result.
Apologies for any inconvenience here and best of luck switching over the the new APIs. firebase_ml_custom is not affected by this deprecation.
Thanks