尝试在repeat()方法的底部添加行c = cv.WaitKey(10)。
这等待10 ms用户输入密钥。即使你不使用密钥,放在这里。我认为只需要一些延迟,所以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()
免责声明:我没有测试这个,所以它可能有错误或只是不工作,但可能会给你至少一个解决方法的想法。