简体   繁体   English

cv2.waitkey(1)没有在opencv python中运行

[英]cv2.waitkey(1) not running in opencv python

I need to understand cv2.waitkey() in python with cv2 我需要用cv2来理解python中的cv2.waitkey()

faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
ret, frame = video_capture.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=8,
    minSize=(40, 40),
    #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    flags = 0
)   

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(gray, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the resulting frame
cv2.imshow('Video', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break
if cv2.waitKey(1) & 0xFF == ord('b'):
    cv2.imwrite('example.png',gray)

cv2.waitKey() when I press b it doesn't save picture but press q works . cv2.waitKey()当我按b它不保存图片但按q工作。 Please help! 请帮忙!

You are calling waitKey() twice. 你正在调用waitKey()两次。 With your code, press any key but q then press b and it will save the image. 使用您的代码,按任意键但q然后按b它将保存图像。

Only call waitKey(1) once, and save the result in a variable, then test that variable, eg: 只调用waitKey(1)一次,并将结果保存在变量中,然后测试该变量,例如:

keypress = cv2.waitKey(1)
if keypress & 0xFF == ord('q'):
    break
if keypress & 0xFF == ord('b'):
    cv2.imwrite('example.png',gray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM