3

I am trying to process frames from my V2 RPI Camera at high framerates and am stuck with the picamera module. The VideoCapture class from OpenCV seems to be much faster than using PiCamera.capture_continuous(). Here are my two different test codes:

from picamera import PiCamera
from picamera.array import PiRGBArray
import time
import cv2

* PiCamera *

camera = PiCamera(framerate = 40)
time.sleep(2)
camera.resolution = (640,480)
rawCapture = PiRGBArray(camera, size=camera.resolution)
start = time.time()
for frame, i in zip(camera.capture_continuous(rawCapture, format="bgr", use_video_port=True), range(400)):
    image = frame.array
    rawCapture.truncate(0)
print("Time for {0} frames: {1} seconds".format(frames ,time.time()-start))

* OpenCV *

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cap.set(cv2.CAP_PROP_FPS, 40)
start = time.time()
for i in range(400):
    ret, img = cap.read()
print("Time for {0} frames: {1} seconds".format(frames, time.time() - start))

The outpus are:

Time for 400 frames: 25.839843720129837403 seconds
Time for 400 frames: 10.234732929110973438 seconds

Why is OpenCV so much faster? I would prefer to use picamera but I cannot get it to perform as expected. Thanks!

3
  • I don't think iterating over a list adds overheads of 15 sec.. I removed zip() and it is still only running around 18 Hz instead of 40 Hz. I think it is rather something with the sensormode or the resolution, but I don't understand what. Jun 10, 2019 at 12:49
  • for picam you use 'format="bgr"' format, and for opencv ?
    – Ephemeral
    Jun 10, 2019 at 12:51
  • Same, default format with openCV VideCapture.read() is BGR Jun 10, 2019 at 13:34

3 Answers 3

1

For anyone having the same issues: It turns out that the above code uses too little CPU load and therefore does not shoot up the frequency. When I checked, my CPU was constantly running at 700 Hz. To achieve the required FPS on the above resolution, I changed my cpufreq governor from ondemand to performance. This might not be the only or the best solution, but it works. An alternative would be to lower the CPU thresholds, at which the frequency goes up to maximum.

0

You can not use openCV in this way to acces picamera. Try cap.isOpened() it should evaluate FALSE.

Just work with picamera module. For rapid capturing refer: https://picamera.readthedocs.io/en/release-1.13/recipes2.html#rapid-capture-and-processing

0

How did you accessed the Picamera using Opencv? I tried a lot and wasn't able to access the camera using cv2.VideoCapture(0).

1
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Milliways
    May 24, 2023 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.