Example #1
0
def train_person_group(groupId):
    # Train PersonGroup
    face_api.trainPersonGroup(groupId)

    # Get training status
    res      = face_api.getPersonGroupTrainingStatus(groupId)
    res      = res.replace('null','None')
    res_dict = eval(res)
    training_status = res_dict['status']
    print training_status

    while (training_status=='running'):
        time.sleep(0.25)
        res = face_api.getPersonGroupTrainingStatus(groupId)
        res = res.replace('null','None')
        res_dict = eval(res)
        training_status = res_dict['status']
        print training_status

    return training_status
def train_person_group(groupId):

    # Train PersonGroup
    face_api.trainPersonGroup(groupId)

    # Get training status
    res      = face_api.getPersonGroupTrainingStatus(groupId)
    res      = res.replace('null','None')
    res_dict = eval(res)
    training_status = res_dict['status']
    print training_status

    while (training_status=='running'):
        time.sleep(0.25)
        res = face_api.getPersonGroupTrainingStatus(groupId)
        res = res.replace('null','None')
        res_dict = eval(res)
        training_status = res_dict['status']
        print training_status

    return training_status
def retake_validate_photos(clientId, personId, step_time, flag_show_photos, imgPath, name):

    global global_vars
    global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()

    # Ask users if they want to change photo(s) or validate them
    b = validate_photo(clientId)
    image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]

    while (b==0):
        global_var['text3'] = "Veuillez repondre"
        simple_message(clientId, u"Veuillez répondre quelles photos que vous voulez changer ?")

        while (global_var['respFromHTML'] == ""):
            pass
        nb = global_var['respFromHTML']
        global_var['respFromHTML'] = ""

        if ('-' in nb):
            nb2 = ''
            for i in range(int(nb[0]), int(nb[2])+1):
                nb2 = nb2 + str(i)
            nb = nb2

        nb = str_replace_chars(nb, [',',';','.',' '], ['','','',''])

        if (nb!=""):
            str_nb = ""
            for j in range(0, len(nb)):
                if (j==len(nb)-1):
                    str_nb = str_nb + "'" + nb[j] + "'"
                else:
                    str_nb = str_nb + "'" + nb[j] + "', "

            simple_message(clientId, 'Vous souhaitez changer les photos: ' + str_nb + ' ?')

            global_var['text']  = 'Re-prenant photos'
            global_var['text2'] = 'Veuillez patienter... '
            global_var['text3'] = ''

            simple_message(clientId, global_var['text'] + ' ' + global_var['text2'])
            time.sleep(0.25)
            chrome_server2client(clientId, 'START')

        for j in range(0, len(nb)):
            global_var['text3'] = str(j) + ' ont ete prises, reste a prendre : ' + str(len(nb)-j)
            time.sleep(step_time)
            print "Reprendre photo ", nb[j]
            #TODO: add a facedetect here to cut the face from image
            image_path = image_to_paths[int(nb[j])-1]
            os.remove(image_path) # Remove old image
            with open(image_path, 'wb') as f:
                f.write(global_var['binary_data'])
                f.close()

            print "Enregistrer photo " + image_path + ", nb de photos prises : " + nb[j]
        chrome_server2client(clientId, 'DONE')
        time.sleep(0.25)

        a = yes_or_no(clientId, u'Reprise de photos finie, souhaitez-vous réviser vos photos ?', 4)
        if (a==1):
            thread_show_photos2 = Thread(target = show_photos, args = (clientId, imgPath, name), name = 'thread_show_photos2_'+clientId)
            thread_show_photos2.start()

        b = validate_photo(clientId)
        global_var['text']  = ''
        global_var['text2'] = ''
        global_var['text3'] = ''
        if (b==1):
            break
    # End of While(b==0)

    print "Adding faces to person group..."
    image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]
    for image_path in image_to_paths:
        face_api.addPersonFace(groupId, personId, None, image_path, None)

    # Retrain Person Group
    resultTrainPersonGroup = face_api.trainPersonGroup(groupId)
    print "Re-train Person Group: ", resultTrainPersonGroup

    global_var['flag_enable_recog'] = 1  # Re-enable recognition
    global_var['flag_ask'] = 1 # Reset asking
def retake_validate_photos(clientId, personId, step_time, flag_show_photos, imgPath, name):

    global global_vars
    global_var = (item for item in global_vars if item["clientId"] == str(clientId)).next()

    # Ask users if they want to change photo(s) or validate them
    b = validate_photo(clientId)
    image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]

    while (b==0):
        global_var['text3'] = "Veuillez repondre"
        simple_message(clientId, u"Veuillez répondre quelles photos que vous voulez changer ?")

        while (global_var['respFromHTML'] == ""):
            pass
        nb = global_var['respFromHTML']
        global_var['respFromHTML'] = ""

        if ('-' in nb):
            nb2 = ''
            for i in range(int(nb[0]), int(nb[2])+1):
                nb2 = nb2 + str(i)
            nb = nb2
        elif (nb=='*' or nb=='all'):
            nb=''
            for j in range(0, nb_img_max):
                nb = nb+str(j+1)
        elif any(nb[idx] in a for idx in range(0, len(nb))): # If there is any number in string
            nb2 = ''
            for j in range(0, len(nb)):
                if (nb[j] in a):
                    nb2 = nb2 + nb[j]
            nb = nb2
        else:
            print 'Fatal error: invalid response'
            nb = ''

        nb = str_replace_chars(nb, [',',';','.',' '], ['','','',''])

        if (nb!=""):
            str_nb = ""
            for j in range(0, len(nb)):
                if (j==len(nb)-1):
                    str_nb = str_nb + "'" + nb[j] + "'"
                else:
                    str_nb = str_nb + "'" + nb[j] + "', "

            simple_message(clientId, 'Vous souhaitez changer les photos: ' + str_nb + ' ?')

            global_var['text']  = 'Re-prenant photos'
            global_var['text2'] = 'Veuillez patienter... '
            global_var['text3'] = ''

            simple_message(clientId, global_var['text'] + ' ' + global_var['text2'])
            time.sleep(0.25)
            chrome_server2client(clientId, 'START')

        for j in range(0, len(nb)):
            global_var['text3'] = str(j) + ' ont ete prises, reste a prendre : ' + str(len(nb)-j)
            time.sleep(step_time)
            print "Reprendre photo ", nb[j]
            #TODO: add a facedetect here to cut the face from image
            image_path = image_to_paths[int(nb[j])-1]
            # os.remove(image_path) # Remove old image
            delete_image_on_github(image_path)

            # with open(image_path, 'wb') as f:
            #     f.write(global_var['binary_data'])
            #     f.close()
            put_image_to_github(image_path, global_var['binary_data'])

            print "Enregistrer photo " + image_path + ", nb de photos prises : " + nb[j]

        chrome_server2client(clientId, 'DONE')
        time.sleep(0.25)

        a = yes_or_no(clientId, u'Reprise de photos finie, souhaitez-vous réviser vos photos ?', 4)
        if (a==1):
            thread_show_photos2 = Thread(target = show_photos, args = (clientId, imgPath, name), name = 'thread_show_photos2_'+clientId)
            thread_show_photos2.start()

        b = validate_photo(clientId)
        global_var['text']  = ''
        global_var['text2'] = ''
        global_var['text3'] = ''
        if (b==1):
            break
    # End of While(b==0)

    print "Adding faces to person group..."
    image_to_paths = [root_path+imgPath+str(name)+"."+str(j)+suffix for j in range(nb_img_max)]
    for image_path in image_to_paths:
        image_data = get_image_from_github(image_path)
        face_api.addPersonFace(groupId, personId, None, None, image_data)

    # Retrain Person Group
    resultTrainPersonGroup = face_api.trainPersonGroup(groupId)
    print "Re-train Person Group: ", resultTrainPersonGroup

    global_var['flag_enable_recog'] = 1  # Re-enable recognition
    global_var['flag_ask'] = 1 # Reset asking
    if nom not in list_nom:
        # Create a Person in a 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]

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

resultTrainPersonGroup = face_api.trainPersonGroup(groupId)

res      = face_api.getPersonGroupTrainingStatus(groupId)
res      = res.replace('null','None')
res_dict = eval(res)
training_status = res_dict['status']
print training_status


while (training_status=='running'):
    time.sleep(0.25)
    res = face_api.getPersonGroupTrainingStatus(groupId)
    res = res.replace('null','None')
    res_dict = eval(res)
    training_status = res_dict['status']
    print training_status