Not able to get frame from the second run (OpenCV on Rasbian)?

asked 2017-07-10 21:19:02 -0500

hoang anh tuan gravatar image

Hi everyone,

i have the following code to run on Rasbian (Raspberry Pi 3's OS):

import cv2
import threading

id = 0
cam = cv2.VideoCapture(id)

while True:
    rt = cam.grab()
    ret,frame = cam.retrieve(rt)    

    if ret:
         # Addtional code
        **while not cam.isOpened():
            cam = cv2.VideoCapture(id)**            

    k = cv2.waitKey(1) & 0xFF
    if k == ord('q'):


At the first run, nothing to say.

from the second run, x value is always false and i must reopen camera continuously.

Why is the ret variable false? Is there any other way? (Compare to my way - additional code)


edit retag flag offensive close merge delete


you definitely should NOT close & reopen the capture.

also please check cam.isOpened(), before starting your loop.

berak gravatar imageberak ( 2017-07-11 04:51:18 -0500 )edit

@berak: I have checked cam.isOpened() but this function always returns false. How can I pass it? If I change code to read 2 or more cameras, I can't get frame from cameras from the second run :(

hoang anh tuan gravatar imagehoang anh tuan ( 2017-07-11 04:54:57 -0500 )edit

If I run the above code on Windows, no error occur

hoang anh tuan gravatar imagehoang anh tuan ( 2017-07-11 04:57:13 -0500 )edit

your raspi might have an install problem. (no idea about that, sorry)

you just won't change anything by hacking your python script.

berak gravatar imageberak ( 2017-07-11 05:02:56 -0500 )edit