cv2.waitkey(1) 在 opencv python 中不是 运行
cv2.waitkey(1) not running in opencv python
我需要用 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()
当我按 b 时它不保存图片但按 q 有效。请帮忙!
您正在调用 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)
我需要用 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()
当我按 b 时它不保存图片但按 q 有效。请帮忙!
您正在调用 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)