Beispiel #1
0
def _training(object_detector, face_detector, pickle_data):
    Colors.print_sucess("[NEW] New Image Detected Run Analyse...\n")
    ex = ExtractFaces()
    ex.run(face_detector, object_detector, pickle_data)
    Colors.print_infos("[INFOS] Reloading the Serialized Data")
    recognizer.data = Serializer.loading_data(pickle_data)
    del ex
Beispiel #2
0
 def saving_faces(faces, pickle_face):
     Colors.print_infos("\n[SAVING] Serializing Faces...")
     f = open(pickle_face, "wb")
     f.write(pickle.dumps(faces))
     f.close()
     del f
     Colors.print_sucess("[SUCCESS] Serializing Faces Completed...\n")
Beispiel #3
0
 def start_threads(self):
     cpt = 0
     for thread in self.list_thread:
         if not thread.is_alive():
             thread.start()
             cpt += 1
             Colors.print_infos("[INFOS] Starting Thread {0}/{1}...".format(cpt, self.thread_launched))
Beispiel #4
0
 def waiting_threads(self):
     for thread in self.list_thread:
         if thread.is_alive():
             while thread.is_alive():
                 Colors.print_infos("[INFOS] Waiting Thread...\n")
                 self.thread_launched -= 1
                 thread.join()
Beispiel #5
0
 def saving_data(data, pickle_data):
     Colors.print_infos("\n[SAVING] Serializing Preformated Data...")
     # Serialize the model
     f = open(pickle_data, "wb")
     f.write(pickle.dumps(data))
     f.close()
     del f
     Colors.print_sucess("[SUCCESS] Serializing Completed\n")
Beispiel #6
0
 def saving_static(obj, pickle_obj):
     Colors.print_infos("\n[SAVING] Serializing Static object...")
     f = open(pickle_obj, "wb")
     f.write(pickle.dumps(obj))
     f.close()
     del f
     Colors.print_sucess(
         "[SUCCESS] Serializing Static object Completed...\n")
Beispiel #7
0
 def __init__(self):
     Subject.__init__(self)
     self._color = Colors()
     self._path = PATH()
     self._serializer = Serializer()
     self._facesThreads = []
     self._faces = []
     self._imgfaces = []
     self._running = 0
     self._total = 0
Beispiel #8
0
    def loading_faces(pickle_face):
        Colors.print_infos("[LOADING] Loading Faces Serialised...")
        faces = []

        # Load the serialised Data
        data = pickle.loads(open(pickle_face, "rb").read())
        for d in data:
            faces.append(d)
        del data

        Colors.print_sucess("[LOADING] Loading Faces Completed\n")
        return faces
Beispiel #9
0
    def loading_static(pickle_obj):
        Colors.print_infos("[LOADING] Loading Static object...")
        obj = []

        # Load the serialised Data
        data = pickle.loads(open(pickle_obj, "rb").read())
        for d in data:
            obj.append(d)
        del data

        Colors.print_sucess("[LOADING] Loading Static object Completed\n")
        return obj
Beispiel #10
0
def _top():
    os.system("clear")
    Colors.print_infos(
        "\n*-----------------------------------------------------*\n"
        "| __author__ = Jordan BERTIEAUX                       |\n"
        "| __copyright__ = Copyright 2020, Facial Recognition  |\n"
        "| __credits__ = [Jordan BERTIEAUX]                    |\n"
        "| __license__ = GPL                                   |\n"
        "| __version__ = 1.0                                   |\n"
        "| __maintainer__ = Jordan BERTIEAUX                   |\n"
        "| __email__ = [email protected]             |\n"
        "| __status__ = Production                             |\n"
        "*-----------------------------------------------------*\n")
Beispiel #11
0
def _reconignizing(face_detector, reco, imgPath):
    # *==================*
    # | Extract the Face |
    # *==================*
    Colors.print_infos("[INFOS] Person Was detected !\n"
                       "[PROCESSING] Running Detect Face Process...\n")

    result = face_detector.detectFaceTiny(frame=cv2.imread(imgPath))
    faces = result[0]
    refined_bbox = result[1]
    del result

    Colors.print_sucess("\n[PROCESSING] " + str(len(faces)) +
                        " Face Detected\n")

    if len(faces) > 0 and faces is not None:
        Colors.print_infos("[PROCESSING] Running Facial Recognizing...\n")

        result = reco.run(faces, face_detector, cv2.imread(imgPath),
                          refined_bbox)

        if result is not None:
            Colors.print_sucess("\n[SUCESS] Detected Person: " +
                                str(result[1]) + " \n")
            try:
                cv2.imwrite(imgPath, result[0])
                cv2.destroyAllWindows()
                return result[1]
            except:
                pass
        else:
            return None
Beispiel #12
0
    def _load_and_align_images(self, filepaths):
        aligned_images = []
        for filepath in filepaths:
            # print(filepath)
            img = cv2.imread(filepath)
            if img is not None:
                aligned = self._align_face(img)
                aligned = (aligned / 255.).astype(np.float32)
                aligned = np.expand_dims(aligned, axis=0)
                aligned_images.append(aligned)
            else:
                Colors.print_error("[ERROR] File Not Found in ImageDatabases: " + filepath)

        return np.array(aligned_images)
Beispiel #13
0
 def __init__(self,
              user='******',
              passwd='zmpass',
              host='localhost',
              database='zm'):
     try:
         self._db = mysql.connector.connect(host=host,
                                            user=user,
                                            passwd=passwd,
                                            database=database)
     except mysql.connector.Error as err:
         Colors.print_error("\n[ERROR] Could not connect to " + host + "\n")
         traceback.print_exc()
         exit(1)
Beispiel #14
0
    def __init__(self, frame, img_path):
        # ===============
        # Use the Builder
        # ===============
        self._color = Colors()

        # ================================================================
        # OpenCV DNN supports 2 networks.
        # 1. FP16 version of the original caffe implementation ( 5.4 MB )
        # 2. 8 bit Quantized version using Tensorflow ( 2.7 MB )
        # ================================================================
        self.DNN = "TF"

        # =======================================
        # Select the Network CAFFE or TensorFlow
        # ========================================
        if self.DNN == "CAFFE":
            self._modelFile = "Data/Model/res10_300x300_ssd_iter_140000_fp16.caffemodel"
            self._configFile = "Data/Model/deploy.prototxt"
            self._net = cv2.dnn.readNetFromCaffe(self._configFile,
                                                 self._modelFile)
        else:
            self._modelFile = "Data/Model/opencv_face_detector_uint8.pb"
            self._configFile = "Data/Model/opencv_face_detector.pbtxt"
            self._net = cv2.dnn.readNetFromTensorflow(self._modelFile,
                                                      self._configFile)

        # Select the confidence (0 to 1)
        self.conf_threshold = 0.8
        self.faces = frame
        self._img_path = img_path
Beispiel #15
0
    def run(self):
        cpt = 0
        for img in self.list_img:
            self.list_thread.append(ObjectDetector(self._confidence, self.threshold, self._data_load, self._show_percent, self.override_zm, self._lock, self, img, self.pattern))
            self.thread_launched += 1
            Colors.print_infos("[PROCESSING] Detection process {0}/{1}".format(cpt + 1, len(self.list_img)))
            cpt += 1
            if self.thread_launched == self.max_thread:
                self.start_threads()
                self.waiting_threads()
                self.clean_thread()

        data = {'Result': self._result_temp,
                'Images': self._image_temp}

        self.cleanning()
        return pd.DataFrame(data)
Beispiel #16
0
def _reconignizing(face_detector, reco, imgPath, faces, refined_bbox):
    if len(faces) > 0 and faces is not None:
        Colors.print_infos("[PROCESSING] Running Facial Recognizing...\n")

        result = reco.run(faces, face_detector, cv2.imread(imgPath),
                          refined_bbox)

        if result is not None:
            Colors.print_sucess("\n[SUCCESS] Found Person: " + str(result[1]) +
                                " \n")
            try:
                cv2.imwrite(imgPath, result[0])
                cv2.destroyAllWindows()
                return result[1]
            except:
                pass
        else:
            return None
Beispiel #17
0
def _extractfaces(yolo_result, face_detector):
    faces_extracted = []
    bbox_list = []
    faces_images = []
    cpt = 0

    score_final, average_image, clusters_w, clusters_h, normal_idx, clusters, session = _load_model(
        face_detector)

    with tf.compat.v1.Session() as sess:
        sess.run(tf.compat.v1.global_variables_initializer())

        for x in yolo_result.Images:
            if "person" in yolo_result.Result[cpt]:
                Colors.print_infos(
                    "[PROCESSING] Person was detected !\n"
                    "[PROCESSING] Running extract face process...\n")

                faces, refined_bbox = face_detector.detectFaceTiny(
                    cv2.imread(x), score_final, average_image, clusters_w,
                    clusters_h, normal_idx, clusters, session, sess)
                Colors.print_sucess("\n[PROCESSING] " + str(len(faces)) +
                                    " Face(s) Found(s)\n")

                faces_images.append(x)
                faces_extracted.append(faces)
                bbox_list.append(refined_bbox)

    Colors.print_sucess("[SUCESS] Extracted FINISHED")

    return [faces_extracted, bbox_list, faces_images]
Beispiel #18
0
def _getting_config(max_threads):
    # *=============================*
    # |  Read the ini config file   |
    # *=============================*
    Colors.print_infos("[INFOS] Reading config detector.ini...")
    config = ConfigParser(interpolation=ExtendedInterpolation())
    config.read(config_path)

    # *=============================*
    # | Create Face/object detector |
    # *=============================*
    Colors.print_infos(
        "[INFOS] Loading object/Face detector and Recognizer ...")
    object_detector = create_object_detector(config, max_threads)
    face_detector = create_face_detector(config)
    recognizer = Recognizer(
        config['Training']["data_Pickle"], config['Training']['train_embs'],
        config['FaceDetectorTiny']['Tiny_Face_detection_model'],
        config['Model']['OPENFACE_NN4_SMALL2_V1_H5'],
        config['Model']['PREDICATOR_68_FACE_LANDMARKS'])

    Colors.print_sucess(
        "[SUCCESS] Object/Face detector and Recognizer Loaded !")
    return _convert_boolean(
        config['General']['use_facial_recognizion']
    ), _convert_boolean(config['General']['use_alpr']), config['Training'][
        "data_Pickle"], object_detector, face_detector, recognizer, config
Beispiel #19
0
 def loading_data(pickle_data):
     if path.isfile(pickle_data):
         Colors.print_infos("[LOADING] Loading Data Serialised...")
         # Load the serialised Data
         data = pickle.loads(open(pickle_data, "rb").read())
         Colors.print_sucess("[LOADING] Loading Data Completed\n")
         return data
     else:
         Colors.print_error("[ERROR] File Not Found : " + str(pickle_data))
         return None
Beispiel #20
0
    def __init__(self, labelindex, nn4_small2, alignment, image_path, train_embs, subject, data):
        Thread.__init__(self)
        Observer.__init__(self)

        # *======================*
        # | Register the Subject |
        # *======================*
        self.register(subject)

        self._path = PATH()
        self._color = Colors()
        self._serializer = Serializer()
        self._train_paths = glob("Data/IMAGE_DB/*")
        self._label_index = labelindex
        self._nn4_small2 = nn4_small2
        self._alignment = alignment
        self._data = data
        self._image_path = image_path
        self._train_embs = train_embs
Beispiel #21
0
    def __init__(self, pickle_data, pickle_embs, tiny_model, openface_model, predicator_landmarks):
        self.data = Serializer.loading_data(pickle_data)
        self._pickles_embs = pickle_embs
        self._train_paths = glob.glob("Data/IMAGE_DB/*")
        self._nb_classes = len(self._train_paths)
        self._label_index = []

        self._tinyFace_model = tiny_face_model.Model(tiny_model)
        self._nn4_small2 = create_model()

        Colors.print_infos("[LOADING] Load the model size of openface")
        Colors.print_infos("[LOADING] Align the face Predicator 68 Face Landmarks")
        # self._nn4_small2.summary()

        self._nn4_small2.load_weights(openface_model)
        self._alignment = AlignDlib(predicator_landmarks)

        Colors.print_sucess("[LOADING] Loading Model Completed\n")
Beispiel #22
0
 def __init__(self, frame, img_path):
     self._color = Colors()
     self.faces = frame
     self._img_path = img_path
Beispiel #23
0
        return True
    else:
        del path
        return False


def check_file_to_detect():
    path = PATH()
    # print(path.IMAGE_DB_RAW)

    if len(list(paths.list_images(path.IMAGE_TO_DETECT))) > 0:
        del path
        return True
    else:
        del path
        return False


if __name__ == "__main__":
    color = Colors()
    if check_new_files():
        color.printing("info", "[NEW] New Image Detected Run Analyse...\n")
        fd = ExtractFaces()
        fd.run()
        del fd
    if check_file_to_detect():
        color.printing("info",
                       "[NEW] New Image To Detect Run Recognizing...\n")
        reco = Recognizer()
        reco.run()
Beispiel #24
0
class ExtractFaces(Subject):
    def __init__(self):
        Subject.__init__(self)
        self._color = Colors()
        self._path = PATH()
        self._serializer = Serializer()
        self._facesThreads = []
        self._faces = []
        self._imgfaces = []
        self._running = 0
        self._total = 0

    # ===========================================================================
    #         Function of main
    # ===========================================================================
    def run(self):
        self._color.printing("info", "[LOADING] Quantifying faces...")

        # Get list of Folder
        train_paths = glob.glob("IMAGE_DB_RAW/*")
        # print(train_paths)

        data = self._format_data(train_paths)
        self._thread_init(data)
        self._color.printing("success",
                             "[SUCCESS] Quantifying faces Finished\n")

        self._launch_detect_face()
        self._waiting_end_thread()

        # Saving Images
        self._saving()

    # ===========================================================================
    #         Create the Data Frame with Panda
    # ===========================================================================
    """
    @:parameter train_path = Path from glog (UNIX LIKE)
    """

    def _format_data(self, train_paths):
        data = pd.DataFrame(columns=['image', 'label', 'name'])

        for i, train_path in tqdm(enumerate(train_paths)):
            name = train_path.split("/")[-1]
            images = glob.glob(train_path + "/*")
            for image in images:
                data.loc[len(data)] = [image, i, name]

        # print(data)
        return data

    # ===========================================================================
    #         Get the Notify from DP Observer
    # ===========================================================================
    """
    @:update
    """

    def update(self, value, message):
        self._faces.append(value)
        self._imgfaces.append(message)
        self._running -= 1

    # ===========================================================================
    #         Initialize the list of threads
    # ===========================================================================
    """
    @:parameter data = DataFrame
    """

    def _thread_init(self, data):
        total = 0

        for img_path in data.image:
            # self._color.printing("info", "[LOADING] Create Threading {}/{}".format(total + 1, len(data.image)))
            # print(img_path)

            # Create the Thread
            frame = cv2.imread(img_path)
            self._facesThreads.append(FaceDetector(frame, img_path, self))
            total += 1

        self._color.printing("success",
                             "[SUCCESS] Create Threading Completed\n")

    def _waiting_end_thread(self):
        while self._running > 0:
            self._color.printing("info",
                                 "[WAITING] Waiting the end of Threads...")
            time.sleep(0.5)
        self._color.printing("success", "[SUCCESS] Thread Finished !\n")

    # ===========================================================================
    #         Launch the Threads
    # ===========================================================================
    """
    @:parameter data = the DataFrame from Panda
    @:parameter max = The maximum of Threads
    """

    def _launch_detect_face(self, max=15):

        while self._total < len(self._facesThreads):
            if self._running <= max:
                self._facesThreads[self._total].start()
                self._running += 1
                self._total += 1
                self._color.printing(
                    "info", "[PROCESSING] Processing image {}/{}".format(
                        self._total, len(self._facesThreads)))
            else:
                while self._running == 5:
                    time.sleep(0.1)

        self._color.printing("success",
                             "[SUCCESS] Processing image completed\n")

    def _saving(self):
        os.system("rsync -a " + self._path.IMAGE_DB_RAW + "/*  " +
                  self._path.IMAGE_DB)
        os.system("rm -rf " + self._path.IMAGE_DB_RAW + "/*")

        # Get list of Folder
        train_paths = glob.glob("Data/IMAGE_DB/*")
        data = self._format_data(train_paths)

        self._color.printing("success", "[SUCCESS] Extraction Completed\n")
        # print(data)
        # print(self._faces)
        self._serializer.saving_data(data)
        self._serializer.saving_faces(self._faces)
Beispiel #25
0
class FaceDetector(Thread, Observer):
    def __init__(self, frame, img_path, subject):
        # ===============
        # Use the Builder
        # ===============
        Thread.__init__(self)
        Observer.__init__(self)
        self._color = Colors()

        # ====================
        # Register the Subject
        # ====================
        self.register(subject)

        # ================================================================
        # OpenCV DNN supports 2 networks.
        # 1. FP16 version of the original caffe implementation ( 5.4 MB )
        # 2. 8 bit Quantized version using Tensorflow ( 2.7 MB )
        # ================================================================
        self.DNN = "TF"

        # =======================================
        # Select the Network CAFFE or TensorFlow
        # ========================================
        if self.DNN == "CAFFE":
            self._modelFile = "Data/Model/res10_300x300_ssd_iter_140000_fp16.caffemodel"
            self._configFile = "Data/Model/deploy.prototxt"
            self._net = cv2.dnn.readNetFromCaffe(self._configFile,
                                                 self._modelFile)
        else:
            self._modelFile = "Data/Model/opencv_face_detector_uint8.pb"
            self._configFile = "Data/Model/opencv_face_detector.pbtxt"
            self._net = cv2.dnn.readNetFromTensorflow(self._modelFile,
                                                      self._configFile)

        # Select the confidence (0 to 1)
        self.conf_threshold = 0.8
        self.faces = frame
        self._img_path = img_path

    # ===========================================================================
    #         Override the start Thread
    # ===========================================================================
    def run(self):
        self._detect_face(self.faces)

    # ===========================================================================
    #         Detect and return faces the face from Frame
    # ===========================================================================
    def _detect_face(self, frame):
        frame_copy = frame.copy()
        frameHeight = frame_copy.shape[0]
        frameWidth = frame_copy.shape[1]

        blob = cv2.dnn.blobFromImage(frame_copy, 1.0, (300, 300),
                                     [104, 117, 123], False, False)
        self._net.setInput(blob)
        detections = self._net.forward()

        faces = []
        for i in range(detections.shape[2]):

            confidence = detections[0, 0, i, 2]

            if confidence > self.conf_threshold:
                x1 = int(detections[0, 0, i, 3] * frameWidth)
                y1 = int(detections[0, 0, i, 4] * frameHeight)
                x2 = int(detections[0, 0, i, 5] * frameWidth)
                y2 = int(detections[0, 0, i, 6] * frameHeight)
                faces.append([x1, y1, x2, y2])
                try:
                    imsave(self._img_path, frame_copy[y1:y2, x1:x2])
                except:
                    self._color.printing(
                        "error",
                        "Error write detecting Faces : " + self._img_path)
                    os.system("rm -rf " + self._img_path)
                finally:
                    pass

        # print(faces)
        self.update_observer(faces, "Finished")
Beispiel #26
0
    clusters_h = clusters[:, 3] - clusters[:, 1] + 1
    clusters_w = clusters[:, 2] - clusters[:, 0] + 1
    normal_idx = np.where(clusters[:, 4] == 1)

    return [
        score_final, average_image, clusters_w, clusters_h, normal_idx,
        clusters, x
    ]


# ============================================= < MAIN FUNCTION > =====================================================

if __name__ == "__main__":
    while _check_running():
        time.sleep(1)
        Colors.print_infos("[INFOS] Waiting end !")

    # *=====================*
    # | Getting Args/Config |
    # *=====================*
    _enable_lock()
    _top()
    imagesdb, images, pattern, config_path, max_threads = _getting_args()
    use_FacialRecognizer, use_ALPR, pickle_data, object_detector, face_detector, recognizer, config = _getting_config(
        max_threads)

    # *==========================*
    # |  Check Training Process  |
    # *==========================*
    _training(object_detector, face_detector, pickle_data)
Beispiel #27
0
 def wait_lock(self):
     if self._lock.locked():
         while self._lock.locked() is True:
             Colors.print_infos("[INFOS] Waiting unlocked thread...")
Beispiel #28
0
    def run(self, fd, obj, pickle_data):
        # Get list of files in Folder
        data = Serializer.format_data(glob.glob("IMAGE_DB_RAW/*"))

        # *======================*
        # | Create Database Tree |
        # *======================*
        Colors.print_infos("[INFO] Create Folders of databases...\n")
        for name in data.name:
            if not os.path.isdir("Data/IMAGE_DB/" + name):
                os.mkdir("Data/IMAGE_DB/" + name)

        # *===================*
        # | Performed Process |
        # *===================*
        obj.list_img = list(paths.list_images('IMAGE_DB_RAW'))
        obj.override_zm = False
        yolo_result = obj.run()
        obj.override_zm = True

        # *=======================*
        # | Extract Faces Process |
        # *=======================*
        cpt = 0
        t2 = time.time()
        for img_path in yolo_result.Images:
            Colors.print_infos("\n[PROCESSING] Try to Detect a Person...")

            if "person" in yolo_result.Result[cpt]:
                Colors.print_infos(
                    "[PROCESSING] Person(s) Found(s) !"
                    "[PROCESSING] Running Extract Faces Processing...")

                # print(img_path)
                Colors.print_infos("[PROCESSING] Extract Faces {}/{}".format(
                    cpt + 1, len(yolo_result.Images)))
                t1 = time.time()
                path = "Data/IMAGE_DB/" + str(
                    data.name[cpt]) + "/result_" + str(cpt)
                result = fd.ExtractFace(cv2.imread(img_path), path)
                Colors.print_infos(
                    "[PROCESSING] Faces Detected : {} in {} s".format(
                        result,
                        time.time() - t1))
                del t1
                del result
            else:
                Colors.print_error("[PROCESSING] No Face Detected !")
            cpt += 1

        Colors.print_infos("[INFO] Remove file in IMG_DB_RAW...")
        os.system("rm -rfv IMAGE_DB_RAW/*")

        Colors.print_sucess("\n[SUCCESS] Extraction Completed in " +
                            str(round(time.time() - t2, 4)) + " s\n")

        # Cleanning RAM
        del data
        del fd
        del cpt
        del t2

        data = Serializer.format_data(glob.glob("Data/IMAGE_DB/*"))

        # Saving Data
        Serializer.saving_data(data, pickle_data)
Beispiel #29
0
if __name__ == "__main__":
    # *=====================*
    # | Getting Args/Config |
    # *=====================*
    _top()
    imagesdb, images, pattern, config_path = _getting_args()
    use_FacialRecognizer, use_ALPR, pickle_data, object_detector, face_detector, recognizer, config = _getting_config(
    )

    # *==========================*
    # |  Check Training Process  |
    # *==========================*
    if len(imagesdb) > 0:
        _training(object_detector, face_detector, pickle_data)
    else:
        Colors.print_infos("\n[INFO] No Faces to Train now\n")

    t1 = time.time()
    result = ''

    # *========================*
    # | check files to Detect  |
    # |         AND            |
    # | Launch Infos Extractor |
    # *========================*
    if len(images) > 0:
        cpt = 0

        # *=============================*
        # | Running the Object Detector |
        # *=============================*