尝试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()