我需要在python中使用cv2
理解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()
当我按b时它不会保存图片但是按q工作。请帮忙!
答案 0 :(得分:5)
你正在两次调用waitKey()。使用您的代码,按任意键但q然后按b它将保存图像。
只调用waitKey(1)一次,并将结果保存在变量中,然后测试该变量,例如:
keypress = cv2.waitKey(1)
if keypress & 0xFF == ord('q'):
break
if keypress & 0xFF == ord('b'):
cv2.imwrite('example.png',gray)