Angel
Angel

Reputation: 41

Flutter Google-Ml-Kit-plugin - FaceDetector not working in iPhone cameras

If we take photos in iPhone

    final XFile? image = await picker.pickImage(
    source: ImageSource.camera,
    maxWidth: 300,
    maxHeight: 300,
    preferredCameraDevice: CameraDevice.front,
    );
final List 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 faces = await faceDetector.processImage(inputImage);
print(faces.length); // IS ALWAYS ZERO

Works perfectly fine if source: ImageSource.gallery in iPhone

Upvotes: 2

Views: 707

Answers (1)

Angel
Angel

Reputation: 41

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);
  /.........../
}

Upvotes: 2

Related Questions