谷歌视觉人脸检测需要很长时间



直接从谷歌复制代码:链接

给我 7 秒的等待时间,以便使用 2MB 图像检测到人脸。如果我将位图压缩 10 倍,它会给我 0.05 秒的人脸检测时间。但是,我找不到其他任何地方的人建议您在运行人脸检测之前压缩位图,事实上,文档建议只需 10 毫秒即可检测人脸,当我使用相机作为图像源时,它会实时完成。

相关未回答的问题:移动视觉 API 检测人脸的时间过长

只是想知道这是否正常,或者我做错了什么:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private int FACE_DETECTION_RATIO_MINIMUM = 10;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: 1");
            ImageView myImageView = (ImageView) findViewById(R.id.imageView);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inMutable=true;
            Bitmap myBitmap = BitmapFactory.decodeResource(
                    getApplicationContext().getResources(),
                    R.drawable.test2,
                    options);
            Log.d(TAG, "onClick: 2");
            Log.d(TAG, "onClick: width: " + myBitmap.getWidth() + " height: " + myBitmap.getHeight());

            Paint myRectPaint = new Paint();
            myRectPaint.setStrokeWidth(5);
            myRectPaint.setColor(Color.RED);
            myRectPaint.setStyle(Paint.Style.STROKE);
            Log.d(TAG, "onClick: 3");

            Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, myBitmap.getWidth()/10, myBitmap.getHeight() /10, false);
            Log.d(TAG, "scaled width: " + scaledBitmap.getWidth() + " height: " + scaledBitmap.getHeight());
            //tempBitmap.compress(Bitmap.CompressFormat.JPEG, 40, tempBitmap)
            Canvas tempCanvas = new Canvas(tempBitmap);
            Canvas tempCanvasScaled = new Canvas(scaledBitmap);
            tempCanvas.drawBitmap(myBitmap, 0, 0, null);
            Log.d(TAG, "onClick: 4");

            FaceDetector faceDetector = new
                    FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).setMode(FAST_MODE).setLandmarkType(NO_LANDMARKS).setClassificationType(NO_CLASSIFICATIONS)
                    .build();
            if(!faceDetector.isOperational()){
                new AlertDialog.Builder(v.getContext()).setMessage("Could not set up the face detector!").show();
                return;
            }
            Log.d(TAG, "onClick: 5");

            Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
            Frame frameScaled = new Frame.Builder().setBitmap(scaledBitmap).build();
            Log.d(TAG, "onClick: 5.1");
            SparseArray<Face> faces = faceDetector.detect(frameScaled);

         -->>   SparseArray<Face> faces2 = faceDetector.detect(frame); <-- Takes 7 seconds
            Log.d(TAG, "onClick: 6");
            for(int i=0; i<faces.size(); i++) {
                Face thisFace = faces.valueAt(i);
                float x1 = thisFace.getPosition().x;
                float y1 = thisFace.getPosition().y;
                float x2 = x1 + thisFace.getWidth();
                float y2 = y1 + thisFace.getHeight();
                tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
                Log.d(TAG, "onClick: x,y,x,y: " + x1 +", " + y1 + ", " +  x2 +", " + y2);
            }
            for(int i=0; i<faces2.size(); i++) {
                Face thisFace = faces2.valueAt(i);
                float x1 = thisFace.getPosition().x;
                float y1 = thisFace.getPosition().y;
                float x2 = x1 + thisFace.getWidth();
                float y2 = y1 + thisFace.getHeight();
                tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
                Log.d(TAG, "onClick: x,y,x,y: " + x1 +", " + y1 + ", " +  x2 +", " + y2);
            }
            Log.d(TAG, "onClick: 5");
            myImageView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));
            faceDetector.release();
            //cropImage(x1,y1,x2,y2);

        }
    });

}

我在使用 vision 17.0.2/18.0.0 版本时遇到了同样的问题,我通过降级视觉库版本克服了它。

您可以使用这些设置

对于顶级构建.gradle

dependencies {
    //Google analytics
    classpath 'com.google.gms:google-services:4.3.2'
}

对于应用级构建.gradle

dependencies {
    implementation 'com.google.android.gms:play-services-vision:16.2.0'
}

如果您使用的是 Analytics 或 Firebase,则这些版本与 Vision 16.2.0 兼容

dependencies {
        //Google analytics
        implementation 'com.google.android.gms:play-services-analytics:16.0.6'
        //firebase
        implementation 'com.google.firebase:firebase-core:16.0.6'
        implementation 'com.google.firebase:firebase-messaging:17.3.4'
}

相关内容

  • 没有找到相关文章

最新更新