OpenCV with multiple webcams - how to tell which camera is which in code?

后端 未结 1 881
暗喜
暗喜 2021-01-19 19:31

Previously I\'ve used industrial cameras with Ethernet connections and distinct IP addresses for multiple camera setups. Now I\'m attempting a multiple camera setup with Op

相关标签:
1条回答
  • 2021-01-19 19:44

    For the solution you found, you need root privileges. On my setup with Ubuntu20 this is not required for:

    udevadm info --name=/dev/video0
    

    This outputs properties of first camera detected. Pipe it through "grep" to filter out specific property that is different for all cameras like "ID_SERIAL=". You can then use "cut" to remove beginning of this string "ID_SERIAL=" and leave just the value like:

    udevadm info --name=/dev/video0 | grep ID_SERIAL= | cut -d "=" -f 2
    

    In Python you can run external command to get this info like:

    def get_cam_serial(cam_id):
        # Prepare the external command to extract serial number. 
        p = subprocess.Popen('udevadm info --name=/dev/video{} | grep ID_SERIAL= | cut -d "=" -f 2'.format(cam_id),
                             stdout=subprocess.PIPE, shell=True)
    
        # Run the command
        (output, err) = p.communicate()
    
        # Wait for it to finish
        p.status = p.wait()
    
        # Decode the output
        response = output.decode('utf-8')
    
        # The response ends with a new line so remove it
        return response.replace('\n', '')
    

    To acquire all the camera serial numbers, just loop through several camera ID's. On my setup trying camera ID 0 and 1 target the same camera. Also 2 and 4 target the second camera, so the loop can have 2 for step. Once all ID's are extracted, place them in a dictionary to be able to associate cam ID with serial number. The complete code could be:

    serials = {}
    FILTER = "ID_SERIAL="
    
    
    def get_cam_serial(cam_id):
        p = subprocess.Popen('udevadm info --name=/dev/video{} | grep {} | cut -d "=" -f 2'.format(cam_id, FILTER),
                             stdout=subprocess.PIPE, shell=True)
        (output, err) = p.communicate()
        p.status = p.wait()
        response = output.decode('utf-8')
        return response.replace('\n', '')
    
    
    for cam_id in range(0, 10, 2):
        serial = get_cam_serial(cam_id)
        if len(serial) > 6:
            serials[cam_id] = serial
    
    print('Serial numbers:', serials)
    
    0 讨论(0)
提交回复
热议问题