首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么网络摄像头没有出现在python-opencv中

为什么网络摄像头没有出现在python-opencv中
EN

Stack Overflow用户
提问于 2020-11-04 01:19:50
回答 1查看 148关注 0票数 0

当我使用macbook air在python上运行我的nooby opencv程序时,它没有显示任何内容。同样,程序在0.5-1秒后退出。

这是代码,它非常简单

代码语言:javascript
运行
复制
import cv2



cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)

while True:
     success, img = cap.read()
     cv2.imshow("Video", img)

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

回答 1

Stack Overflow用户

发布于 2020-11-04 02:33:10

您应该首先列出可用的摄像机ids

在这里您可以使用G M's answer

代码语言:javascript
运行
复制
def list_ports():
    is_working = True
    dev_port = 0
    working_ports = []
    available_ports = []
    while is_working:
        camera = cv2.VideoCapture(dev_port)
        if not camera.isOpened():
            is_working = False
            print("Port %s is not working." % dev_port)
        else:
            is_reading, img = camera.read()
            w = camera.get(3)
            h = camera.get(4)
            if is_reading:
                print("Port %s is working and reads images (%s x %s)" % (dev_port, h, w))
                working_ports.append(dev_port)
            else:
                print("Port %s for camera ( %s x %s) is present but does not reads." % (dev_port, h, w))
                available_ports.append(dev_port)
        dev_port += 1
    return available_ports, working_ports

结果将是:

代码语言:javascript
运行
复制
Port 0 is working and reads images (720.0 x 1280.0)
OpenCV: out device of bound (0-0): 1
OpenCV: camera failed to properly initialize!
Port 1 is not working.

例如,在我的mac上,摄像头id (cid) 0是可用的,但不是1。

因此,我用cid 0初始化了代码。

代码语言:javascript
运行
复制
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while cap.isOpened():
    success, img = cap.read()

    if success:
        cv2.imshow("Video", img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

默认情况下,不应初始化VideoCapture对象。因为它可能不起作用。如果没有列出可用端口,则可能需要外置摄像头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64667524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档