gpt4 book ai didi

python - 按下按键时保存图像 Python/Opencv

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:38 27 4
gpt4 key购买 nike

我试图在按下某个键时保存来自网络摄像头的图像,但无法正常工作。在这里我附上我的代码:

import cv

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

def repeat():
global capture
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = cv.WaitKey(25)
if(c=="n"):
cv.SaveImage("f"+str(i)+".jpg",frame)
i=i+1
while True:
repeat()

最佳答案

cv.WaitKey() 不返回 key ,但在超时时给出 -1。

解决方法:

import cv

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

def repeat():
global capture
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = cv.WaitKey(25)
if (c != -1):
cv.SaveImage("f"+str(i)+".jpg",frame)
i=i+1
while True:
repeat()

关于python - 按下按键时保存图像 Python/Opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com