使用视觉谷歌服务未检测到面部

问题描述 投票:0回答:2

https://developers.google.com/android/reference/com/google/android/gms/vision/face/FaceDetector.Builder

我在我的应用程序中使用上述谷歌服务进行面部检测。我确保我的手机有最低的谷歌播放服务版本,在我的手机上是8.3,但我仍然无法让面部检测工作!我通过在 Eclipse 项目中导入 google play 库来导入该库......这是代码:

           @Override
        protected void onPreExecute() 
        {
            detector = new FaceDetector.Builder(MainContext)
            .setTrackingEnabled(false)
            //.setProminentFaceOnly(true)
            .setLandmarkType(FaceDetector.ALL_LANDMARKS) //required
            .build();   
        }

private void detectTheFace(Bitmap converted) 
            {

                Frame frame = new Frame.Builder().setBitmap(converted).build();
                faces = detector.detect(frame);
             }   

我不知道是否有必要将用于检测面部的位图转换为 RGB_565 配置,但我还是这样做了。我尝试过更改和不更改 RGB 配置,结果都是相同的。基本上,面部稀疏数组的大小为 0,这意味着它永远不会检测到面部。顺便说一句,只是为了提供上述代码的一些上下文,我在异步任务中执行面部检测,因为我想在后台运行它。

android face-detection google-vision
2个回答
3
投票

我有同样的问题,即它在 Nexus 上运行良好,但在 Galaxy 上则不然。我通过将位图旋转 90 度解决了这个问题,以防 detector.detect() 方法给出零尺寸的面。因此调用 detector.detect() 后最大重试次数为 3 次,因为第 4 次旋转会提供相同的位图。

Bitmap rotateBitmap(Bitmap bitmapToRotate) {

        Matrix matrix = new Matrix();

        matrix.postRotate(90);

        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapToRotate, 0, 0,
                bitmapToRotate.getWidth(), bitmapToRotate.getHeight(), matrix,
                true);
        return rotatedBitmap;

    } 

检查 detector.detect() 返回的人脸大小是否为零,然后应运行以下代码。

if(!faces.size()>0){
if (rotationCounter < 3) {
                    rotationCounter++;
                   bitmap= rotateBitmap(bitmapToRotate);
//again call detector.detect() here
                                    }

}

无需编写上述代码即可检查是否需要旋转位图。从您的原始代码开始,代码尝试在手机的横向模式下捕获图像,或者将图像旋转到 90 度并捕获它。


-1
投票

要解决此问题,请使用照片 EXIF 中的方向规范。

© www.soinside.com 2019 - 2024. All rights reserved.