OpenCV: сделать снимок при обнаружении лица, глаз и улыбки - PullRequest
0 голосов
/ 27 мая 2020

Привет!

Я пытаюсь написать модель с OpenCV, чтобы обнаруживать лицо, открывать глаза и улыбаться, и после обнаружения автоматически делаю и сохраняю снимок.

Я использую этот код, который выполняет обнаружение, но делает снимок при нажатии клавиши (в моем случае «q»). Но я хочу добиться автоматического c прерывания управления, когда все особенности (лицо, глаза, улыбка) будут обнаружены в данный момент.

Буду признателен за любой совет :)

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()                 

1 Ответ

0 голосов
/ 27 мая 2020

что-то вроде:

if len(smiles) > 0 and len(eyes) > 0:
     cv2.imwrite("smile_and_eyes.jpg", canvas)

должно подойти.

...