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:[email protected]] 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}'