简体   繁体   English

适用于 Android 的 Firebase MLKit Facedetection 慢

[英]Firebase MLKit Facedetection Slow For Android

I want to detect face from gallery Image (Bitmap).我想从图库图像(位图)中检测人脸。

Issues问题

  1. I noticed that Firebase MLKIT is performing very slow on Gallery Image Bitmap.我注意到 Firebase MLKIT 在 Gallery Image Bitmap 上的执行速度非常慢。
  2. Can i still used mobile vision api for detection of face in image.( I want only detect face, dont want eyes,nose,etc)我还可以使用移动视觉 api 来检测图像中的人脸吗。(我只想检测人脸,不想要眼睛、鼻子等)
  3. What should i do to improve performance for detecting face with Firebase MLKIT.我应该怎么做才能提高使用 Firebase MLKIT 检测人脸的性能。
  4. I used Firebase Image Labeling.我使用了 Firebase 图像标签。 Firebase Image Labeling is performed fast but Face Detection is very slow comparatively. Firebase 图像标记执行速度很快,但人脸检测相对来说非常慢。

I tried with Mobile vision Api and detected face successfully.我尝试使用 Mobile vision Api 并成功检测到人脸。 On website of mobile vision api, they have mentioned about Firebase MLKIT.在移动视觉 api 的网站上,他们提到了 Firebase MLKIT。 I also tried firebase ML Kit and detected face successfully.我还尝试了 firebase ML Kit 并成功检测到人脸。 I followed this link for demo: [ https://github.com/hitanshu-dhawan/FirebaseMLKit]我按照此链接进行演示:[ https://github.com/hitanshu-dhawan/FirebaseMLKit]

Library Versions:库版本:

implementation 'com.google.firebase:firebase-core:17.0.1'
implementation 'com.google.firebase:firebase-ml-vision:22.0.0'
implementation 'com.google.firebase:firebase-ml-vision-face-model:18.0.0'
implementation 'com.google.firebase:firebase-ml-vision-image-label-model:18.0.0' 

    FirebaseVisionFaceDetectorOptions option =
     new FirebaseVisionFaceDetectorOptions.Builder()
    .setPerformanceMode(FirebaseVisionFaceDetectorOptions.ACCURATE)
    .setLandmarkMode(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
    .setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
    .build();

    FirebaseVisionFaceDetector detector = FirebaseVision.getInstance()
                .getVisionFaceDetector(option);

        detector.detectInImage(image).addOnSuccessListener(
                new OnSuccessListener<List<FirebaseVisionFace>>() {
                    @Override
                    public void onSuccess(List<FirebaseVisionFace> faces) {
    }

Is am i doing something wrong?我做错了什么吗?

I think you can change .setPerformanceMode(FirebaseVisionFaceDetectorOptions.ACCURATE) To .setPerformanceMode(FirebaseVisionFaceDetectorOptions.FAST)我认为您可以将.setPerformanceMode(FirebaseVisionFaceDetectorOptions.ACCURATE)更改为.setPerformanceMode(FirebaseVisionFaceDetectorOptions.FAST)

May be it will improve speed of detecting可能会提高检测速度

I have the same issue but with text recognition, very slow on Android, around 1 image per second (Huawei Y6 2018) but incredibly fast on iOS, I can easily run 10 frames per second on iPhone 6s.我有同样的问题,但文本识别在 Android 上非常慢,每秒大约 1 张图像(华为 Y6 2018)但在 iOS 上非常快,我可以轻松地在 iPhone 6s 上每秒运行 10 帧。 I have tried all image formats which Firebase support, during my tests fastest varian is ByteBuffer, so I convert Bitmap to ByteBuffer before start recognision.我已经尝试了 Firebase 支持的所有图像格式,在我的测试中最快的变量是 ByteBuffer,所以我在开始识别之前将 Bitmap 转换为 ByteBuffer。

Firebase face detector is very slow if you instantiate your FirebaseVisionImage directly from a bitmap, as in:如果你实例化火力地堡的人脸检测器是很慢的FirebaseVisionImage直接从位图,如下所示:

FirebaseVisionImage visionImage = FirebaseVisionImage.fromBitmap(bitmap);

A solution is to convert the bitmap into a byte array ( byte[] ) and use this other constructor to create the FirebaseVisionImage :一种解决方案是将位图转换为字节数组 ( byte[] ) 并使用其他构造函数来创建FirebaseVisionImage

FirebaseVisionImage visionImage = FirebaseVisionImage.fromByteArray(byteArray, metadata);

This fact does not seem to be documented.这个事实似乎没有记录。 I found it in a comment on this GitHub issue and used the proposed technique, reducing face detection time 6 times aprox.在这个 GitHub 问题的评论中找到了它,并使用了建议的技术,将人脸检测时间减少了大约 6 倍。 Also in that link there's a code snippet from GitHub user jllarraz to transform the bitmap to an nv21 byte array.在该链接中还有来自 GitHub 用户jllarraz的代码片段,用于将位图转换为nv21字节数组。

Thanks for above solution but finally i am able to use face detector at very fast.感谢上述解决方案,但最终我能够非常快速地使用人脸检测器。 I just calculate insamplesize of the bitmap and reduce the size of bitmap.我只是计算位图的 insamplesize 并减小位图的大小。 As bitmap size is very less, able to process face detection at much faster and then once i got the coordinate, again map that coordinate with original image by multiplying insamplesize.由于位图大小非常小,能够以更快的速度处理人脸检测,然后一旦获得坐标,再次通过乘以 insamplesize 将该坐标与原始图像映射。 This way i achieved image processing with FirebaseVisionFaceDetectorOptions at fast speed.通过这种方式,我使用 FirebaseVisionFaceDetectorOptions 快速实现了图像处理。 Below is code for calculating insamplesize.下面是计算 insamplesize 的代码。

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM