OpenCV预测需要花费太多时间并冻结屏幕

时间:2017-09-12 06:31:25

标签: java android c++ opencv

我已尝试使用代码预测使用CameraBridgeViewBase

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mGray = inputFrame.gray();

    if (mAbsoluteFaceSize == 0) {
        int height = mGray.rows();
        if (Math.round(height * mRelativeFaceSize) > 0) {
            mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
        }
    }

    MatOfRect faces = new MatOfRect();
    if (mJavaDetector != null)
        mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2,
                new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());

    Rect[] facesArray = faces.toArray();

    if (facesArray.length == 0) {
        return mRgba;
    }
    if (faceType == PREDICT && canPredictNext) {
        canPredictNext = false;
        Mat m = mGray.submat(facesArray[0]);

        int[] label = new int[1];
        double[] d = new double[1];
        faceRecognizer.predict(m, label, d);

此屏幕冻结,直到此日志被打印。我可以知道如何避免这种情况吗?

        Log.v(TAG, "ID: "+label[0]+", confidence: "+d[0]);
        canPredictNext = true;
    }
    return mRgba;
}

predict冻结屏幕至少3秒

在FaceRecognizer类中预测。

public void predict(Mat mGrey, int[] label, double[] confidence) {
    predict(mGrey.getNativeObjAddr(), label, confidence);
}

原生预测方法声明为

public native void predict(long mats, int[] integer, double[] d);

此预测的JNI方法是

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_predict
        (JNIEnv *env, jobject thisObj, jlong images1, jintArray label, jdoubleArray confidence) {
    Mat &mGr = *(Mat *) images1;
    jint *labelBody = (*env).GetIntArrayElements(label, 0);
    jdouble *confidenceBody = (*env).GetDoubleArrayElements(confidence, 0);
    int &myLabel = labelBody[0];
    double &myConfidence = confidenceBody[0];
    model->predict(mGr, myLabel, myConfidence);
    (*env).ReleaseIntArrayElements(label, labelBody, 0);
    (*env).ReleaseDoubleArrayElements(confidence, confidenceBody, 0);
}

如何提高预测速度并解决预测问题?

0 个答案:

没有答案