|
1 | 1 | import cv2
|
2 | 2 | import numpy as np
|
3 | 3 | import os
|
4 |
| -import time |
| 4 | +import dlib |
| 5 | +from imutils import face_utils |
| 6 | +from imutils.face_utils import FaceAligner |
5 | 7 |
|
6 |
| -face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml') |
| 8 | +detector = dlib.get_frontal_face_detector() |
| 9 | +shape_predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") |
| 10 | +face_aligner = FaceAligner(shape_predictor, desiredFaceWidth=200) |
| 11 | + |
| 12 | +#face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml') |
7 | 13 |
|
8 | 14 | video_capture = cv2.VideoCapture(0)
|
9 | 15 |
|
|
17 | 23 | os.makedirs(directory, exist_ok = 'True')
|
18 | 24 |
|
19 | 25 | number_of_images = 0
|
20 |
| -MAX_NUMBER_OF_IMAGES = 10 |
| 26 | +MAX_NUMBER_OF_IMAGES = 50 |
21 | 27 | count = 0
|
22 | 28 |
|
23 | 29 | while number_of_images < MAX_NUMBER_OF_IMAGES:
|
24 | 30 | ret, frame = video_capture.read()
|
25 | 31 |
|
26 | 32 | frame = cv2.flip(frame, 1)
|
27 | 33 |
|
28 |
| - faces = face_cascade.detectMultiScale(frame, 1.3, 5) |
29 |
| - for(x,y,w,h) in faces: |
30 |
| - cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),2) |
31 |
| - roi = frame[y:y+h, x:x+w] |
32 |
| - if count == 10: |
33 |
| - cv2.imwrite(os.path.join(directory, str(name+str(number_of_images)+'.jpg')), roi) |
| 34 | + frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| 35 | + |
| 36 | + #faces = face_cascade.detectMultiScale(frame, 1.3, 5) |
| 37 | + faces = detector(frame_gray) |
| 38 | + if len(faces) == 1: |
| 39 | + face = faces[0] |
| 40 | + (x, y, w, h) = face_utils.rect_to_bb(face) |
| 41 | + face_img = frame_gray[y-50:y + h+100, x-50:x + w+100] |
| 42 | + face_aligned = face_aligner.align(frame, frame_gray, face) |
| 43 | + |
| 44 | + if count == 5: |
| 45 | + cv2.imwrite(os.path.join(directory, str(name+str(number_of_images)+'.jpg')), face_aligned) |
34 | 46 | number_of_images += 1
|
35 | 47 | count = 0
|
36 | 48 | print(count)
|
|
0 commit comments