确保你的系统有conda环境并且安装了cv2。
–确保你conda是基于Python3.x以上。
–原理就是循环cv2中VideoCapture的指定摄像头的号数。
–从0开始计数,当VideoCapture初始化摄像头失败时,
–(这里使用的是VideoCapture中的grab()函数,如果成功返回True,否则是False)
–那么代表着没有这个摄像头。终止计数退出循环。这里建议要设置循环的上限。
import cv2 import os class Camera: def __init__(self, cam_preset_num=10): self.cam_preset_num = cam_preset_num def get_cam_num(self): cnt = 0 for device in range(0, self.cam_preset_num): stream = cv2.VideoCapture(device) grabbed = stream.grab() stream.release() if not grabbed: break cnt = cnt + 1 print(cnt) os.system("python ./OpenVideo.py") if __name__ == '__main__': cam = Camera() cam_num = cam.get_cam_num()
转自:https://blog.csdn.net/JayLincoder/article/details/102494407