Ejemplo n.º 1
0
    def _get_face_embed(img: np.ndarray, proc_size: int,
                        mode: str) -> Union[np.ndarray, None]:
        capt_faces = FaceCapture.cap(img)
        if not capt_faces.has_face:
            print(
                f"[WARN] {__name__} ♦ failed to get face-embed cuz FaceCapture can't capture any faces"
            )
            return

        if capt_faces.face_count != 1:
            print(
                f"[WARN] {__name__} ♦ failed to get face-embed cuz the count of CapturedFace is not 1"
            )
            return

        face = Cropper.get_cropped_face(img, capt_faces[0], 0)

        if mode == 'cv2-dnn':
            embed_model = ModelAPI.get('openface-embed')
            face_blob = ImgTool.get_dnn_blob(img, proc_size)
            if face_blob is not None:
                embed_model.setInput(face_blob)
                vec = embed_model.forward()
                return vec.flatten()

        elif mode == 'fr-dlib':
            face = ImgTool.get_rgb_img(face)
            if face is not None:
                face = ImgTool.resize(face, proc_size)
                return face_recognition.face_encodings(face)[0]
Ejemplo n.º 2
0
 def get_smart_cropped_face(img: np.ndarray, face_block: dlib.rectangle):
     for i in range(10, -1, -1):
         margin = i / 10
         cropped = Cropper.get_cropped_face(img, face_block, margin)
         h, w, _ = cropped.shape
         if h > 0 and w > 0:
             capt_faces = FaceCapture.cap(cropped)
             if capt_faces.face_count == 1:
                 return cropped
Ejemplo n.º 3
0
    def get_encodes_of_faces_in_frame(img: np.ndarray, mode: str) -> list:
        result = list()
        capt_faces = FaceCapture.cap(img)
        for face_block in capt_faces:
            face_encode = FacesProc.get_face_encode_from_raw(
                mode, img, face_block)
            if face_encode is not None:
                result.append(face_encode)

        return result
Ejemplo n.º 4
0
    def open_camera(self):
        UITool.msg_window('Info', 'Start face distant protect mode.')
        flag = True
        vs = WebcamVideoStream(src=0)
        vs.start()
        time.sleep(2.0)
        cv2.startWindowThread()
        while flag:
            check = True
            frame = vs.read()
            frame = imutils.resize(frame, width=800)
            display_frame = frame.copy()
            capt_faces = FaceCapture.cap(frame)
            if capt_faces.has_face:
                display_frame = ImgTool.add_face_block(display_frame,
                                                       capt_faces)
                face_data = FacesProc.get_all_faces_with_encode_low_filter(
                    frame, self.mode)
                if face_data:
                    check = self.check_face_encode(face_data)
                    print(f"status: {check}")
                    if not check:
                        print(
                            f" WARN - find stranger and no master in the same time !!!"
                        )
                        self.alert = True
                else:
                    print('fetched face data is not enough')

            if self.display:
                display_frame = cv2.flip(display_frame, 1)
                if check:
                    text = 'Safe'
                    print(text)
                    display_frame = ImgTool.add_text(display_frame, text)
                else:
                    text = 'Detect Stranger!'
                    display_frame = ImgTool.add_text(display_frame,
                                                     text,
                                                     color='red')

                cv2.imshow('Frame', display_frame)

            key = cv2.waitKey(1)
            if self.alert or key == 27 or key == ord('q'):
                flag = False
                cv2.waitKey(500)
                cv2.destroyAllWindows()
                cv2.waitKey(500)

        vs.stop()
        cv2.destroyAllWindows()
Ejemplo n.º 5
0
 def get_rotated_face(img: np.ndarray) -> np.ndarray:
     img = img.copy()
     capt_faces = FaceCapture.cap(img)
     if capt_faces.face_count == 1:
         face_block = capt_faces[0]
         lmk_detector = ModelAPI.get('dlib-lmk68')
         landmark = FaceRotate.get_landmarks(lmk_detector, img, face_block)
         if landmark:
             left_eye_coord = FaceRotate.get_left_eye_center(landmark)
             right_eye_coord = FaceRotate.get_right_eye_center(landmark)
             angle_to_hrzl = FaceRotate.get_angle_to_hrzl(
                 left_eye_coord, right_eye_coord)
             return ImgTool.get_rotate_img(img, angle_to_hrzl)
Ejemplo n.º 6
0
    def launch(self):
        flag = True
        vs = WebcamVideoStream(src=0)
        vs.start()
        time.sleep(2.0)
        fetch_face = 0
        last_face_ts = 0
        cv2.startWindowThread()
        while flag:
            frame = vs.read()
            if frame is None:
                continue
            frame = imutils.resize(frame, width=800)
            display_frame = frame.copy()
            capt_faces = FaceCapture.cap(frame)
            if capt_faces.face_count == 1:
                display_frame = ImgTool.add_face_block(display_frame,
                                                       capt_faces)
                ts = FileTool.get_curr_ts()
                if ts - last_face_ts > RecordMD.INTERVAL:
                    if self.__record_face(frame, capt_faces, ts):
                        fetch_face += 1
                        print(
                            f'fetch face successfully. ({fetch_face}/{self.fetch_count})'
                        )
                        last_face_ts = ts
                    else:
                        print(f'failed to record faces')

            if self.display:
                display_frame = cv2.flip(display_frame, 1)
                text = f"{fetch_face} / {self.fetch_count}"
                display_frame = ImgTool.add_text(display_frame, text)
                cv2.imshow('Frame', display_frame)

            key = cv2.waitKey(1)

            if fetch_face == self.fetch_count or key == 27 or key == ord('q'):
                flag = False
                cv2.waitKey(500)
                cv2.destroyAllWindows()
                cv2.waitKey(500)

        vs.stop()
        cv2.destroyAllWindows()

        # self.__img_meta.to_csv(RecordMD.meta_path, index=False, encoding='utf-8')
        msg = 'Finish recoding master data.'
        UITool.msg_window(msg=msg)

        print('finish saving img meta.')
Ejemplo n.º 7
0
    def get_all_faces_with_encode_low_filter(img: np.ndarray,
                                             mode: str) -> list:
        result = list()
        capt_faces = FaceCapture.cap(img)
        for face_block in capt_faces:
            cropped_face = Cropper.get_smart_cropped_face(img, face_block)
            face_grid = FacesProc.get_face_grid(cropped_face)
            if face_grid is None:
                print('failed to get face grid')
                continue

            face_encode = FacesProc.get_face_encode(img, mode)
            if face_encode is not None:
                record = {'portrait': cropped_face, 'encode': face_encode}
                result.append(record)
            else:
                print('failed to get face encode')

        return result
Ejemplo n.º 8
0
    def get_face_encode_from_raw(mode: str,
                                 img: np.ndarray,
                                 face_block=None) -> Union[np.ndarray, None]:
        if face_block is not None:
            portrait = FacesProc.get_portrait(img, face_block)
            if portrait is None:
                return

            face_grid = FacesProc.get_face_grid(portrait)
            if face_grid is None:
                return

            return FacesProc.get_face_encode(face_grid, mode)

        else:
            capt_faces = FaceCapture.cap(img)
            if capt_faces.face_count != 1:
                return
            face_block = capt_faces[0]
            return FacesProc.get_face_encode_from_raw(mode, img, face_block)
Ejemplo n.º 9
0
    def save_image(self, data):
        result = list()
        image_id = data[0]
        image = data[1]
        count = 0

        capt_faces = FaceCapture.cap(image)
        for i, fb in enumerate(capt_faces, start=1):
            cropped_face = Cropper.get_smart_cropped_face(image, fb)
            if cropped_face is not None:
                rotated = FaceRotate.get_rotated_face(cropped_face)
                if rotated is not None:
                    if RecogTool.can_get_embed(rotated, 'cv2-dnn'):
                        image_name = f"{FileTool.gen_random_token()}.jpg"
                        img_path = f"{self.img_dir}/{image_name}"
                        cv2.imwrite(img_path, rotated)
                        count += 1

        result.append(image_id)
        result.append(count)
        return result
Ejemplo n.º 10
0
    def get_face_encode(img: np.ndarray,
                        mode: str,
                        proc_size=96) -> Union[np.ndarray, None]:
        """
        1. cv2-dnn
        2. fr-dlib

        :param img:
        :param mode:
        :param proc_size:
        :return:
        """
        if mode == 'cv2-dnn':
            embed_model = ModelAPI.get('openface-embed')
            img = face_recognition.load_image_file(img)
            face_blob = ImgTool.get_dnn_blob(img, proc_size)
            if face_blob is not None:
                embed_model.setInput(face_blob)
                vec = embed_model.forward()
                print(vec)
                return vec.flatten()

        elif mode == 'fr-dlib':
            face = ImgTool.get_rgb_img(img)
            if face is None:
                return

            face = ImgTool.resize(face, proc_size)
            if face is None:
                return

            capt_faces = FaceCapture.cap(face)
            if capt_faces.face_count != 1:
                return

            fb = capt_faces[0]
            box = FacesProc.dlib_rect_to_boxes(fb)
            result = face_recognition.face_encodings(face, [box])
            if len(result):
                return result[0]
Ejemplo n.º 11
0
    def get_all_faces_with_encode(img: np.ndarray, mode: str) -> list:
        result = list()
        capt_faces = FaceCapture.cap(img)
        for face_block in capt_faces:
            portrait = FacesProc.get_portrait(img, face_block)
            if portrait is None:
                print('failed to get portrait from image')
                continue

            face_grid = FacesProc.get_face_grid(portrait)
            if face_grid is None:
                print('failed to get face grid from image')
                continue

            face_encode = FacesProc.get_face_encode(img, mode)
            if face_encode is not None:
                record = {'portrait': portrait, 'encode': face_encode}
                result.append(record)
            else:
                print('failed to get face encode from image')

        return result
Ejemplo n.º 12
0
import cv2

from face_ult.cropper import Cropper
from face_ult.face_capture import FaceCapture
from face_ult.face_rotate import FaceRotate

img_path = 'b.jpg'
image = cv2.imread(img_path)

if __name__ == '__main__':
    capt_faces = FaceCapture.cap(image)
    for i, fb in enumerate(capt_faces, start=1):
        # cropped_face = Cropper.get_cropped_face(image, fb, 0.9)
        cropped_face = Cropper.get_smart_cropped_face(image, fb)
        rotated = FaceRotate.get_rotated_face(cropped_face)
        cf = FaceCapture.cap(rotated)
        print(cf.face_count)
Ejemplo n.º 13
0
 def get_face_grid(img: np.ndarray) -> np.ndarray:
     capt_faces = FaceCapture.cap(img)
     if capt_faces.has_face:
         face_block = capt_faces[0]
         return Cropper.get_cropped_face(img, face_block, 0)
Ejemplo n.º 14
0
 def is_single_face(img: np.ndarray) -> bool:
     if FaceCapture.cap(img).face_count == 1:
         return True
     else:
         return False