Reputation: 79
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.
Anyone have any ideas?
import 'package:flutter/material.dart';
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;
}
}
EDIT: The conversion of the image to 'FirebaseVisionImage'
appears to work fine - it's the FaceDetector.processImage
that fails...
Upvotes: 0
Views: 1449