您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用OpenCV和Python显示网络摄像头

使用OpenCV和Python显示网络摄像头

尝试c = cv.WaitKey(10)repeat()方法底部添加该行。

这将等待10毫秒,以便用户输入密钥。即使您根本不使用密钥,也请放进去。我认为这只需要稍加延迟,因此time.sleep(10)可能也可以。

关于相机索引,您可以执行以下操作:

for i in range(3):
    capture = cv.CaptureFromCAM(i)
    if capture: break

这将至少在0-2的索引中找到第一个“工作”捕获设备的索引。您的计算机中可能有多个设备被识别为正确的捕获设备。我知道确认您正确的唯一方法是手动看灯。也许获取图像并检查其属性

要将用户提示添加到该过程中,可以在重复循环中将密钥绑定到切换摄像机:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
    global capture #declare as globals since we are assigning to them Now
    global camera_index
    frame = cv.QueryFrame(capture)
    cv.ShowImage("w1", frame)
    c = cv.WaitKey(10)
    if(c=="n"): #in "n" key is pressed while the popup window is in focus
        camera_index += 1 #try the next camera index
        capture = cv.CaptureFromCAM(camera_index)
        if not capture: #if the next camera index didn't work, reset to 0.
            camera_index = 0
            capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()

免责声明:我尚未对此进行测试,因此它可能存在错误或无法正常工作,但至少可以为您提供一种解决方法

python 2022/1/1 18:33:15 有216人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶