关于windows下opencv-python无法检测到摄像头的问题,摄像头权限(独一份)

关于windows下opencv-python无法检测到摄像头的问题,摄像头权限(独一份)

笔者电脑环境

  • Windows11
  • Anaconda3(python3.11)
  • Conda环境python==3.10
  • opencv-python==4.9.0.80(这个不重要)

前言:

​ 在查遍几乎所有此问题的解决方法并尝试使用后,依然不成功。由此可断定我这问题是属于独一份了(苦笑),在我慢慢尝试并找到解决方法后固然如此。开门见山,先上解决方法。

1.解决办法

其实解决方法很简单,打开windows11的设置,找到隐私和安全性选项,找到应用权限下的摄像头选项

在这里插入图片描述

让桌面应用访问摄像头,打开其开关即可。

2.出现的报错

以下是测试代码

import cv2
from pygrabber.dshow_graph import FilterGraph

def list_cameras():
    graph = FilterGraph()
    devices = graph.get_input_devices()
    return devices


def capture_camera(device_index):
    cap = cv2.VideoCapture(device_index)
    if not cap.isOpened():
        print(f"Cannot open camera {device_index}")
        return

    while True:
        ret, frame = cap.read()
        if not ret:
            print("Failed to grab frame")
            break

        cv2.imshow('Camera', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    devices = list_cameras()
    if not devices:
        print("No cameras found.")
    else:
        print("Available devices:")
        for idx, device in enumerate(devices):
            print(f"{idx}: {device}")

        device_index = int(input("Enter the device index to use: "))
        if 0 <= device_index < len(devices):
            capture_camera(device_index)
        else:
            print("Invalid device index.")

终端信息
在这里插入图片描述

这是我用pygrabber库列出来的摄像头索引与名称,可以看到有0与1两个摄像头设备。然后我输入1启动摄像头就开始报错了。

然后按照上面解决办法开启权限后就可以正常启用摄像头了,以下是效果图
在这里插入图片描述

3.我尝试过的无效方法(可能对你们有用)

可能你不是我这个问题,以下方法可能会帮到你

  • 1.重启电脑大法(雾)
  • 2.管理员运行命令行
  • 3.更换环境python的版本,opencv-python版本
  • 4.更换视频流库如pygrabber,imageio,v4l2等
  • 5.关闭可能在使用摄像头的其他程序
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值