如何优化dlib标志性检测?

时间:2017-04-24 18:55:47

标签: java android opencv libgdx dlib

如何优化dlib标志性检测? 位图160x120处理7秒。 我想50或100ms。 我的代码:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();

    ArrayList<android.graphics.Point> points = new ArrayList();
    try {
        long startTime = System.currentTimeMillis();
        points = LandmarkDetection.getLandmark(matToBitmap(mRgba), this, landmarkPath);
        long endTime = System.currentTimeMillis();
        Log.i(TAG +"Time cost: ", String.valueOf((endTime - startTime) / 1000f) + " sec");
        //drawPoint(points);
        Log.i(TAG, "size = " + String.valueOf(points.size()));
    }catch (Exception e) {
        Log.i(TAG, "bitmap error! " + e.getMessage());
    }
    return mRgba;
}

private Bitmap matToBitmap(@NonNull Mat mat) {
    Bitmap bmp;
    try {
        Mat resized = new Mat();
        Imgproc.resize(mat, resized, new Size(160, 120));
        bmp = Bitmap.createBitmap(resized.cols(), resized.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(resized, bmp);
    }catch(Exception e) {
        Log.e(TAG + ":matToBitmap", e.getMessage());
        return null;
    }
    return bmp;
}

和LandmarkDetection类(此方法一直占用):

public static ArrayList<Point> getLandmark(@NonNull Bitmap bmp, Context context, String landmarkPath) {
    mFaceDet = new FaceDet(landmarkPath);
    Log.i(AndroidLauncher.TAG, String.valueOf(new File(context.getExternalCacheDir() + "/shape_predictor_68_face_landmarks.dat").exists()));
    Log.i(AndroidLauncher.TAG, "Ширина: " + String.valueOf(bmp.getWidth()) + "\nВысота: " + String.valueOf(bmp.getHeight()));
    results = mFaceDet.detect(bmp);
    if (results != null) {
        for (final VisionDetRet ret : results) {
            landmarks = ret.getFaceLandmarks();
        }
    }
    return landmarks;
}

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

很多东西可以优化你的代码:

  1. 不为每个面部检测构造face_detector和shape_predictor。它可能需要几秒钟。你可以为所有线程使用一个shape_predictor,但face_detector应该是每个线程一个
  2. mFaceDet代码不清楚。可能是你在那里调整图像或进行其他操作
  3. http://dlib.net/faq.html#Whyisdlibslow