Reputation: 91
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!
Upvotes: 5
Views: 3971
Reputation: 6826
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)
Upvotes: 7