How to correctly check if a camera is available?

后端 未结 4 402
情歌与酒
情歌与酒 2021-01-12 06:46

I am using OpenCV to open and read from several webcams. It all works fine, but I cannot seem to find a way to know if a camera is available.

I tried this code (cam

4条回答
  •  爱一瞬间的悲伤
    2021-01-12 07:29

    Here is a "NOT working" solution to help you prevent tumbling over in this pitfall:

    import cv2 as cv
    import PySpin
    
    print (cv.__version__)
    
    # provided by Patrick Artner as solution to be working for other cameras than
    #                                                  those of Point Grey (FLIR).
    
    def testDevice(source):
       cap = cv.VideoCapture(source) 
       if cap is None or not cap.isOpened():
           print('Warning: unable to open video source: ', source)
    
    # ... PySpin / Spinnaker (wrapper/SDK libary) ...
    
    system   = PySpin.System.GetInstance()
    cam_list = system.GetCameras()
    
    cam = ''
    cam_num = 0
    
    for ID, cam in enumerate(cam_list):
        # Retrieve TL device nodemap
        if ID == cam_num:
            print ('Got cam')
            cam = cam
    
            cam.Init()
    
            # ... CV2 again ...
    
            for i in range(10):
                testDevice(i) # no printout
    

提交回复
热议问题