I have been having some issues with getting a video stream from my cameras on my Pi4B. I am running this on Python. I have been getting the error;

VIDEOIO(V4L2:/dev/video0): select() timeout

with the code;

import socket
import cv2
import numpy as np

print("OpenCV version:", cv2.__version__)
#print("CUDA support:", cv2.cuda.getCudaEnabledDeviceCount() > 0)

BUFF_SIZE = 65535
CHUNK_SIZE = 16384
WIDTH = 640
HEIGHT = 480
port = 5555
port2 = 5556

# Set the end marker
delimiter = b'FRAME_END_MARKER'

    # Creat the UDP socket
    UDP_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    UDP_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFF_SIZE)
    host_name = socket.gethostname()
    host_ip = ""
    UDP_socket.bind((host_ip, port))
    print(f"UDP socket bound to {host_ip}:{port}")
    vid0 = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L2)
    #vid1 = cv2.VideoCapture('/dev/video2', cv2.CAP_V4L2)
    #vid0 = cv2.VideoCapture(0)
    vid0.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
    while True:
        msg, client_addr = UDP_socket.recvfrom(BUFF_SIZE)
        print('Got UDP connection from ', client_addr)

            _, frame0 = vid0.read()
            #_, frame1 = vid1.read()
            frame = cv2.resize(frame0, (WIDTH, HEIGHT))
            #frame1 = cv2.resize(frame1, (WIDTH, HEIGHT))
            #frame = np.hstack((frame0, frame1))
            _, encoded_frame = cv2.imencode('.jpg', frame)
            # Split the encoded frame into chunks
            for i in range(0, len(encoded_frame), CHUNK_SIZE):
                chunk = encoded_frame[i:i + CHUNK_SIZE].tobytes()
                UDP_socket.sendto(chunk, client_addr)

            # Send the end marker after the last chunk
            UDP_socket.sendto(delimiter, client_addr)

    print("There was an error in the main loop")
    print("The sockets have been closed")

I've had the best luck using V4L2; no other APIs have worked. Here's a list of my devices (v4l2-ctl --list-devices);

bcm2835-codec-decode (platform:bcm2835-codec):

bcm2835-isp (platform:bcm2835-isp):

rpivid (platform:rpivid):

UVC Camera (32e4:8830): HD USB  (usb-0000:01:00.0-1.1):

USB2.0 PC CAMERA: USB2.0 PC CAM (usb-0000:01:00.0-1.2):

And the formats for video0 and video2 (v4l2-ctl -d /dev/video* --list-formats);

        Type: Video Capture

        [0]: 'MJPG' (Motion-JPEG, compressed)
        [1]: 'YUYV' (YUYV 4:2:2)
        Type: Video Capture

        [0]: 'YUYV' (YUYV 4:2:2)

Sometimes, I can get a camera to work a little before it stops working. I don't know why that is. Please let me know if you have any other ideas or things I could try. I bet it's something stupid and small I'm missing. I appreciate any and all help.

Cheers, Dwayne Wallace


Your Answer

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

Browse other questions tagged or ask your own question.