Yllier123
Yllier123

Reputation: 76

cv2.VideoCapture().read() from IP camera results in choppy framerate

I have a function that calls cv2.VideoCapture on an RTSP stream from a IP camera:

vidcap = cv2.VideoCapture("rtsp://usr:passw@my_camera:1234/") 

Then takes frames from the captured video, converts them to a jpeg encoded bytearray, and makes them available via a generator:

if vidcap.isOpened():
    try:
        for x in iter(int, 1):
            ret, frame = vidcap.read()
            if ret:
                print("Read Frame")
            elif ret is False:
                print("Frame Dropped")
            converted = convert_image(frame, to_type=bytearray)
            yield converted

This generator is called at regular intervals:

for image in generator():
    with open("/path/{}.jpg".format(datetime.datetime.now().strftime('%M-%S-%f')), 'wb') as f:
            f.write(image)
            f.close()
    time.sleep(0.3)

The problem is that, upon looking at the saved images, there are jumps between frames. It looks like the program will grab several images in rapid succession, stop recording for a second or two, then pick back up and grab several more images in rapid succession. The console never prints "Frame Dropped" so I assume it's not a problem on the cameras end. What is going wrong here and is there any way to get smooth, constant framerates in this way? Thank you in advance.

Upvotes: 0

Views: 951

Answers (1)

Chop Labalagun
Chop Labalagun

Reputation: 612

You may want to open a new thread to handle the camera frames and process the images on the main thread, that will give you a better FPS.

Upvotes: 0

Related Questions