簡體   English   中英

Flutter Google-Ml-Kit-plugin - FaceDetector 在 iPhone 相機中不起作用

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

如果我們在 iPhone 上拍照

    final XFile? image = await picker.pickImage(
    source: ImageSource.camera,
    maxWidth: 300,
    maxHeight: 300,
    preferredCameraDevice: CameraDevice.front,
    );
final List faces = await faceDetector.processImage(inputImage);

總是給出空列表

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

如果來源:iPhone 中的 ImageSource.gallery,則效果很好

通過給予修復

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

然后

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM