Using device name instead of ID in OpenCV method VideoCapture.open()

后端 未结 3 813
感动是毒
感动是毒 2021-01-06 08:31

I have a lot of video devices in my /dev folder (e.g. video1, video2, ..., video9) and one /dev/video which

3条回答
  •  孤街浪徒
    2021-01-06 09:01

    The best thing is to parse the ID of the device.

    #include 
    #include 
    
    ...
    
    boost::filesystem::path path( "/dev/video3" );
    auto target = boost::filesystem::canonical(path).string();
    std::regex exp( ".*video(\\d+)" );
    std::smatch match;
    std::regex_search( target, match, exp );
    auto id = strtod( match[1] );
    auto cap = cv::VideoCapture( id );
    

    Note the use of canonical() to make the path absolute and resolve any symbolic links. This way it works even if you give it v4l device paths from /dev/v4l/by-id or /dev/v4l/by-path like for example:

    "/dev/v4l/by-id/usb-046d_0990_1188AD49-video-index0"
    "/dev/v4l/by-path/pci-0000:00:13.2-usb-0:4.4.4:1.0-video-index0"
    

提交回复
热议问题