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]
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
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")
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
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
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")
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")
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
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) # *=======================* # | Extract Faces Process | # *=======================* cpt = 0 t2 = time.time() for img_path in data.image: Colors.print_infos("\n[PROCESSING] Try to Detect a Person...") # *===================* # | Performed Process | # *===================* yolo_result = obj.run(img_path) if re.match('person', yolo_result): Colors.print_sucess("[PROCESSING] Person Detected !") Colors.print_infos("[PROCESSING] Running Extract Faces Processing...") # print(img_path) Colors.print_infos("[PROCESSING] Extract Faces {}/{}".format(cpt + 1, len(data.image))) t1 = time.time() result = fd.ExtractFace(cv2.imread(img_path), "Data/IMAGE_DB/" + str(data.name[cpt]) + "/result_" + str(cpt)) 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)
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
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
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
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")
result_voit += " " + yolo_result.Result[cpt] # *======================================* # | Remove Images that nothings detected | # *======================================* cpt = 0 # sql = SQLHelpers() for im1 in images: for im2 in yolo_result.Images: if im1 not in im2: id = re.findall(r'\b\d+\b', im1) if len(id) == 2: Colors.print_error("[DELETE] Delete Images : " + im1 + " With Id : " + str(id[1])) # sql.delete_frames(str(id[0]), str(id[1])) os.system("rm -rfv " + im1) cpt += 1 if result_pers: Colors.print_sucess("Person Detected : " + result_pers) if result_voit: Colors.print_sucess("Voiture Detected : " + result_voit) Colors.print_sucess("\n[SUCCESS] Finished with Total processing time : " + str(round(time.time() - t1, 3)) + " s") _remove_lock() del images del t1
# result = None # # if re.match('person', yolo_result) and use_FacialRecognizer: # # print("Found : Person") # if 'Person' not in final_val: # final_val += " Person" # # result = _reconignizing(face_detector, recognizer, img) # # elif re.match('car', yolo_result) and use_ALPR: # # print("Found : CAR AND ALPR") # if 'Car' not in final_val: # final_val += " Car" # # TODO ALPR RECOGNIZING # # elif yolo_result is None or yolo_result == "": # # os.system("mv " + img + " /home/zerocool/PycharmProjects/FacialRecognizionTFE/Test/FaceRecognizerV4.0/IMG_DELETED") # if verbose: # Colors.print_error("[ERROR] Nothing Found: " + img) # cpt += 1 # # if verbose: # Colors.print_infos("\n[PROCESSING] Processing Image {0}/{1} in {2} s".format(cpt + 1, len(images), round(time.time()-t2, 3))) if result: print("Detected : " + result) Colors.print_sucess("\n[SUCCESS] Finished with Total processing time : " + str(round(time.time() - t1, 3)) + " s") del images del t1