def eval_process_image_dir(cluster_dict, images_path, max_num_proc_imgs=None, metric=2, threshold=0.73): Models.altered_mtcnn.keep_all = False try: eval_process_faces(images_path, max_num_proc_imgs=max_num_proc_imgs) except IncompleteDatabaseOperation: return cluster_dict_copy = cluster_dict.copy() def eval_process_image_dir_worker(con): embeddings_with_ids = list(DBManager.get_all_embeddings(with_ids=True)) eval_core_algorithm = EvalCoreAlgorithm(metric=metric, classification_threshold=threshold) # passing result cluster dict already overwrites it clustering_result = eval_core_algorithm.cluster_embeddings_no_split(embeddings_with_ids, existing_clusters_dict=cluster_dict, should_reset_cluster_ids=True, final_clusters_only=False) _, modified_clusters_dict, removed_clusters_dict = clustering_result DBManager.overwrite_clusters_simplified(modified_clusters_dict, removed_clusters_dict, con=con, close_connections=False) try: DBManager.connection_wrapper(eval_process_image_dir_worker) except IncompleteDatabaseOperation: overwrite_dict(cluster_dict, cluster_dict_copy)
def clear_data_worker(con): if data_kind_to_clear in ('l', 'b'): # TODO: How to use local connections here? Rollback on multiple? # clear_local_tables() drop_local_tables() if data_kind_to_clear in ('g', 'b'): # clear_central_tables(con=con, close_connections=False) drop_central_tables(con=con, close_connections=False) overwrite_dict(cluster_dict, dict()) if data_kind_to_clear == 'c': clear_clustering(con=con, close_connections=False) overwrite_dict(cluster_dict, dict())
def process_image_dir(cluster_dict, threshold=0.73, metric=2, **kwargs): """ Extract faces from user-chosen images and cluster them :param threshold: :param metric: :param cluster_dict: :param kwargs: :return: """ # TODO: Store entered paths(?) --> Makes it easier if user wants to revisit them, but probs rarely? images_path = user_choose_images_path() try: process_faces(images_path) except IncompleteDatabaseOperation: return cluster_dict_copy = cluster_dict.copy() def cluster_processed_faces(con): embeddings_with_ids = list(DBManager.get_all_embeddings(with_ids=True)) # TODO: Call reclassify handler here? # TODO: Clear existing clusters? Issues with ids etc.???? core_algorithm = CoreAlgorithm(metric=metric, classification_threshold=threshold) # passing result cluster dict already overwrites it clustering_result = core_algorithm.cluster_embeddings( embeddings_with_ids, existing_clusters_dict=cluster_dict, should_reset_cluster_ids=True, final_clusters_only=False) _, modified_clusters_dict, removed_clusters_dict = clustering_result DBManager.overwrite_clusters_simplified(modified_clusters_dict, removed_clusters_dict, con=con, close_connections=False) reset_cluster_ids(con=con, close_connections=False) new_cluster_dict = DBManager.load_cluster_dict(con=con, close_connections=False) overwrite_dict(cluster_dict, new_cluster_dict) try: DBManager.connection_wrapper(cluster_processed_faces) except IncompleteDatabaseOperation: overwrite_dict(cluster_dict, cluster_dict_copy)
def reclassify_worker(con): # all operations in worker, so if any DB operation raises error, it is caught if embeddings_with_ids is not None: local_embeddings_with_ids = embeddings_with_ids else: local_embeddings_with_ids = list(DBManager.get_all_embeddings(with_ids=True)) if not local_embeddings_with_ids: log_error('no embeddings found, nothing to edit') return new_cluster_dict = DBManager.get_certain_clusters() core_algorithm = CoreAlgorithm() clustering_result = core_algorithm.cluster_embeddings(embeddings=local_embeddings_with_ids, existing_clusters_dict=new_cluster_dict, should_reset_cluster_ids=True, final_clusters_only=False) _, modified_clusters_dict, removed_clusters_dict = clustering_result DBManager.overwrite_clusters(new_cluster_dict, removed_clusters_dict, no_new_embs=True, clear_clusters=True, con=con, close_connections=False) overwrite_dict(cluster_dict, new_cluster_dict)
def cluster_processed_faces(con): embeddings_with_ids = list(DBManager.get_all_embeddings(with_ids=True)) # TODO: Call reclassify handler here? # TODO: Clear existing clusters? Issues with ids etc.???? core_algorithm = CoreAlgorithm(metric=metric, classification_threshold=threshold) # passing result cluster dict already overwrites it clustering_result = core_algorithm.cluster_embeddings( embeddings_with_ids, existing_clusters_dict=cluster_dict, should_reset_cluster_ids=True, final_clusters_only=False) _, modified_clusters_dict, removed_clusters_dict = clustering_result DBManager.overwrite_clusters_simplified(modified_clusters_dict, removed_clusters_dict, con=con, close_connections=False) reset_cluster_ids(con=con, close_connections=False) new_cluster_dict = DBManager.load_cluster_dict(con=con, close_connections=False) overwrite_dict(cluster_dict, new_cluster_dict)
def process_images_dir_measure(cluster_dict, n): images_path = DATASET_PATH try: print('------ PROCESSING FACES') process_faces_measure(images_path, n) print('------ DONE PROCESSING') except IncompleteDatabaseOperation as e: print('process_images_dir_measure error') log_error(e) return cluster_dict_copy = cluster_dict.copy() def cluster_processed_faces(con): embeddings_with_ids = list( DBManager.get_all_embeddings(with_ids=True)) # TODO: Call reclassify handler here? # TODO: Clear existing clusters? Issues with ids etc.???? core_algorithm = CoreAlgorithm() # passing result cluster dict already overwrites it clustering_result = core_algorithm.cluster_embeddings( embeddings_with_ids, existing_clusters_dict=cluster_dict, should_reset_cluster_ids=True, final_clusters_only=False) _, modified_clusters_dict, removed_clusters_dict = clustering_result DBManager.overwrite_clusters_simplified(modified_clusters_dict, removed_clusters_dict, con=con, close_connections=False) try: DBManager.connection_wrapper(cluster_processed_faces) except IncompleteDatabaseOperation: overwrite_dict(cluster_dict, cluster_dict_copy)
def clear_data_worker(central_con, local_con): DBManager.clear_local_tables(path_to_local_db, con=local_con, close_connections=False) clear_central_tables(con=central_con, close_connections=False) overwrite_dict(cluster_dict, dict())