google_ml_kit_flutter icon indicating copy to clipboard operation
google_ml_kit_flutter copied to clipboard

FaceDetector finds empty

Open easylolicon opened this issue 2 years ago • 7 comments

    XFile? xFile = await state.cameraController?.takePicture();
    if (xFile == null) {
      print('pic is null');
      return;
    }

    /// faceDetector
    InputImage inputImage = InputImage.fromFilePath(xFile.path);
    final faceDetector = FaceDetector(options: FaceDetectorOptions(enableContours: true, enableLandmarks: true));
    final List<Face> faceList = await faceDetector.processImage(inputImage);
    faceDetector.close();
    if (faceList.isEmpty) {
      print('empty');
      return;
    }
    print(faceList);

echo []

easylolicon avatar Aug 22 '23 04:08 easylolicon

Please provide more details: Android or iOS? plugin version? a better description, etc.. some random code is not a nice way to report an issue

fbernaly avatar Aug 22 '23 16:08 fbernaly

Same issue in IOS

go2hyder avatar Sep 12 '23 09:09 go2hyder

就是在ios中 拍照拿到 的图片 识别不出人脸,但是将图片保存到文件中,再拿来识别就可以。 The image taken in iOS cannot recognize a face, but it can be saved to a file and used for recognition.

easylolicon avatar Sep 12 '23 10:09 easylolicon

XFile? xFile = await state.cameraController?.takePicture(); //// Directly using xFile to recognize faces with empty results //// 直接使用 xFile 识别人脸结果为空

File imageFile = File(xFile.path); imageFile.writeAsBytesSync(img.encodeJpg(image)); /// Save the xFile obtained from the photo to Jpg and then identify it, and the result will be obtained /// 将 拍照拿到的xFile 保存到Jpg 再去识别 就有结果

easylolicon avatar Sep 12 '23 10:09 easylolicon

import 'dart:io';
import 'dart:math';
import 'dart:ui';

import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_mlkit_face_detection/google_mlkit_face_detection.dart';
import 'package:image_picker/image_picker.dart';
import 'package:injectable/injectable.dart';
import 'package:path_provider/path_provider.dart';

part 'face_detection_state.dart';

@injectable
class FaceDetectionCubit extends Cubit<FaceDetectionState> {
  FaceDetectionCubit()
      : super(
          FaceDetectionInitialState(),
        );
  final FaceDetector _faceDetector = FaceDetector(
    options: FaceDetectorOptions(
      enableContours: true,
      enableLandmarks: true,
      enableTracking: true,
      enableClassification: true,
    ),
  );

  Future<void> detectFace({required XFile file}) async {
    emit(FaceDetectionLoadingState());
    final File pickedImage = File(file.path);
    final Directory appDocDir = await getApplicationDocumentsDirectory();
    final i = await pickedImage.copy('${appDocDir.path}/${file.name}');
    final InputImage inputImage = InputImage.fromFile(i);

    final List<Face> faces = await _faceDetector.processImage(inputImage);
    if (faces.isNotEmpty) {
      debugPrint('Face not empty: ${faces.length}');
      for (final Face face in faces) {
        final Rect boundingBox = face.boundingBox;

        debugPrint(boundingBox.toString());

        final double? rotX =
            face.headEulerAngleX; // Head is tilted up and down rotX degrees
        final double? rotY =
            face.headEulerAngleY; // Head is rotated to the right rotY degrees
        final double? rotZ =
            face.headEulerAngleZ; // Head is tilted sideways rotZ degrees
        debugPrint(rotX.toString());
        debugPrint(rotY.toString());
        debugPrint(rotZ.toString());
        // If landmark detection was enabled with FaceDetectorOptions
        // (mouth, ears, eyes, cheeks, and nose available):
        final FaceLandmark? leftEar = face.landmarks[FaceLandmarkType.leftEar];
        if (leftEar != null) {
          final Point<int> leftEarPos = leftEar.position;
          debugPrint(leftEarPos.toString());
        }

        // If face tracking was enabled with FaceDetectorOptions:
        if (face.trackingId != null) {
          final int? id = face.trackingId;
          debugPrint(id.toString());
        }
      }
      if (faces.length == 1) {
        emit(FaceDetectionLoadedState());
      }
    } else {
      debugPrint(' empty faces');
      emit(FaceDetectionErrorState());
    }
  }

  @override
  Future<void> close() {
    _faceDetector.close();
    return super.close();
  }
}

Above solution worked for me

rounaktadvi avatar Dec 28 '23 18:12 rounaktadvi

I have encountered the same issue. When using the front camera by default, analyzing faces through 'CameraImage' and capturing an image with 'cameraController?.takePicture()', then analyzing it using 'InputImage.fromFilePath(path)' always results in 'faces.length' being zero. Currently, this problem exists only on iOS, while Android behaves normally. I have also attempted to use '_imagePicker?.pickImage' to recognize photos from the iOS gallery. Even in cases where there is only one clear face in the photo, the recognition result is still null. Interestingly, when using the iOS front camera in portrait mode, which blurs the background except for the face, face recognition works correctly.

camera: ^0.10.5+9 google_mlkit_face_detection: ^0.9.0 image_picker: ^1.0.2

   await cameraController?.stopImageStream();
    XFile? file = await cameraController?.takePicture();
    if (file != null) {
      capturingImagePath.value = file.path;
    }
    
      Future<void> testIMG () async {
// final pickedFile = await _imagePicker?.pickImage(source: ImageSource.gallery);
try {

  FaceDetector faceDetectorTest = FaceDetector(
    options: FaceDetectorOptions(
      performanceMode: FaceDetectorMode.accurate,
      enableContours: true,
      enableLandmarks: true,
      enableTracking: true,
      enableClassification: true,
    )
  );
  InputImage inputImage = InputImage.fromFilePath(capturingImagePath.value);
  // InputImage inputImage = InputImage.fromFilePath(pickedFile!.path);
  List<Face> faces = await faceDetectorTest.processImage(inputImage);
  print('faces ========${faces.length}');
  for (Face face in faces) {
  }
} catch (e) {
  Log.i('Erro', e);
}

}

laoduan395 avatar Feb 27 '24 10:02 laoduan395

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Apr 18 '24 12:04 github-actions[bot]