【发布时间】:2020-09-14 10:08:59
【问题描述】:
你好!
我正在尝试使用 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()
【问题讨论】:
标签: opencv face-detection