def make_poses_from_poses_left(pose):
    eucl_dis_tresh_torso = 0.075  #0.065  of 0.11 ??
    rotation_tresh_torso = 16.801
    eucl_dis_tresh_legs = 0.046
    rotation_tresh_legs = 14.588
    eucld_dis_shoulders_tresh = 0.068

    os.system("mkdir -p " + path + pose)
    os.system("mkdir -p " + path + pose + "/json")
    os.system("mkdir -p " + path + pose + "/jsonfout")
    os.system("mkdir -p " + path + pose + "/fotos")
    os.system("mkdir -p " + path + pose + "/fotosfout")
    model = path + pose + "/json/0.json"
    model_features = parse_openpose_json.parse_JSON_single_person(model)
    count = 0
    for json in glob.iglob(path + "pose0left/json/*"):
        input_features = parse_openpose_json.parse_JSON_single_person(json)
        (result, error_score,
         input_transform) = pose_match.single_person_treshholds(
             model_features, input_features, eucl_dis_tresh_torso,
             rotation_tresh_torso, eucl_dis_tresh_legs, rotation_tresh_legs,
             eucld_dis_shoulders_tresh, True)
        if result == True:
            os.system("mv " + json + " " + path + pose + "/json/")
            foto = json.split(".")[0]
            foto = foto.replace("json", "fotos")
            foto = foto + ".png"
            os.system("mv " + foto + " " + path + pose + "/fotos/")
            count = count + 1
    print("there are " + str(count) + " matches found")
def find_treshholds(pose):
    #THRESHOLDS
    eucl_dis_tresh_torso = 0.65  #0.065  of 0.11 ??
    rotation_tresh_torso = 1000
    eucl_dis_tresh_legs = 1000
    rotation_tresh_legs = 400
    eucld_dis_shoulders_tresh = 100
    angle = 0
    model = path + pose + "/json/0.json"
    model_features = parse_openpose_json.parse_JSON_single_person(model)
    replace_json_files(pose)
    # print "angle"
    for json in glob.iglob(path + pose + "/json/*"):
        input_features = parse_openpose_json.parse_JSON_single_person(json)
        primary_angles = calcAngle.prepareangles(model_features)
        secondary_angles = calcAngle.prepareangles(input_features)
        result = calcAngle.find_treshhold(primary_angles, secondary_angles,
                                          angle)
        while (result == False):
            angle = angle + 0.001
            result = calcAngle.find_treshhold(primary_angles, secondary_angles,
                                              angle)
            if result == True:
                print(json)
    '''
def check_pose_data(pose):
    model = path + pose + "/json/1.json"
    model_features = parse_openpose_json.parse_JSON_single_person(model)
    count = 0
    for json in glob.iglob(path + pose + "/json/*"):
        input_features = parse_openpose_json.parse_JSON_single_person(json)
        (result, error_score,
         input_transform) = pose_match.single_person(model_features,
                                                     input_features, True)
        if result == False:
            count = count + 1
    return count
def sort_pose_from_data(model):
    model_features = parse_openpose_json.parse_JSON_single_person(model)
    number = 0
    for json in glob.iglob(data + "set*/a/json/*"):
        input_features = parse_openpose_json.parse_JSON_single_person(json)
        (result, error_score,
         input_transform) = pose_match.single_person(model_features,
                                                     input_features, True)
        if result == True:
            number = number + 1

            os.system("cp " + json + " " + matched + "/json")
            foto = json.split("_")[0]
            foto = foto.replace("json", "image")
            foto = foto + ".png"
            os.system("cp " + foto + " " + matched + "/image")
def check_pose_score(pose):
    count = check_pose_data(pose)
    model = path + pose + "/json/1.json"
    model_features = parse_openpose_json.parse_JSON_single_person(model)
    number = 0
    for json in glob.iglob(path + "*/json/*"):
        if json.split("/json/")[0].split("/poses/")[1] != pose:
            input_features = parse_openpose_json.parse_JSON_single_person(json)
            (result, error_score, input_transform) = pose_match.single_person(
                model_features, input_features, True)
            if result == True:
                number = number + 1

                os.system("cp " + json + " " + matched + "/json")
                foto = json.split(".json")[0]
                foto = foto.replace("json", "fotos")
                foto = foto + ".png"
                os.system("cp " + foto + " " + matched + "/image")
def reorder_json_fout():
    moved = 0
    for i in range(1, 58):
        pose = "pose" + str(i)
        for json in glob.iglob(path + pose + "/jsonfout/*"):
            input_features = parse_openpose_json.parse_JSON_single_person(json)
            for x in range(1, 58):
                pose2 = "pose" + str(x)
                model = path + pose2 + "/json/1.json"
                model_features = parse_openpose_json.parse_JSON_single_person(
                    model)
                (result, error_score,
                 input_transform) = pose_match.single_person(
                     model_features, input_features, True)
                if result == True:
                    os.system("mv " + json + " " + path + pose + "/json/")
                    foto = json.split(".")[0]
                    foto = foto.replace("json", "fotos")
                    foto = foto + ".png"
                    os.system("mv " + foto + " " + path + pose + "/fotos/")
                    moved = moved + 1
    print("finished and moved : " + str(moved))
def clean_dataset():
    #THRESHOLDS

    for i in range(1, 9):
        pose = "pose" + str(i)
        os.system("mkdir -p " + path + pose + "/fotosfout")
        os.system("mkdir -p " + path + pose + "/jsonfout")
        model = path + pose + "/json/0.json"
        model_features = parse_openpose_json.parse_JSON_single_person(model)
        count = 0
        for json in glob.iglob(path + pose + "/json/*"):
            count = 0
            input_features = parse_openpose_json.parse_JSON_single_person(json)
            (result, error_score, input_transform) = pose_match.single_person(
                model_features, input_features, True)
            if result == False:
                os.system("mv " + json + " " + path + pose + "/jsonfout/")
                foto = json.split(".")[0]
                foto = foto.replace("json", "fotos")
                foto = foto + ".png"
                os.system("mv " + foto + " " + path + pose + "/fotosfout/")
                count = count + 1
json_data_path = 'data/json_data/'
images_data_path = 'data/image_data/'

model = "trap7"
input = "trap9"

model = "foto1"
input = "kleuter8"
model_json = json_data_path + model + '.json'
input_json = json_data_path + input + '_keypoints.json'

model_image = images_data_path + model + '.jpg'
input_image = images_data_path + input + '.jpg'

model_features = parse_openpose_json.parse_JSON_single_person(model_json)
input_features = parse_openpose_json.parse_JSON_single_person(input_json)
'''
Calculate match fo real (incl. normalizing)
'''
#TODO: edit return tuple !!
match_result = pose_match.single_person_zonder_split(model_features,
                                                     input_features, True)
logger.info("--Match or not: %s    score= %f", str(match_result.match_bool),
            match_result.error_score)
'''
Calculate match + plot the whole thing
'''
# Reload features bc model_features is a immutable type  -> niet meer nodig want er wordt een copy gemaalt in single_psoe()
# and is changed in single_pose in case of undetected bodyparts
#model_features = parse_openpose_json.parse_JSON_single_person(model_json)
def statistics():

    for i in range(1, 7):
        affine_right = 0
        affine_rightmissed = 0
        affine_wrong = 0
        angle_right = 0
        angle_rightmissed = 0
        angle_wrong = 0
        affinev2_right = 0
        affinev2_rightmissed = 0
        affinev2_wrong = 0
        total_jsonfout = 0
        pose = "pose" + str(i)
        model = path + pose + "/json/0.json"
        model_features = parse_openpose_json.parse_JSON_single_person(model)
        for json in glob.iglob(path + pose + "/json/*"):
            input_features = parse_openpose_json.parse_JSON_single_person(json)
            (result, error_score, input_transform) = pose_match.single_person(
                model_features, input_features, True)
            if result == True:
                affine_right += 1
            else:
                affine_rightmissed += 1

            (result, error_score,
             input_transform) = pose_match.single_person_v2(
                 model_features, input_features, True)
            if result == True:
                affinev2_right += 1
            else:
                affinev2_rightmissed += 1

            primary_angles = calcAngle.prepareangles(model_features)
            secondary_angles = calcAngle.prepareangles(input_features)
            result = calcAngle.succes(primary_angles, secondary_angles)
            if result == True:
                angle_right += 1
            else:
                angle_rightmissed += 1

        for json in glob.iglob(path + pose + "/jsonfout/*"):
            input_features = parse_openpose_json.parse_JSON_single_person(json)
            (result, error_score, input_transform) = pose_match.single_person(
                model_features, input_features, True)
            if result == True:
                affine_wrong += 1

            (result, error_score,
             input_transform) = pose_match.single_person_v2(
                 model_features, input_features, True)
            if result == True:
                affinev2_wrong += 1

            primary_angles = calcAngle.prepareangles(model_features)
            secondary_angles = calcAngle.prepareangles(input_features)
            result = calcAngle.succes(primary_angles, secondary_angles)
            if result == True:
                angle_wrong += 1

            total_jsonfout += 1

        print(
            "******************************************************************************"
        )