0

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'

try:
    # 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 = "0.0.0.0"
    
    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)

        while(vid0.isOpened()):
            _, 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)

finally:
    print("There was an error in the main loop")
    vid0.release()
    #vid1.release()
    cv2.destroyAllWindows()
    UDP_socket.close
    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):
        /dev/video10
        /dev/video11
        /dev/video12
        /dev/video18
        /dev/video31
        /dev/media3

bcm2835-isp (platform:bcm2835-isp):
        /dev/video13
        /dev/video14
        /dev/video15
        /dev/video16
        /dev/video20
        /dev/video21
        /dev/video22
        /dev/video23
        /dev/media0
        /dev/media1

rpivid (platform:rpivid):
        /dev/video19
        /dev/media2

UVC Camera (32e4:8830): HD USB  (usb-0000:01:00.0-1.1):
        /dev/video0
        /dev/video1
        /dev/media4

USB2.0 PC CAMERA: USB2.0 PC CAM (usb-0000:01:00.0-1.2):
        /dev/video2
        /dev/video3
        /dev/media5

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

ioctl: VIDIOC_ENUM_FMT
        Type: Video Capture

        [0]: 'MJPG' (Motion-JPEG, compressed)
        [1]: 'YUYV' (YUYV 4:2:2)
ioctl: VIDIOC_ENUM_FMT
        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

0

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.