【发布时间】:2014-12-13 17:10:16
【问题描述】:
我有一个问题: 我创建了一个小程序来打开网络摄像头并进行人脸识别。 现在我想做这个任务:当按下一个键(空格)时,程序必须进入阶段 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)
【问题讨论】:
-
创建一个名为 phase 的新变量,初始化为 1。然后如果 k == 13:phase = 2。在你的循环中,然后询问 phase 是 1 还是 2,并在每个变量中做任何你喜欢的事情那些阶段。
标签: opencv