Unable to get OpenCV 3.1 FPS over ~15 FPS
<p>I have some extremely simple performance testing code below for measuring the FPS of my webcam with OpenCV 3.1 + Python3 on a Late 2011 Macbook Pro:</p>

<pre><code>cap = cv2.VideoCapture(0)
count = 0
start_time = time.perf_counter()
end_time = time.perf_counter()
while (start_time + 1) &gt; end_time:
    count += 1
    cap.read()
    # Attempt to force camera FPS to be higher
    cap.set(cv2.CAP_PROP_FPS, 30)
    end_time = time.perf_counter()
print("Got count", count)
</code></pre>

<p>Doing no processing, not even displaying the image or doing this in another thread, I am only getting around 15 FPS. </p>

<p>Trying to access the FPS of the camera with cap.get(cv2.CAP_PROP_FPS) I get 0.0.</p>

<p>Any ideas why?</p>

<p>I've already searched the internet a fair amount for answers, so things I've thought about:</p>

<ul>
<li><p>I build OpenCV with release flags, so it shouldn't be doing extra debugging logic</p></li>
<li><p>Tried manually setting the FPS each frame (see above)</p></li>
<li><p>My FPS with other apps (e.g. Camera toy in Chrome) is 30FPS</p></li>
<li><p>There is no work being done in the app on the main thread, so putting the video capture logic in another thread as most other posts suggest shouldn't make a difference</p></li>
</ul>

<p>** EDIT ** 
Additional details: it seems like the first frame I capture is quick, then subsequent frames are slower; seems like this could be a buffer issues (i.e. the camera is being paused after the first frame because a new buffer must be allocated to write to?)</p>

<p>Tweaked the code to calculate the average FPS so far after each read:</p>

<pre><code> cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_CONVERT_RGB, False)
cap.set(cv2.CAP_PROP_FPS, 30)
start_time = time.perf_counter()
count = 0
cv2.CAP_PROP_FPS
end_time = time.perf_counter()
while True:
    count += 1
    ret, frame = cap.read()
    end_time = time.perf_counter()
    print("Reached FPS of: ", count / (end_time - start_time))
</code></pre>

<p>And I get one frame around 30FPS, and then subsequent frames are slower:</p>

<p>Reached FPS of:  27.805818385257446</p>

<p>Reached FPS of:  19.736237223924398</p>

<p>Reached FPS of:  18.173748156583795</p>

<p>Reached FPS of:  17.214809956810114</p>

<p>Reached FPS of:  16.94737657138959</p>

<p>Reached FPS of:  16.73624509452099</p>

<p>Reached FPS of:  16.33156408530572</p>

<p>IT'S NOT ANSWER. Since the comment in original question is too long for your attention. I post outside instead. </p>

<p>First, it's normal when CV_CAP_PROP_FPS return 0. OpenCV for Python just a wrapper for OpenCV C++. As far as I know, this property only works for video file, not camera. You have to calculate FPS yourself (like your edited). </p>

<p>Second, OpenCV have a bug that always convert the image get from camera to RGB <a href="https://github.com/opencv/opencv/issues/4831" rel="nofollow">https://github.com/opencv/opencv/issues/4831</a>. Normal camera usually use YUYV color. It's take a lot of time. You can check all supported resolution + fps <a href="https://trac.ffmpeg.org/wiki/Capture/Webcam" rel="nofollow">https://trac.ffmpeg.org/wiki/Capture/Webcam</a>. I see some camera not support RGB color and OpenCV force to get RGB and take terrible FPS. Due to camera limitation, in the same codec, the higher resolution, the slower fps. In different supported codec, the bigger output in same resolution, the slower fps. For example, my camera support yuyv and mjpeg, in HD resolution, YUYV have max 10 fps while MJPEG have max 30 fps. </p>

<p>So, first you can try ffmpeg executable to get frames. After identifying where error come from, if ffmpeg works well, you can use ffmpeg library (not ffmpeg executable) to get frame from your camera (OpenCV using ffmpeg for most video I/O including camera).</p>

<p>Be aware that the I only work with ffmpeg and OpenCV in C++ language, not Python. Using ffmpeg library is another long story. </p>

<p>Good luck! </p>

python c++ opencv video camera