简体   繁体   English

Android OpenCV:如何放慢相机捕获的帧速率(有目的地)

[英]Android OpenCV: How to slow down camera capture frame rate (purposefully)

I'm currently developing an Android face detection app, and I'm wondering how I can slow down the capture frame rate purposefully. 我目前正在开发一个Android人脸检测应用程序,并且想知道如何有目的地降低捕获帧速率。 Currently it is around 30fps, and all I really require is 5-10fps. 目前大约是30fps,我真正需要的是5-10fps。 That way I don't need to use up additional processing which can be used on other tasks. 这样,我就不需要用尽可用于其他任务的其他处理。

I'm wondering if a Thread.sleep() is all that is needed to do the trick, or should I look into setting it via cvSetCaptureProperty(CvCapture* capture, int property_id, double value) ? 我想知道是否仅需要Thread.sleep()即可完成此操作, 或者我应该考虑通过 cvSetCaptureProperty(CvCapture* capture, int property_id, double value)吗? I read that it only works on certain cameras though, and is, for the most part, useless... 我读到它虽然只能在某些相机上使用,但在大多数情况下是没有用的...

I have also read about setting maximum frame size (eg mOpenCvCameraView.setMaxFrameSize(640, 480); ) but... it doesn't make sense to me to do that?.. 我也读过有关设置最大帧大小的信息(例如mOpenCvCameraView.setMaxFrameSize(640, 480); ),但是...这样做对我来说没有任何意义?

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

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

    MatOfRect faces = new MatOfRect();

    if (mDetectorType == JAVA_DETECTOR) {
        if (mJavaDetector != null)
            mJavaDetector.detectMultiScale(mGray, faces, SCALE_FACTOR, MIN_NEIGHBOURS, 2, 
                    new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
    }
    else if (mDetectorType == NATIVE_DETECTOR) {
        if (mNativeDetector != null)
            mNativeDetector.detect(mGray, faces);
    }
    else {
        Log.e(TAG, "Detection method is not selected!");
    }

    //put thread to sleep to slow capture?  
    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return mRgba;
}

Any advice is greatly appreciated! 任何意见是极大的赞赏! Thanks. 谢谢。

I don't recommend to you to use cvSetCaptureProperty() because its behaviour is very rhapsodic. 我不建议您使用cvSetCaptureProperty()因为它的行为非常夸张。

You should rather register the timestamp of last frame arrived (and processed) to onCameraFrame() and return from the event handler if the difference between last timestamp and now is less then ~100 ms. 如果最后一个时间戳与现在的时间戳之间的差小于100毫秒,则应该将最后一个到达(并处理)的帧的时间戳注册到onCameraFrame()并从事件处理程序返回。

You can use a counter. 您可以使用计数器。 Let's say fps=30, and you want to process only 5fs, then we have: 假设fps = 30,而您只想处理5fs,那么我们有:

class YourClass{
   int counter = 0;
   // Your code
   public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
      counter++;
      if (counter < 6)
          return null;
      counter = 0;
      //Your processing code here
   }

}

Running: 运行:

SystemClock.sleep(...); SystemClock.sleep(...);

in onCameraFrame works well for me and reduces power usage of the app. 在onCameraFrame中对我来说效果很好,并减少了该应用的功耗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM