你好!
[我正在尝试使用OpenCV编写模型以检测面部,睁开眼睛和微笑,并在检测到后自动拍摄并保存快照。
我正在使用此代码,该代码可以进行检测,但是在按下键(在我的情况下为“ q”)时拍摄快照。但是我想在此刻检测到所有功能(面部,眼睛,微笑)时实现自动中断控制。
我将不胜感激任何建议:)
def detect(gray, frame):
faces = face_cascade.detectMultiScale(gray, 1.5, 7)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), ((x + w), (y + h)), (255, 0, 0), 2)
reg_gray = gray[y:y + h, x:x + w]
reg_color = frame[y:y + h, x:x + w]
smiles = smile_cascade.detectMultiScale(reg_gray, 1.8, 20)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(reg_color, (sx, sy), ((sx + sw), (sy + sh)), (0, 0, 255), 2)
for (p, q, w, h) in faces:
cv2.rectangle(frame, (p, q), ((p + w), (q + h)), (255, 0, 0), 2)
reg_gray = gray[q:q + h, p:p + w]
reg_color = frame[q:q + h, p:p + w]
eyes = eye_cascade.detectMultiScale(reg_gray, 1.8, 20)
for (sp, sq, sw, sh) in eyes:
cv2.rectangle(reg_color, (sp, sq), ((sp + sw), (sq + sh)), (0, 255, 0), 2)
return frame
video_capture = cv2.VideoCapture(0)
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
canvas = detect(gray, frame)
cv2.imshow('Video', canvas)
# The control breaks once q is pressed
if cv2.waitKey(1) & 0xff == ord('q'):
break
video_capture.release()
类似:
if len(smiles) > 0 and len(eyes) > 0:
cv2.imwrite("smile_and_eyes.jpg", canvas)
应该。