5

I need to understand cv2.waitkey() in python with cv2

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 . Please help!

1 Answer 1

7

You are calling waitKey() twice. With your code, press any key but q then press b and it will save the image.

Only call waitKey(1) once, and save the result in a variable, then test that variable, e.g.:

keypress = cv2.waitKey(1)
if keypress & 0xFF == ord('q'):
    break
if keypress & 0xFF == ord('b'):
    cv2.imwrite('example.png',gray)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.