def create_group_add_person(groupId, groupName):

    # Create PersonGroup
    result = face_api.createPersonGroup(groupId, groupName, "")
    flag_reuse_person_group = False
    if (result!=''):
        result = eval(result)
        
        if (result["error"]["code"] == "PersonGroupExists"):
            
            del_person_group = raw_input('Delete this PersonGroup? (y/n) ')
    
            if (del_person_group=='y') or (del_person_group=='1'):
                print 'PersonGroup exists, deleting...'
        
                res_del = face_api.deletePersonGroup(groupId)
                print ('Deleting PersonGroup succeeded' if res_del=='' else 'Deleting PersonGroup failed')
                
                result = face_api.createPersonGroup(groupId, groupName, "")
                print ('Re-create PersonGroup succeeded' if res_del=='' else 'Re-create PersonGroup failed')
                
            elif (del_person_group=='n') or (del_person_group=='0'):
                # Get PersonGroup training status
                res      = face_api.getPersonGroupTrainingStatus(groupId)
                res      = res.replace('null','None')
                res_dict = eval(res)
                training_status = res_dict['status']
                if (training_status=='succeeded'):
                    flag_reuse_person_group = True
        elif (result["error"]["code"] == "RateLimitExceeded"):
            print 'RateLimitExceeded, please retry after 30 seconds'
            sys.exit()
    
    if not flag_reuse_person_group:
        # Create person and add person image
        image_paths = [os.path.join(imgPath, f) for f in os.listdir(imgPath)]
        nbr = 0
        
        for image_path in image_paths:
            nom = os.path.split(image_path)[1].split(".")[0]
            if nom not in list_nom:
                # Create a Person in PersonGroup
                personName = nom
                personId   = face_api.createPerson(groupId, personName, "")
    
                list_nom.append(nom)
                list_personId.append(personId)
                nbr += 1
            else:
                personId = list_personId[nbr-1]
    
            # Add image
            face_api.addPersonFace(groupId, personId, None, image_path, None)
            print "Add image...", nom, '\t', image_path
            time.sleep(0.25)
def create_group_add_person(groupId, groupName):

    # Create PersonGroup
    result = face_api.createPersonGroup(groupId, groupName, "")
    flag_reuse_person_group = False
    # if (result!=''):
    if ('error' in result):
        result = eval(result)

        if (result["error"]["code"] == "PersonGroupExists"):

            res_train_status      = face_api.getPersonGroupTrainingStatus(groupId)
            res_train_status      = res_train_status.replace('null','None')
            res_train_status_dict = eval(res_train_status)

            if ('error' not in res_train_status_dict):
                createdDateTime = res_train_status_dict['createdDateTime']
                year, month, day, hour, mi, sec = convert_datetime(createdDateTime)

                structTime = time.localtime()
                dt_now = datetime(*structTime[:6])


                # Compare if the PersonGroup has expired or not (24 hours)
                if (dt_now.year==year):
                    if (dt_now.month==month):
                        if (dt_now.day==day):
                            del_person_group = False
                        elif (dt_now.day-1==day):
                            if (dt_now.hour<hour):
                                del_person_group = False
                            else:
                                del_person_group = True
                        else:
                            del_person_group = True
                    else:
                        del_person_group = True
            else:
                del_person_group = True

            if (del_person_group):
                print 'PersonGroup exists, deleting...'

                res_del = face_api.deletePersonGroup(groupId)
                print ('Deleting PersonGroup succeeded' if res_del=='' else 'Deleting PersonGroup failed')

                result = face_api.createPersonGroup(groupId, groupName, "")
                print ('Re-create PersonGroup succeeded' if res_del=='' else 'Re-create PersonGroup failed')

                flag_reuse_person_group = False

            elif (not del_person_group):
                # Get PersonGroup training status
                training_status = res_train_status_dict['status']
                if (training_status=='succeeded'):
                    flag_reuse_person_group = True

        elif (result["error"]["code"] == "RateLimitExceeded"):
            print 'RateLimitExceeded, please retry after 30 seconds'
            sys.exit()

    if not flag_reuse_person_group:
        # Create person and add person image
        image_paths = [os.path.join(imgPath, f) for f in os.listdir(imgPath)]
        nbr = 0

        for image_path in image_paths:
            nom = os.path.split(image_path)[1].split(".")[0]
            if nom not in list_nom:
                # Create a Person in PersonGroup
                personName = nom
                personId   = face_api.createPerson(groupId, personName, "")

                list_nom.append(nom)
                list_personId.append(personId)
                nbr += 1
            else:
                personId = list_personId[nbr-1]

            # Add image
            face_api.addPersonFace(groupId, personId, None, image_path, None)
            print "Add image...", nom, '\t', image_path
            time.sleep(0.25)
def create_group_add_person(groupId, groupName):

    # Create PersonGroup
    result = face_api.createPersonGroup(groupId, groupName, "")
    flag_reuse_person_group = False

    # if (result!=''):
    if ('error' in result):
        result = eval(result)

        if (result["error"]["code"] == "PersonGroupExists"):

            res_train_status      = face_api.getPersonGroupTrainingStatus(groupId)
            res_train_status      = res_train_status.replace('null','None')
            res_train_status_dict = eval(res_train_status)
            print res_train_status

            if 'error' not in res_train_status_dict:
                createdDateTime = res_train_status_dict['createdDateTime']
                year, month, day, hour, mi, sec = convert_datetime(createdDateTime)

                structTime = time.localtime()
                dt_now = datetime(*structTime[:6])


                # Compare if the PersonGroup has expired or not (24 hours)
                if (dt_now.year==year):
                    if (dt_now.month==month):
                        if (dt_now.day==day):
                            del_person_group = False
                        elif (dt_now.day-1==day):
                            if (dt_now.hour<hour):
                                del_person_group = False
                            else:
                                del_person_group = True
                        else:
                            del_person_group = True
                    else:
                        del_person_group = True
            else:
                del_person_group = True

            if (del_person_group):
                print 'PersonGroup exists, deleting...'

                res_del = face_api.deletePersonGroup(groupId)
                print ('Deleting PersonGroup succeeded' if res_del=='' else 'Deleting PersonGroup failed')

                result = face_api.createPersonGroup(groupId, groupName, "")
                print ('Re-create PersonGroup succeeded' if res_del=='' else 'Re-create PersonGroup failed')

                flag_reuse_person_group = False

            elif (not del_person_group):
                # Get PersonGroup training status
                training_status = res_train_status_dict['status']
                if (training_status=='succeeded'):
                    flag_reuse_person_group = True

        elif (result["error"]["code"] == "RateLimitExceeded"):
            print 'RateLimitExceeded, please retry after 30 seconds'
            sys.exit()



    if not flag_reuse_person_group:
        # Create person and add person image
        image_paths = [os.path.join(imgPath, f) for f in os.listdir(imgPath)]
        nbr = 0

        for image_path in image_paths:
            nom = os.path.split(image_path)[1].split(".")[0]
            if nom not in list_nom:
                # Create a Person in PersonGroup
                personName = nom
                personId   = face_api.createPerson(groupId, personName, "")

                list_nom.append(nom)
                list_personId.append(personId)
                nbr += 1
            else:
                personId = list_personId[nbr-1]

            # Add image
            image_data = get_image_from_github(image_path)
            face_api.addPersonFace(groupId, personId, None, None, image_data)
            print "Add image...", nom, '\t', image_path
            time.sleep(0.25)
xls_filename = 'formation.xls' # Excel file contains Formation information
maxNbOfCandidates = 1 # Maximum number of candidates for the identification

# Natural Language Classifier
natural_language_classifier = NaturalLanguageClassifierV1(
                              username = '******',
                              password = '******')

# Training Phase
groupId     = "group_all"
groupName   = "employeurs"

face_api.deletePersonGroup(groupId)

# Create PersonGroup
result = face_api.createPersonGroup(groupId, groupName, "")
print result
#result = eval(result)

# if result["error"]["code"] == "PersonGroupExists":
#     #TODO: change to get personGroup info than delete it
#     #face_api.deletePersonGroup(groupId)
#     print 'a'

list_nom = []
list_personId = []
nbr = 0

image_paths = [os.path.join(imgPath, f) for f in os.listdir(imgPath)]

for image_path in image_paths: