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

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}'
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Qt6 中使用 QML 实时显示 OpenCV 获取摄像头需要以下步骤: 1. 创建一个 QQuickImageProvider 类,用于将 OpenCV 获取的图像数据提供给 QML。 ```cpp class CameraImageProvider : public QQuickImageProvider { public: CameraImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {} QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override { Q_UNUSED(id) Q_UNUSED(requestedSize) cv::Mat image; // 获取摄像头图像数据 // ... // 将图像数据转换为 QPixmap QPixmap pixmap = QPixmap::fromImage(QImage(image.data, image.cols, image.rows, QImage::Format_RGB888)); *size = pixmap.size(); return pixmap; } }; ``` 2. 创建一个 QML 界面,使用 Image 控件显示摄像头图像。 ```qml import QtQuick 2.0 Image { id: cameraImage width: 640 height: 480 source: "image://camera/capture" fillMode: Image.PreserveAspectFit } ``` 3. 在 C++ 代码中注册 QQuickImageProvider 类,并将其作为上下文属性传递给 QML。 ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; // 注册 QQuickImageProvider 类 CameraImageProvider cameraImageProvider; engine.addImageProvider("camera", &cameraImageProvider); // 将 QQuickImageProvider 实例作为上下文属性传递给 QML QQmlContext *context = engine.rootContext(); context->setContextProperty("cameraImageProvider", &cameraImageProvider); // 加载 QML 界面 const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); } ``` 4. 在程序运行时,通过 OpenCV 获取摄像头图像数据,并在 QML 界面中实时显示。 ```cpp cv::VideoCapture cap(0); if (!cap.isOpened()) { // 摄像头打开失败 } while (true) { cv::Mat frame; cap.read(frame); // 将图像数据传递给 QML QCoreApplication::processEvents(); } ``` 以上是在 Qt6 中使用 QML 实时显示 OpenCV 获取摄像头的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值