1

我想检测所有插入 linuxbox 的摄像头。

我发现的最好的是:

import cv2

def test_open_cam():
    connected_cam = []
    for port in range(0, 10):
        try:
            cam = cv2.VideoCapture(port)
            if cam.isOpened():
                connected_cam.append(port)
            cam.release()
            cv2.destroyAllWindows()
        except:
            cam.release()
            cv2.destroyAllWindows()
    print(f'=> list of found cameras: {connected_cam}')
    return(connected_cam)

当我运行它时,我得到了它,但有很多 [WARN:0]:

start
==> test port: 0
==> test port: 1
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
==> test port: 2
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
==> test port: 3
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video3): can't open camera by index
==> test port: 4
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video4): can't open camera by index
==> test port: 5
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video5): can't open camera by index
==> test port: 6
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video6): can't open camera by index
==> test port: 7
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video7): can't open camera by index
==> test port: 8
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video8): can't open camera by index
==> test port: 9
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video9): can't open camera by index
=> list of found cameras: [0]

我怎样才能避免这一切[ WARN:0] ...,顺便避免丑陋的尝试:除了?

注意:使用 Patrick Yeadon 的提议https://stackoverflow.com/a/61768256/3313834来自:Listing available devices in python-opencv

def test_open_cam():
    # checks the first 10 indexes.
    index = 0
    arr = []
    i = 10
    while i > 0:
        cap = cv2.VideoCapture(index)
        if cap.read()[0]:
            arr.append(index)
            cap.release()
        index += 1
        i -= 1
    return arr

我仍然[ WARN:0]在 cv2.VideoCapture 上得到相同的结果(在第一个之后)

[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video3): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video4): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video5): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video6): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video7): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video8): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video9): can't open camera by index

按照我尝试替换cam = cv2.VideoCapture(port)的Christoph Rackwitz 的想法cam = cv2.VideoCapture(port, apiPreference=cv2.CAP_FFMPEG),不再有 WARN 消息!但cam.isOpened()为所有端口返回 False ;(

>>> print(cv2.getBuildInformation()) 
...
  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (58.109.100)
      avformat:                  YES (58.61.100)
      avutil:                    YES (56.60.100)
      swscale:                   YES (5.8.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)
4

0 回答 0