Page 1 of 1

Low framerate on Raspberry PI 4 + PiCamera + Python + OpenCv

Posted: Sat Feb 15, 2020 7:10 pm
by mateus_guilherme
Hello

I'm trying to get continuous video from a PiCamera on a raspberry PI 4. I need a minimum resolution of 1280x720p and 30 FPS. I'm using Python 2.7 and OpenCV. In the future OpenCV will try to identify a Marker Aruco, but at the moment it only displays the video stream.

With low resolutions (320X240) the FPS is very good, but with higher resolutions ( 720p or 1080p) the FPS is very slow and many frames are lost.

I used this tutorial trying to increase the FPS:
https://www.pyimagesearch.com/2015/12/2 ... nd-opencv/

I noticed that with the "raspvid" command (raspivid -o - -t 0 -w 1920 -h 1080 -fps 30 -b 250000) the frame rate is excellent even at high resolutions like 1080p so that means there is enough hardware for this. The problem is getting the same resolution and FPS using a python script with opecv (remembering that opencv is not doing any image processing just displaying it on the screen)

Here is my code:

Code: Select all

# import the necessary packages
from __future__ import print_function
from imutils.video.pivideostream import PiVideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--num-frames", type=int, default=1000,
        help="# of frames to loop over for FPS test")
ap.add_argument("-d", "--display", type=int, default=-1,
        help="Whether or not frames should be displayed")
args = vars(ap.parse_args())

# created a *threaded *video stream, allow the camera sensor to warmup,
# and start the FPS counter
print("[INFO] sampling THREADED frames from `picamera` module...")
vs = PiVideoStream((1280, 720), 30).start()
time.sleep(2.0)
fps = FPS().start()
# loop over some frames...this time using the threaded stream
while fps._numFrames < args["num_frames"]:
        # grab the frame from the threaded video stream and resize it
        # to have a maximum width of 400 pixels
        frame = vs.read()
        # check to see if the frame should be displayed to our screen
        if args["display"] > 0:
                cv2.imshow("Frame", frame)
                key = cv2.waitKey(1) & 0xFF
        # update the FPS counter
        fps.update()
# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

Run: python2.7 teste.py --display 1

Any suggestion?

Re: Low framerate on Raspberry PI 4 + PiCamera + Python + OpenCv

Posted: Sat Feb 22, 2020 9:39 pm
by mateus_guilherme
Hello

For all those who are experiencing problems with low FPS on the Raspberry PI using OpenCv and Python use this script (author CarlosGS). I got very good results, but it will only work with the PI Camera. The python script uses the raspibian native raspividYUV command and obtains the gray scale image with high FPS. If anyone knows a similar way to test with a USB webcam it would be interesting.

https://gist.github.com/CarlosGS/b8462a ... bb0f3a4d63