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

免责声明:我还没有测试过这一点,所以它可能有错误,或者只是没有工作,但至少可以给您一个解决办法的想法。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐