def __build_model(auth_params, func_params): """ This script has been defined in order to build (or update) a face recognition model for a specific person based on a set of instances previously extracted and saved. The object representation of the model must already exist. """ token = auth_params.get('token', '1234') model_id = func_params.get('id', 0) # retrieve the entity model object if exists model = get_model(model_id, token) if model is None: raise Exception('The provided model id is not valid!') # retrieve all instances associated to the model instances = get_instances_by_model(model_id, token)['results'] inst_paths = [] for inst in instances: inst_paths.append(os.path.join(get_media_root(), inst['features'])) fm = FaceModels() model_file_path = fm.create_model_from_image_list(aligned_faces_list) tsm.set_model_file(model_id, model_file_path, token=token)
def delete_face_model(auth_dict, param_dict): """ Function used to delete face model :param auth_dict: Input parameters provided by the trigger Action :param param_dict: Output parameters returned by the trigger Action """ model_id = param_dict['id'] fm = FaceModels() fm.delete_model(model_id) tsm.delete_model(model_id, auth_params['token']) return ok
def __recognize_instance(auth_params, func_params): """ This script has been defined in order to recognize face instances that are saved into the database according to existent models """ try: token = auth_params.get('token', '1234') instance_id = func_params["id"] instance_path = os.path.join( get_media_root(), func_params["features"]) # Get available models model_type = 'video' models = tsm.get_models(model_type, auth_params['token']) # Create dictionary with models models_list = [] for model in models: model_id = model['id'] model_file = os.path.join( get_media_root(), model['model_file']) model_dict = {c.MODEL_ID_KEY: model_id, c.MODEL_FILE_KEY: model_file, } models_list.append(model_dict) fm = FaceModels(models_list) # Recognize given instance face = cv2.imread(instance_path, cv2.IMREAD_GRAYSCALE) (model_id, conf) = fm.recognize_face(face) # update the instance reference if recognized if model_id != c.UNDEFINED_LABEL: edit_instance(instance_id, model_id=label, token=token) return 'Instance ' + instance_id + ' associated to model ' + model_id return 'Instance ' + instance_id + ' not recognized by any model' # TODO modificare i dynamic tag per associare automaticamente la persona ? except Exception as e: print e return 'Error on instance recognition'
def update_face_model(auth_dict, param_dict): """ Function used to update global face models used as training set for people recognition. :param auth_dict: Input parameters provided by the trigger Action :param param_dict: Output parameters returned by the trigger Action """ # Get instances associated to model model_id = param_dict['id'] instances = tsm.get_instances_by_model(model_id, auth_params['token']) # Get aligned faces from instances aligned_faces_list = [] for instance in instances: aligned_face_path = instance['features'] aligned_faces_list.append(aligned_face_path) fm = FaceModels() model_file_path = fm.create_model_from_image_list(aligned_faces_list) tsm.set_model_file(model_id, model_file_path, auth_params['token'])