0

I take the camera from the outside It is possible to turn the camera on and off using Python. Often, however, in the process of turning the same camera on and off [WARN:[email protected]] global /tmp/pip-wheel-efxaz4j7/opencv-python_bedc0fac27944da0021e079da44d32bf/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO (V4L2:/dev/video1): can't open camera by index is alerted and the camera does not open. Could you ever solve these problems? It's not that it doesn't open every time, it's that it doesn't open every once in a while.

while True:
    all_state=0
    #time.sleep(0.1)
    cap = cv2.VideoCapture(-1)
    if not cap.isOpened():
        print("Camera open1 failed")
        time.sleep(0.5)
        cap = cv2.VideoCapture(-1)
        if not cap.isOpened():
            print("camera open2 failed")
    
    state_bat, igw, igh = cam_read(cap, ser_grbl)
    print('state_bat = ', state_bat, igw, igh)
    if (state_bat ==0):
        time.sleep(0.1)
        cam_state=servo_AT(servo1_pin, servo2_pin, sensor_pin, ser_grbl, igw, igh)
        print('cam_state =', cam_state)

        if(cam_state==0):

            move_pickup(ser_grbl, -8, -168)
            servo_DT(servo1_pin, servo2_pin)
            move_pickup(ser_grbl, -8, -190)
            #time.sleep(0.1)
            time.sleep(0.1)
            cap2 = cv2.VideoCapture(2)
            if not cap2.isOpened():
                print("Camera2 open failed")
                time.sleep(0.5)
                cap2 = cv2.VideoCapture(2)
                if not cap2.isOpened():
                    print("Camera2 open2 failed")
                    all_state=1
            if(all_state==0):
                bat_scan(cap2, ser_grbl, servo1_pin, servo2_pin, sensor_pin)
                cap3 = cv2.VideoCapture(2)
                if not cap3.isOpened():
                    print("Camera3 open failed")
                    time.sleep(0.5)
                    cap3 = cv2.VideoCapture(2)
                    if not cap3.isOpened():
                        print("Camera3 open3 failed")
                time.sleep(0.1)
                double_state = bat_scan2(cap3, ser_grbl, servo1_pin, servo2_pin)
                print('double_state = ', double_state)
                if (double_state==0):
                    bat_down(servo1_pin, servo2_pin)
                    servo_DT(servo1_pin,  servo2_pin)
                    time.sleep(0.2)
                    servo_INIT(servo1_pin, servo2_pin, sensor_pin)
                else:
                    move_pickup(ser_grbl, -42, -195.7)
                    bat_down(servo1_pin, servo2_pin)
                    time.sleep(0.3)
                    servo_DT(servo1_pin,  servo2_pin)
                    time.sleep(0.2)
                    servo_INIT(servo1_pin, servo2_pin, sensor_pin)
                    time.sleep(0.2)
                    cap2 = cv2.VideoCapture(2)
                    if not cap2.isOpened():
                        print("Camera2 open failed")
                    bat_scan(cap2, ser_grbl, servo1_pin, servo2_pin, sensor_pin)
                    time.sleep(0.2)
                    bat_down(servo1_pin, servo2_pin)
                    time.sleep(0.3)
                    servo_DT(servo1_pin,  servo2_pin)
                    time.sleep(0.2)
                    servo_INIT(servo1_pin, servo2_pin, sensor_pin)
                    time.sleep(0.3)
            #bat_down(servo1_pin, servo2_pin)
                #time.sleep(0.2)
           else:
                homing(ser_grbl)

1 Answer 1

0

i got same error on windows. i solved the problem with this command. Command is only for PC not Raspberry PI

cv2.VideoCapture(0,cv2.CAP_DSHOW) 

also you can use -1 instead of 0 to solve the problem

cv2.VideoCapture(-1)

I cant see cap.release() in your code. you should use cap1.release() before open cap2 ( or cap2.release() before open cap1 ) if you dont use both camera. this command will close the video1 source. if u use both camera simultaniously, raspberry pi's band with may not allow to open two cameras.

Sign up to request clarification or add additional context in comments.

16 Comments

I'm using two cameras. Can both cameras apply DSHOW?
Currently the camera index numbers are using -1 and 2 respectively
yes you can apply both of them.
Applying what you have said, cv2. VideoCapture(-1, DSHOW) and cv2. When I applied VideoCapture(2, DSHOW) and ran it, the camera failed to open. I use the Raspberry Pi and the language uses Python.
getting any error message ? could you please share?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.