2017-04-24 78 views
2

如何優化dlib界標檢測? 位圖160x120被處理7秒。 我想要50或100ms。 我的代碼:如何優化dlib界標檢測?

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

很多事情可以優化你的代碼:

  1. 沒有構建face_detector和shape_predictor對每個人臉檢測。它可能需要幾秒鐘。你可以爲你的所有線程使用一個shape_predictor,但face_detector應該是每個線程一個
  2. mFaceDet代碼不清楚。可能是您正在調整圖像大小或進行其他操作
  3. http://dlib.net/faq.html#Whyisdlibslow