flutter-ml / google_ml_kit_flutter

A flutter plugin that implements Google's standalone ML Kit
MIT License
997 stars 751 forks source link

FaceDetector finds empty #513

Closed easylolicon closed 1 month ago

easylolicon commented 1 year ago
    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 []

fbernaly commented 1 year ago

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

go2hyder commented 1 year ago

Same issue in IOS

easylolicon commented 1 year ago

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

easylolicon commented 1 year ago

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 再去识别 就有结果

RounakTadvi commented 11 months ago
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

laoduan395 commented 9 months ago

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

}

github-actions[bot] commented 7 months ago

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

github-actions[bot] commented 1 month ago

This issue was closed because it has been inactive for 14 days since being marked as stale.