Exemple #1
0
def main():
    face_detector = FaceDetector(device='cuda')
    cap = cv2.VideoCapture(-1)
    fr_cnt = 0
    st_time = time.time()
    interval = 5
    info = 'calculating...'
    faces = torch.zeros((112,112))
    cnt = 0
    totime = time.time()
    while cap.isOpened() and time.time()-totime < 60:
        isSuccess, image = cap.read()
        if isSuccess:
            fr_cnt += 1
            cnt += 1
            bounding_boxes,landmarks = face_detector.detect(image)
            if bounding_boxes is not None and bounding_boxes.numel() > 0:
                image, faces = drawBoxesGetFaces(image, bounding_boxes, landmarks)

        if time.time() - st_time >= interval:
            info = str(fr_cnt/interval) + "FPS | " + str(image.shape)
            print (info)
            fr_cnt = 0
            st_time = time.time()

        image = writeText(image, info)
        cv2.imshow('debug', image)
        cv2.imshow('face', faces[0])
        if cv2.waitKey(1)&0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
Exemple #2
0
class FaceID():
    def __init__(self):
        self._camera = cv2.VideoCapture(0)
        self._camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
        self._camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)
        self._windowManager = WindowManager('Face ID', self.onKeypress)
        self._captureManager = CaptureManager(self._camera,
                                              self._windowManager, True)
        self._faceDetector = FaceDetector()

    def run(self):  # run the application
        self._windowManager.createWindow()
        while self._windowManager.isWindowCreated:
            self._captureManager.enterFrame()
            frame = self._captureManager.frame

            self._faceDetector.update(frame)
            faces = self._faceDetector.faces

            for face in faces:
                x, y, h, w = face.faceRect
                face_image = frame[y:y + h, x:x + w]
                cv2.imshow('face', face_image)
                break

            self._captureManager.exitFrame()
            self._windowManager.processEvents()

    def onKeypress(self, keycode):
        # handle a keypress
        if keycode == 27:  # escape
            self._windowManager.destroyWindow()
            self._camera.release()
        elif chr(keycode).isalnum():
            self._windowManager.appendChar(chr(keycode))
Exemple #3
0
 def __init__(self):
     self._camera = cv2.VideoCapture(0)
     self._camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
     self._camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)
     self._windowManager = WindowManager('Face ID', self.onKeypress)
     self._captureManager = CaptureManager(self._camera,
                                           self._windowManager, True)
     self._faceDetector = FaceDetector()
Exemple #4
0
def main():
    detector = FaceDetector()
    #    prob = detector.detect()
    #    if prob == None:
    #        print("No face detected")
    #        return
    #
    #    print(f"face detected with {(prob * 100):.2f}% certainty")
    prob = detector.detect()
    if not prob:
        print("No face detected")
        return

    print(f"face detected with {(prob * 100):.2f}% certainty")
Exemple #5
0
def create_database(img_dir, file):
    image = []
    gender = []
    age = []
    emotion = []

    detector = FaceDetector()
    dic = read_property(file)

    #     abs_path = os.path.join(img_dir, 'CF7AF8-96DD-480B-9A50-AC5C3787DA08.jpeg')
    #     img = cv2.imread(abs_path)
    #     _, points = detector.detect(img)
    #     aligned = detector.align(img, landmark = points[:, 0].reshape( (2,5) ).T, image_size = [112, 112])
    #     resized = cv2.resize(aligned, (64,64))
    num = 0
    for key, value in dic.items():
        num += 1
        print(str(num))
        print(key)

        abs_path = os.path.join(img_dir, key)
        if not os.path.exists(abs_path):
            continue
        img = cv2.imread(abs_path)
        _, points = detector.detect(img)

        if len(points) != 0:
            if len(points[0]) != 0:
                aligned = detector.align(img,
                                         landmark=points[:, 0].reshape(
                                             (2, 5)).T,
                                         image_size=[112, 112])
                resized = cv2.resize(aligned, (64, 64))

                image.append(resized)
                gender.append(value[0])
                age.append(value[1])
                emotion.append(value[2])

    mat = {
        'image': np.array(image),
        'gender': np.array(gender),
        'age': np.array(age),
        'emotion': np.array(emotion),
        'db': "kiwi_db",
        'img_size': 64,
        'min_score': 1
    }
    scipy.io.savemat("kiwi_db.mat", mat)
Exemple #6
0
def extract_embedding(image,
                      face_detector=None,
                      face_encoder=None,
                      face_aligned=True):
    if face_detector is None:
        face_detector = FaceDetector(model=g_detector,
                                     path=join(ROOT_DIR,
                                               INPUT_DIR_MODEL_DETECTION))
        face_encoder = FaceEncoder(model=g_encoder,
                                   path=join(ROOT_DIR,
                                             INPUT_DIR_MODEL_ENCODING),
                                   path_training=join(
                                       ROOT_DIR, INPUT_DIR_MODEL_TRAINING),
                                   training=False)
    if True in [
            image.lower().endswith(ext)
            for ext in ["jpg", "jpeg", "bmp", 'png']
    ]:
        # if imghdr.what(image) in ['jpg', 'jpeg', 'png', 'JPG', 'bmp']:
        tmp_image = cv2.imread(image)
        rgb_image = cv2.cvtColor(tmp_image, cv2.COLOR_BGR2RGB)
        if not face_aligned:
            face_locations = face_detector.detect(tmp_image)
            if len(face_locations) != 1:
                print('invalid image: %s' % image)
                WriteLog(
                    'num face not equal 1: %s\n' % (os.path.split(image)[1]),
                    'make_data.log')
                return
            if is_save_face == True:
                path, filename = os.path.split(image)
                save_image(face_locations, tmp_image, output_dir, filename)
            face_emb = face_encoder.encode(rgb_image, face_locations)
        else:
            #get embeding on aligned face
            face_emb = face_encoder.encode(rgb_image)
            #get embedding by insightface
            # img = np.transpose(rgb_image, (2,0,1))
            # face_emb = model.get_feature(img)
        if face_emb is None:
            WriteLog(
                'can not get embedding from image: %s\n' %
                (os.path.split(image)[1]), 'make_data.log')
        return face_emb
Exemple #7
0
class CustomModelProcessor(BaseProcessor):
    def __init__(self):
        super().__init__()
        self.recognizer = Recognizer()
        self.detector = MaskDetector()
        self.face_detector = FaceDetector()
        self.dlibProcessor = DlibProcessor()

    def get_face_locations(self, image):
        return self.face_detector.detect_faces(image)

    def process_detected_faces(self, image, faces):
        if faces is None or len(faces) == 0:
            return

        detected_faces, locations = [], []
        for x, y in faces:
            detected_faces.append(x)
            locations.append(y)

        mask_per_person = self.detector.predict_mask(detected_faces)
        if len(mask_per_person) < len(faces):
            return
        all_without_mask = self.is_all_without_masks(mask_per_person)
        if all_without_mask:
            self.dlibProcessor.process_single_image(image)
        else:
            self.process_with_masks(faces, image, locations, mask_per_person)

    def process_with_masks(self, faces, image, locations, mask_per_person):
        for index in range(0, len(faces)):
            location = locations[index]
            (mask, withoutMask) = mask_per_person[index]

            # determine the class label and color we'll use to draw
            # the bounding box and text
            in_mask = mask > withoutMask
            label = "Mask" if in_mask else "No Mask"
            if label == "Mask":
                color = (0, 255, 0)
            else:
                color = (0, 0, 255)

            label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
            image = self.draw_face_rectangle(image, location, label, color)

    def is_all_without_masks(self, mask_per_persons):
        if mask_per_persons is None or len(mask_per_persons) == 0:
            return True

        for index in range(0, len(mask_per_persons)):
            (mask, without_mask) = mask_per_persons[index]
            if mask > without_mask:
                return False
        return True
Exemple #8
0
def capture(label: str):
    cam = cv2.VideoCapture(0)
    detector = FaceDetector(cam)
    while True:
        _, frame = cam.read()
        cv2.imshow(label, frame)

        key = cv2.waitKey(1)
        if key == ord("q"):
            break
        if key == ord(" "):
            print("Checking for face...")
            if detector.detect(frame):
                print("Face detected, capturing image...")
                img_name = f"data/{label}/{str(int(time.time() * 1000))}.png"
                print(f"{label} image saved to: {img_name}")
                cv2.imwrite(img_name, frame)
            else:
                print("No face detected.")
    cam.release()
    cv2.destroyAllWindows()
Exemple #9
0
def web_cam(model_to_test):
    cap = cv2.VideoCapture(0)
    detector = FaceDetector()
    selector = ImageSelector()

    while True:
        ret, frame = cap.read()
        font = cv2.FONT_HERSHEY_COMPLEX
        faces, start_xs, start_ys, end_xs, end_ys = detector.get_all_faces(
            frame)

        for face, start_x, start_y, end_x, end_y in zip(
                faces, start_xs, start_ys, end_xs, end_ys):
            try:
                face = cv2.resize(face, (RESNET_IMG_WIDTH, RESNET_IMG_HEIGHT))
                face = process_image(face,
                                     mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
            except:
                break
            face = np.expand_dims(face, 0).astype(np.float16)
            prediction, prob = model_to_test.get_database_prediction(face)
            if prob > 0.5:
                name = selector.get_name_by_index(prediction.item())
            else:
                name = 'Unknown'

            cv2.rectangle(frame, (start_x, start_y), (end_x, end_y),
                          (0, 0, 255), 2)
            cv2.putText(frame, f"{name}:{prob * 100: .2f}%",
                        (start_x, start_y), font, 0.5, (255, 0, 0), 1)

        cv2.imshow("Camera", frame)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    cv2.destroyAllWindows()
    cap.release()
Exemple #10
0
    def __init__(self, name):

        self._configs = utils.loadConfigFile('facecapture.cf')
        self._users = utils.loadConfigFile('names.cf')

        self._name = name

        self._camera = cv2.VideoCapture(int(self._configs['capture.camera']))
        self._camera.set(cv2.CAP_PROP_FRAME_WIDTH,
                         int(self._configs['capture.width']))
        self._camera.set(cv2.CAP_PROP_FRAME_HEIGHT,
                         int(self._configs['capture.height']))

        self._windowManager = WindowManager(
            'Face Capture for {0}'.format(self._name), self.onKeypress)
        self._captureManager = CaptureManager(self._camera,
                                              self._windowManager, True)
        self._faceDetector = FaceDetector()

        self._filePath = '{0}'.format(self._configs['dataset.dir'])

        os.makedirs(self._filePath, exist_ok=True)
    def __init__(self, vision_api_client, doorbell, min_confidence,
        timeout_secs, verbose=False):
        """This is a `watchdog` event handler which monitors a directory for
        newly created image files, checking them for the presence of faces, and
        ringing a doorbell if it believes strongly enough that someone is
        waiting.

        All Vision API face detection annotations will have an associated
        confidence value, which must exceed the specified minimum confidence
        threshold (`min_confidence`) in order to trigger a doorbell ring.

        We also do not want to repeatedly ring for the same group of visitors,
        so we do not even bother checking for faces if we are within the
        specified `timeout_secs`.

        The `Observer` object for this event handler is created inside of here
        for simplicity's sake.

        Only the `on_create()` method will be overridden here, as we do not
        care about other file-system events.

        Args:
            vision_api_client: `visionapi.VisionAPIClient` object
            doorbell: `doorbell.SlackDoorbell` object
            min_confidence: minimum confidence threshold for face detection
            timout_secs: number of seconds to wait before ringing again
            verbose: (optional) if True, print info message for every event
        Returns:
            `FaceDetectionDoorbellRinger` event handler
        """
        self._detector = FaceDetector(vision_api_client)
        self._doorbell = doorbell
        self._min_confidence = min_confidence
        self._timeout_secs = timeout_secs
        self._time_of_prev_ring = 0
        self._logger = Logger(LogLevel.ANY if verbose else LogLevel.INFO)
Exemple #12
0
# -*- coding:utf-8 -*
"""
@version: ??
@Author by Ggao
@Mail: [email protected]
@File: demo.py
@time: 2019-11-15 上午9:48
"""
from detector import FaceDetector
from keypoints import FaceKps
import cv2
import os

if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"] = ""
    detector = FaceDetector("./model/det.pb")
    keypoints = FaceKps("./model/kps.pb")
    img = cv2.imread("demo.jpg")
    bnds = detector.predict(img, 1024)
    for bnd in bnds:
        bnd = [int(x) for x in bnd]
        x0, y0, x1, y1 = bnd
        pts = keypoints.predict(img, bnd)
        img = keypoints.vis(img, pts)
        cv2.rectangle(img, (x0, y0), (x1, y1), (255, 0, 0), 2)
    cv2.imwrite("res.jpg", img)

Exemple #13
0
def parallel_extract(image_parent_dir, num_worker=24, min_photo=15):
    '''
    image_parent_dir: folder contains photo folders
    '''
    if num_worker == 1:
        face_detector = FaceDetector(model=g_detector,
                                     path=join(ROOT_DIR,
                                               INPUT_DIR_MODEL_DETECTION))
        face_encoder = FaceEncoder(model=g_encoder,
                                   path=join(ROOT_DIR,
                                             INPUT_DIR_MODEL_ENCODING),
                                   path_training=join(
                                       ROOT_DIR, INPUT_DIR_MODEL_TRAINING),
                                   training=False)
    RemoveLog('make_data.log')
    dict_face_encodings = {}
    src_dirs = [
        d for d in os.listdir(image_parent_dir)
        if os.path.isdir(os.path.join(image_parent_dir, d))
    ]
    file_count = 0
    processed_file_count = 0
    for folder in src_dirs:
        try:
            list_known_face_encodings = []
            files = glob.glob(join(image_parent_dir, folder) + '/*')
            if len(files) < min_photo:
                WriteLog("ignore {}".format(folder), 'make_data.log')
                continue
            file_count += len(files)
            if num_worker > 1:
                with concurrent.futures.ProcessPoolExecutor(
                        max_workers=num_worker) as executor:
                    gen_tmp = executor.map(extract_embedding, files)
                list_known_face_encodings = [
                    emb for emb in gen_tmp if emb is not None
                ]
            else:
                for image in tqdm(files):
                    try:
                        face_embedding = extract_embedding(image,
                                                           face_detector,
                                                           face_encoder,
                                                           face_aligned=True)
                        if face_embedding is not None:
                            list_known_face_encodings.append(face_embedding)
                    except Exception as ex:
                        print(ex)

            dict_face_encodings[str(folder)] = list_known_face_encodings
            WriteLog(
                " {} processed {}/ {}".format(folder,
                                              len(list_known_face_encodings),
                                              len(files)), 'make_data.log')
            processed_file_count += len(list_known_face_encodings)
        except Exception as ex:
            print(ex)
    print("Processed: ", processed_file_count, ", total files:", file_count)

    face_embeddings_path = join(
        ROOT_DIR, 'models/embeddings/{}_face_encodings_{}.pickle'.format(
            g_encoder.value, len(dict_face_encodings)))
    with open(face_embeddings_path, 'wb+') as handle:
        pickle.dump(dict_face_encodings,
                    handle,
                    protocol=pickle.HIGHEST_PROTOCOL)
    return dict_face_encodings
Exemple #14
0
    for i in range(len(hull)):
        hullMask.append(hull[i])
    mask = np.zeros(shape, dtype=ty)
    cv2.fillConvexPoly(mask, np.int32(hullMask), (255, 255, 255))
    r = cv2.boundingRect(np.float32(hull))
    center = (r[0] + r[2] // 2, r[1] + r[3] // 2)
    ret = cv2.seamlessClone(np.uint8(warped_im), img, mask, center, 1)
    return ret


if __name__ == '__main__':

    logging.info('Start.')

    img = load_img(2)
    det = FaceDetector('hog')  # haar, nn, hog
    pose_estimator = load_pose_estimator()
    logging.info('Detector loaded.')

    face_coords = det.detect(img.copy())
    logging.info('{} Faces detected.'.format(str(face_coords.shape[0])))
    # print(face_coords[0].shape)
    #   print(face_coords[0].shape)

    face_shape = pose_estimation(img, face_coords, pose_estimator)
    marks = predictor_shape_to_coord_list(face_shape)
    np.save("fcs_crds.npy", face_coords)

    np.save("fcs_shp.npy", face_shape)
    logging.info('Landmarks calculated.')
Exemple #15
0
 def __init__(self):
     super().__init__()
     self.recognizer = Recognizer()
     self.detector = MaskDetector()
     self.face_detector = FaceDetector()
     self.dlibProcessor = DlibProcessor()
class FaceDetectionDoorbellRinger(FileSystemEventHandler):

    def __init__(self, vision_api_client, doorbell, min_confidence,
        timeout_secs, verbose=False):
        """This is a `watchdog` event handler which monitors a directory for
        newly created image files, checking them for the presence of faces, and
        ringing a doorbell if it believes strongly enough that someone is
        waiting.

        All Vision API face detection annotations will have an associated
        confidence value, which must exceed the specified minimum confidence
        threshold (`min_confidence`) in order to trigger a doorbell ring.

        We also do not want to repeatedly ring for the same group of visitors,
        so we do not even bother checking for faces if we are within the
        specified `timeout_secs`.

        The `Observer` object for this event handler is created inside of here
        for simplicity's sake.

        Only the `on_create()` method will be overridden here, as we do not
        care about other file-system events.

        Args:
            vision_api_client: `visionapi.VisionAPIClient` object
            doorbell: `doorbell.SlackDoorbell` object
            min_confidence: minimum confidence threshold for face detection
            timout_secs: number of seconds to wait before ringing again
            verbose: (optional) if True, print info message for every event
        Returns:
            `FaceDetectionDoorbellRinger` event handler
        """
        self._detector = FaceDetector(vision_api_client)
        self._doorbell = doorbell
        self._min_confidence = min_confidence
        self._timeout_secs = timeout_secs
        self._time_of_prev_ring = 0
        self._logger = Logger(LogLevel.ANY if verbose else LogLevel.INFO)

    def _file_is_allowed(self, event):
        return not event.is_directory and \
            event.src_path.split('.')[-1].lower() in ALLOWED_EXTENSIONS

    def _already_rang_doorbell(self):
        return int(time.time()) - self._time_of_prev_ring < self._timeout_secs

    def _get_confidence(self, image_path):
        try:
            faces = self._detector.detect_faces(image_path)
            confidence = sum(faces) / len(faces)
        except VisionAPIError as err:
            self._logger.warning(err, 'Treating this as 0% confidence')
            confidence = 0. 
        return confidence

    def _ring_doorbell(self, confidence):
        try:
            self._doorbell.ring(confidence=confidence)
            self._time_of_prev_ring = int(time.time())
        except SlackDoorbellError as err:
            # TODO: Not all failed rings should be critical
            self._logger.critical(err)
            self._observer.stop()

    def on_created(self, event):
        """Overrides the `watchdog.events.FileSystemEventHandler.on_created()`.
        
        When a new file is created in our watch directory:
            1. Check if this was an image file
            2. Check if we have already rang the doorbell recently
            3. Check if faces are present in the image
            4. Check if average confidence of faces exceeds threshold
            5. Ring doorbell
            6. Update the timeout clock

        Args:
            event: `watchdog.events.FileSystemEvent` object
        Returns:
            None
        """
        self._logger.debug('Event triggered')

        if self._file_is_allowed(event) and not self._already_rang_doorbell():
            self._logger.debug('Checking for faces')
            confidence = self._get_confidence(event.src_path)
            self._logger.debug('Confidence of faces: %f' % confidence)

            if confidence >= self._min_confidence:
                self._logger.info('Ringing doorbell')
                self._ring_doorbell(confidence)
            else:
                self._logger.debug('Did not ring doorbell')
        else:
            self._logger.debug('Ignored event')

    def run(self, motion_output_dir, sleep_secs, recursive=False):
        """Create and run a `watchdog.observers.Observer` over the `motion`
        process's output directory, triggering potential doorbell rings (see
        `on_created()` meothod of this class).

        Args:
            motion_output_dir: path to `motion` daemon image output directory
            sleep_secs: number of seconds to sleep between polling
            recursive: (optional) recursively watch directory
        Returns:
            None
        """
        observer = Observer()
        observer.schedule(self, motion_output_dir, recursive=recursive)
        observer.start()

        self._logger.info('Monitoring %s for images %srecursively' % (
            motion_output_dir, '' if recursive else 'non-'))

        try:
            while True:
                time.sleep(sleep_secs)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()
# -*- coding: utf-8 -*-
import os
import cv2
import numpy as np
from detector import FaceDetector
from recogn import FaceRecognition

facer_detecor = FaceDetector()
facer_recogn = FaceRecognition()

#参数设置
frame_frequency = 1
face_verification_threshold = 1.0

#计算embedding之间的距离
def cal_dist(fea1, fea2):
    return np.sum(np.square(fea1 - fea2))

def video_2(video_path, features_template_list):

    vide_capture = cv2.VideoCapture(video_path)
    frame_height = vide_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
    frame_width = vide_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
    fps = vide_capture.get(cv2.CAP_PROP_FPS)

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output_datou.avi', fourcc, fps, (int(frame_width), int(frame_height)))

    frame_id = 0
    while vide_capture.isOpened():
Exemple #18
0
    def __ne__(self, other):
        return not self.__eq__(other)


def rel_change(a, b):
    x = (b - a) / max(a, b)
    print(x)
    return x


print("Video :" + videopath)
print("Frame Directory: " + dir)

cap = cv2.VideoCapture(str(videopath))

detector = FaceDetector('../model/weights.pkl')

curr_frame = None
prev_frame = None

frame_diffs = []
frames = []
ret, frame = cap.read()
i = 1

while (ret):

    src = frame

    # Start detection process
    boxes = []
Exemple #19
0
    id='tv_env-v0',
    entry_point='envs.tv_env:TVEnv',
    max_episode_steps=50,
    reward_threshold=100000.0,
)

# Get the environment and extract the number of actions.
env = gym.make(ENV_NAME)
np.random.seed(3210)
env.seed(3210)
# nb_actions = env.action_space.n

slot_width = stream_config.INPUT_WIDTH / env_config.num_slots

# cam = PiCamera(stream_config.VIDEO_STREAM, stream_config.INPUT_WIDTH)
detector = FaceDetector()
agent = FollowAgent(env)
agent.start_service(
    "/home/tomas/Projects/follow-me-tv/src/dqn_follow-me-tv-v0_weights_100000.h5f"
)


@app.route('/action', methods=['POST'])
def get_action():
    img = np.fromstring(request.data,
                        np.uint8).reshape(stream_config.INPUT_HEIGHT,
                                          stream_config.INPUT_WIDTH, 3)
    faces = detector.get_faces(img)
    if faces:
        centroid = detector.get_centroid(faces)
        slot = int(centroid / slot_width)
Exemple #20
0
class FaceCapture(object):
    def __init__(self, name):

        self._configs = utils.loadConfigFile('facecapture.cf')
        self._users = utils.loadConfigFile('names.cf')

        self._name = name

        self._camera = cv2.VideoCapture(int(self._configs['capture.camera']))
        self._camera.set(cv2.CAP_PROP_FRAME_WIDTH,
                         int(self._configs['capture.width']))
        self._camera.set(cv2.CAP_PROP_FRAME_HEIGHT,
                         int(self._configs['capture.height']))

        self._windowManager = WindowManager(
            'Face Capture for {0}'.format(self._name), self.onKeypress)
        self._captureManager = CaptureManager(self._camera,
                                              self._windowManager, True)
        self._faceDetector = FaceDetector()

        self._filePath = '{0}'.format(self._configs['dataset.dir'])

        os.makedirs(self._filePath, exist_ok=True)

    def run(self):

        if not self._name in self._users:
            exit(1)

        self._windowManager.createWindow()

        idx = 1

        while self._windowManager.isWindowCreated:

            self._captureManager.enterFrame()
            frame = self._captureManager.frame

            self._faceDetector.update(frame)
            faces = self._faceDetector.faces

            if len(faces) == 1:
                x, y, h, w = faces[0].faceRect
                face_image = frame[y:y + h, x:x + w]
                face_image = cv2.cvtColor(face_image, cv2.COLOR_RGB2GRAY)

                cv2.imshow('face', face_image)
                cv2.imwrite(
                    '{0}\\{1}-{2}.jpg'.format(self._filePath,
                                              self._users[self._name], idx),
                    face_image)
                idx += 1

                time.sleep(0.1)

            self._captureManager.exitFrame()

            self._windowManager.processEvents()

    def onKeypress(self, keycode):

        if keycode == 27:  # escape

            self._windowManager.destroyWindow()
            self._camera.release()

        elif keycode == ord('r'):  # record

            if self._isRecording:
                self._isRecording = False
            else:
                self._isRecording = True
import cv2

from detector import LandmarkDetector, FaceDetector
from headpose_estimator import HeadPoseEstimator

cap = cv2.VideoCapture(0)
_, start_frame = cap.read()

height, width = start_frame.shape[:2]
pose_estimator = HeadPoseEstimator(image_size=(height, width))
fd = FaceDetector()
ld = LandmarkDetector()

while True:
    _, frame = cap.read()
    frame = cv2.flip(frame, 2)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    success, dnn_face = fd.detect_with_dnn(frame, 0.75)

    if success:
        fd.draw_face(frame, dnn_face, (0, 0, 255))

        landmarks = ld.detect(gray, dnn_face)
        nose = landmarks[30]

        head_pose = pose_estimator.solve_pose_by_68_points(landmarks)

        pose_estimator.draw_face_direction(
            frame, nose, head_pose[0], head_pose[1])