运行从网络摄像头读取数据的私人代码时出现以下错误:
Loading weights from handyolo/weights/000010.weights... Done!
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:Creating instance of the ImageHeatmapsPose2dZrelZAll_FPN model.
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:n_iter: 1
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:improvement_per_itr: 0.01
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:beta: 5.0
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:Creating instance of L1 Loss.
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy
Error getting frame from device 0
以下是相机信息:
$ v4l2-ctl -d /dev/video0 --all
Driver Info (not using libv4l2):
Driver name : uvcvideo
Card type : HD Pro Webcam C920
Bus info : usb-0000:00:14.0-9
Driver version: 4.13.13
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 1: ok)
Format Video Capture:
Width/Height : 320/180
Pixel Format : 'YUYV'
Field : None
Bytes per Line : 640
Size Image : 115200
Colorspace : sRGB
Transfer Function : Default
YCbCr Encoding : Default
Quantization : Default
Flags :
Crop Capability Video Capture:
Bounds : Left 0, Top 0, Width 320, Height 180
Default : Left 0, Top 0, Width 320, Height 180
Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 320, Height 180
Selection: crop_bounds, Left 0, Top 0, Width 320, Height 180
Streaming Parameters Video Capture:
Capabilities : timeperframe
Frames per second: 30.000 (30/1)
Read buffers : 0
brightness (int) : min=0 max=255 step=1 default=128 value=128
contrast (int) : min=0 max=255 step=1 default=128 value=128
saturation (int) : min=0 max=255 step=1 default=128 value=128
white_balance_temperature_auto (bool) : default=1 value=1
gain (int) : min=0 max=255 step=1 default=0 value=132
power_line_frequency (menu) : min=0 max=2 default=2 value=2
white_balance_temperature (int) : min=2000 max=6500 step=1 default=4000 value=3255 flags=inactive
sharpness (int) : min=0 max=255 step=1 default=128 value=128
backlight_compensation (int) : min=0 max=1 step=1 default=0 value=0
exposure_auto (menu) : min=0 max=3 default=3 value=3
exposure_absolute (int) : min=3 max=2047 step=1 default=250 value=333 flags=inactive
exposure_auto_priority (bool) : default=0 value=1
pan_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0
tilt_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0
focus_absolute (int) : min=0 max=250 step=5 default=0 value=0 flags=inactive
focus_auto (bool) : default=1 value=1
zoom_absolute (int) : min=100 max=500 step=1 default=100 value=100
led1_mode (menu) : min=0 max=3 default=0 value=3
led1_frequency (int) : min=0 max=255 step=1 default=0 value=0
$ v4l2-ctl -d /dev/video0 --list-formats
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUYV 4:2:2
Index : 1
Type : Video Capture
Pixel Format: 'H264' (compressed)
Name : H.264
Index : 2
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : Motion-JPEG
我怎样才能解决这个问题?
此外,尝试从 guvcview 获取视频时出现以下错误:
$ guvcview
GUVCVIEW: version 2.0.2
GUVCVIEW: couldn't open /home/mona/.config/guvcview2/video0 for read: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
libv4l2: error setting pixformat: Device or resource busy
V4L2_CORE: (VIDIOC_S_FORMAT) Unable to set format: Device or resource busy
GUCVIEW: could not set the defined stream format
GUCVIEW: trying first listed stream format
libv4l2: error setting pixformat: Device or resource busy
V4L2_CORE: (VIDIOC_S_FORMAT) Unable to set format: Device or resource busy
GUCVIEW: also could not set the first listed stream format
GUVCVIEW: Video capture failed
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
V4L2_CORE: (VIDIOC_S_PARM) error: Device or resource busy
V4L2_CORE: Unable to set 1/30 fps
GUVCVIEW: (status) saving video to /home/mona/my_video-1.mkv
ENCODER: add stream 0 to stream list
ENCODER: add stream 1 to stream list
ENCODER: (matroska) add seekhead entry 0 (max 10)
ENCODER: (matroska) add seekhead entry 1 (max 10)
AUDIO: (portaudio) Aborting audio stream
AUDIO: Closing audio stream...
ENCODER: (matroska) closing context
ENCODER: (matroska) closing cluster
ENCODER: (matroska)write seekhead
ENCODER: (matroska) end duration = 6922 (6922.000000)
^CGUVCVIEW Caught signal 2
^CGUVCVIEW Caught signal 2
到目前为止,拔下并重新插入网络摄像头,使用奶酪,现在我得到了视频:
$ cheese &
但是现在,运行代码时出现这个新错误:
VIDEOIO ERROR: V4L: index 0 is not correct!
Error getting frame from device 0
此前,摄像头灯是蓝色的,现在却不亮了。
因此我的网络摄像头在设备 014 上,我使用它在 OpenCV 中捕获视频:
$ sudo lsusb | grep -i cam
Bus 003 Device 014: ID 046d:082d Logitech, Inc. HD Pro Webcam C920
以下是代码:
if USE_CAMERA:
cap = cv.VideoCapture(args.device)
然后我将 014 作为参数传递给它。我收到此错误:
VIDEOIO ERROR: V4L: index 14 is not correct!
Error getting frame from device 14
答案1
首先拔下并插入网络摄像头 USB,然后使用以下命令查找网络摄像头的设备编号并使用适当的端口号:
$ ls /dev/video*
/dev/video1
答案2
就我而言,这是 OpenCV 版本问题。我将其版本从 4.0 降级到 3.4,并且对我有用。由于我之前使用 pip 安装了 OpenCv,因此我按以下方式降级了它:
pip uninstall opencv-python
pip install opencv-python==3.4.0.12
答案3
出现此错误的原因是 OpenCV 没有释放相机。当您开始使用相机捕捉帧cap= cv2.VideoCapture(0)
并在后台释放相机时cap.release()
,我们的相机仍在工作。
再次使用相机且不出现错误的一种方法是重新启动 Ubuntu。
如果你不想每次运行代码时都重新启动,你可以做的是提出一个键盘中断通过这种方式,在代码的最后,raise KeyboardInterrupt
相机流被正确关闭。这可能不是最安全的方法。但这是有效的。