google_ml_kit_flutter icon indicating copy to clipboard operation
google_ml_kit_flutter copied to clipboard

FaceDetector not working in iPhone cameras

Open AngelEric22 opened this issue 3 years ago • 1 comments

If we take photos in iPhone final XFile? image = await picker.pickImage( source: ImageSource.camera, maxWidth: 300, maxHeight: 300, preferredCameraDevice: CameraDevice.front, ); final List<Face> faces = await faceDetector.processImage(inputImage); always give empty list

final FaceDetector faceDetector = FaceDetector( options: FaceDetectorOptions( enableTracking: true, enableContours: true, enableClassification: true, enableLandmarks: true, performanceMode: FaceDetectorMode.accurate));

final inputImage = InputImage.fromFilePath(image.path); final List<Face> faces = await faceDetector.processImage(inputImage); print(faces.length); // IS ALWAYS ZERO

Works perfectly fine if source: ImageSource.gallery in iPhone

AngelEric22 avatar Aug 09 '22 11:08 AngelEric22

Fixed by Giving import 'dart:math' as Math; import 'dart:math'; import 'package:image/image.dart' as Im;

class CompressObject {
  File imageFile;
  String path;
  int rand;
 CompressObject(this.imageFile, this.path, this.rand);
    }

     Future<String> compressImage(CompressObject object) async {
      return compute(decodeImage, object);
    }

     String decodeImage(CompressObject object) {
        Im.Image? image = Im.decodeImage(object.imageFile.readAsBytesSync());
         I.m.Image smallerImage = Im.copyResize(
             image!,width: 200,height: 200 ); // choose the size here, it will maintain aspect ratio
          var decodedImageFile = File(object.path + '/img_${object.rand}.jpg');
        decodedImageFile.writeAsBytesSync(Im.encodeJpg(smallerImage, quality: 85));
       return decodedImageFile.path;
    }

then

                     void chooseImage(bool isFromGallery) async {
                     final XFile? image = await picker.pickImage(
                     source: isFromGallery ? ImageSource.gallery : ImageSource.camera,
                     preferredCameraDevice: CameraDevice.front,
                   );
                   if (image != null) {
                     if (Platform.isIOS) {
                        final tempDir = await getTemporaryDirectory();
                         final rand = Math.Random().nextInt(10000);
                        CompressObject compressObject =
                            CompressObject(File(image.path), tempDir.path, rand);
                         String filePath = await compressImage(compressObject);
                          print('new path: ' + filePath);
                          file = File(filePath);
                      } else
                   {
                        file = File(image.path);
                      } 
                 }
            final inputImage = InputImage.fromFilePath(i file.path);
           final List<Face> faces = await faceDetector.processImage(inputImage);

AngelEric22 avatar Aug 10 '22 08:08 AngelEric22