利用opencv获取网络摄像头数据并显示报错 select() timeout

博客内容讲述了在使用OpenCV进行视频捕获时遇到的错误,即无法从摄像头接收帧。作者通过检查设备、安装V4l2工具包并查看摄像头信息,发现摄像头存在但未被点亮。最终通过设置摄像头的宽度和高度参数解决了问题。修复代码涉及在初始化VideoCapture后设置帧的宽度和高度。
摘要由CSDN通过智能技术生成

opencv官网demo-Capture Video from Camera

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

运行后报错:、

[ WARN:0@10.049] global /io/opencv/modules/videoio/src/cap_v4l.cpp (1000) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
Can't receive frame (stream end?). Exiting ...

查看是否有摄像头:

nvidia@miivii-tegra:~$ ls /dev/video*
/dev/video0  /dev/video2  /dev/video4  /dev/video6
/dev/video1  /dev/video3  /dev/video5  /dev/video7
nvidia@miivii-tegra:~$ 

可以看到/dev/video0是存在的,但摄像头没有被点亮
安装V4l2工具包:

sudo apt install v4l-utils

查看对应摄像头信息:

sudo v4l2-ctl -d  /dev/video10 --all

输出:

Driver Info (not using libv4l2):
	Driver name   : tegra-video
	Card type     : vi-output, mv-max9296 2-0004
	Bus info      : platform:15c10000.vi:0
	Driver version: 4.9.201
	Capabilities  : 0x84200001
		Video Capture
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps   : 0x04200001
		Video Capture
		Streaming
		Extended Pix Format
Priority: 2
Video input : 0 (Camera 0: ok)
Format Video Capture:
	Width/Height      : 1280/720
	Pixel Format      : 'YUYV'
	Field             : None
	Bytes per Line    : 2560
	Size Image        : 1843200
	Colorspace        : sRGB
	Transfer Function : Default (maps to sRGB)
	YCbCr/HSV Encoding: Default (maps to ITU-R 601)
	Quantization      : Default (maps to Limited Range)
	Flags             : 

Camera Controls

                     group_hold 0x009a2003 (bool)   : default=0 value=0 flags=execute-on-write
                           gain 0x009a2009 (int64)  : min=0 max=0 step=0 default=0 value=0 flags=slider
                       exposure 0x009a200a (int64)  : min=0 max=0 step=0 default=0 value=59 flags=slider
                     frame_rate 0x009a200b (int64)  : min=0 max=0 step=0 default=0 value=30000000 flags=slider
                 exposure_short 0x009a200c (int64)  : min=0 max=0 step=0 default=0 value=59 flags=slider
           sensor_configuration 0x009a2032 (u32)    : min=0 max=0 step=0 default=0 flags=read-only, volatile, has-payload
         sensor_mode_i2c_packet 0x009a2033 (u32)    : min=0 max=0 step=0 default=0 flags=read-only, volatile, has-payload
      sensor_control_i2c_packet 0x009a2034 (u32)    : min=0 max=0 step=0 default=0 flags=read-only, volatile, has-payload
                    bypass_mode 0x009a2064 (intmenu): min=0 max=1 default=0 value=0
                override_enable 0x009a2065 (intmenu): min=0 max=1 default=0 value=0
                   height_align 0x009a2066 (int)    : min=1 max=16 step=1 default=1 value=1
                     size_align 0x009a2067 (intmenu): min=0 max=2 default=0 value=0
               write_isp_format 0x009a2068 (int)    : min=1 max=1 step=1 default=1 value=1
       sensor_signal_properties 0x009a2069 (u32)    : min=0 max=0 step=0 default=0 flags=read-only, has-payload
        sensor_image_properties 0x009a206a (u32)    : min=0 max=0 step=0 default=0 flags=read-only, has-payload
      sensor_control_properties 0x009a206b (u32)    : min=0 max=0 step=0 default=0 flags=read-only, has-payload
              sensor_dv_timings 0x009a206c (u32)    : min=0 max=0 step=0 default=0 flags=read-only, has-payload
               low_latency_mode 0x009a206d (bool)   : default=0 value=0
               preferred_stride 0x009a206e (int)    : min=0 max=65535 step=1 default=0 value=0
                   sensor_modes 0x009a2082 (int)    : min=0 max=30 step=1 default=30 value=4 flags=read-only

其中摄像头分辨率及视频格式如下:

Width/Height      : 1280/720
Pixel Format      : 'YUYV'

在原文件cap = cv.VideoCapture(0)后加入:

cap = cv.VideoCapture(0)
cap.set(3,1280)#宽
cap.set(4,720)#高
if not cap.isOpened():
    print("Cannot open camera")
    exit()

问题解决

yolov5 utils/dataloaders.py做如下修改

class LoadStreams:
    # YOLOv5 streamloader, i.e. `python detect.py --source 'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP streams`
    			...
            cap = cv2.VideoCapture(s)
            cap.set(3,1280)#宽
            cap.set(4,720)#高
            assert cap.isOpened(), f'{st}Failed to open {s}'
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值