gpt4 book ai didi

opencv - 强制waitkey()进行操作{OpenCV} {PY}

转载 作者:行者123 更新时间:2023-12-02 17:12:50 24 4
gpt4 key购买 nike

我有个问题:
我创建了一个打开网络摄像头并进行人脸识别的小程序。
现在,我想执行此任务:按下键(空格)时,程序必须进入阶段2(人脸识别)。为此,我使用了cv2.waitkey()。主要问题在于,按我的功能,当按下空格键时,程序将进入第2阶段,但仅持续几秒钟(仅在按下空格键时进入第2阶段,并在释放时停止)。

有什么建议吗?

我将举一个例子说明我的意思:

cam = cv2.VideoCapture(0)   ##Load Cam
cv2.namedWindow(name, cv2.WINDOW_AUTOSIZE)

while True:
s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow(name, img)
k = cv2.waitKey(1)

if k == 13: ## if return is pressed, on the screen will appear the text 'instruction',but when it
## released the text disappear, and i don't want this...
s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.putText(img,'instructions',(10,25), font, 0.7,(255,255,255),1,cv2.LINE_AA)

cv2.imshow(name, img)

解:
cv2.namedWindow(name, cv2.WINDOW_AUTOSIZE)

while True:
s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow(name, img)
k = cv2.waitKey(1)

if k == 13:
break
while True:
s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.putText(img,'instructions',(10,25), font, 0.7,(255,255,255),1,cv2.LINE_AA)

cv2.imshow(name, img)

最佳答案

问题是您的条件指令位于循环中-仅在条件评估为 true 时才运行它们。为避免这种情况,请考虑中断循环并遵循进一步的说明。

 cam = cv2.VideoCapture(0)   ##Load Cam
cv2.namedWindow(name, cv2.WINDOW_AUTOSIZE)

while True:
s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow(name, img)
k = cv2.waitKey(1)

if k == 13:
break

s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.putText(img,'instructions',(10,25), font, 0.7,(255,255,255),1,cv2.LINE_AA)
cv2.imshow(name, img)
cv2.waitKey(1)

关于opencv - 强制waitkey()进行操作{OpenCV} {PY},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27461466/

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