Create openCV VideoCapture from interface name instead of camera numbers

后端 未结 5 907
眼角桃花
眼角桃花 2020-12-17 20:13

The normal way to create a videocapture is this:

cam = cv2.VideoCapture(n)

where n corresponds to the number of /dev/video0,

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 20:53

    Instead of the suggested solution I found a shorter one, that feels a little bit hacky.

    I just look at where the symbolic link points, find the integer in it, and then use that.

    import subprocess
    
    cmd = "readlink -f /dev/CAMC"
    process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    
    # output of form /dev/videoX
    out = process.communicate()[0]
    
    # parse for ints
    nums = [int(x) for x in out if x.isdigit()]
    
    cap = cv2.VideoCapture(nums[0])
    

提交回复
热议问题