def getListTestFile(test_stip_folder, test_label_save_file, index_of_actor):
    test_stip_file_list = []
    for i in range(1, number_action + 1):
        folder_path = test_stip_folder + "\\Action_" + str(i)
        temp_list_test_sample = getAllTextFileFromFolder(folder_path)
        list_test_sample = removeTrainActorFiles(temp_list_test_sample,
                                                 actor_name, index_of_actor)
        for j in range(0, len(list_test_sample)):
            temp_file = test_stip_folder + str(i) + "\\" + list_test_sample[j]
            test_label_save_file.write(str(i) + "\n")
            test_stip_file_list.append(temp_file)
        test_label_save_file.close()
    return test_stip_file_list
def getHistogramByUsingRandomForestModel(rf_model_list, stip_samples_cam,
                                         index_of_actor, histogram_cam_folder):
    for i in range(1, number_action + 1):
        rf = rf_model_list[i - 1]
        leaf_list = getForestLeafs(rf_model_list[i - 1], n_estimators)
        data_file_path = histogram_cam_folder + "\\histogram_action_" + str(
            i) + ".txt"
        label_file_path = histogram_cam_folder + "\\labels_" + str(i) + ".txt"
        label_save_file = open(label_file_path, "a")
        label_save_file.truncate()
        save_file = open(data_file_path, "a")
        save_file.truncate()
        for j in range(1, number_action + 1):
            train_folder = stip_samples_cam + "\\Action_" + str(j)
            temp_list_train_sample = getAllTextFileFromFolder(train_folder)
            list_train_sample = removeTestActorFiles(temp_list_train_sample,
                                                     actor_name,
                                                     index_of_actor)

            for k in range(0, len(list_train_sample)):
                stip_file_path = train_folder + '\\' + list_train_sample[k]
                read_file = open(stip_file_path, "r")
                read_file.readline()
                read_file.readline()
                read_file.readline()
                file = read_file.readlines()
                features = np.loadtxt(file, ndmin=2)
                if features.size > 0:
                    X = features[:, 7:]
                    rf_output = rf.apply(X)
                    #print(str(rf_output))
                    histogram = convertRFOutputToHistogram(
                        rf_output, leaf_list, max_depth, n_estimators)
                    #print(str(histogram))
                    for item in histogram:
                        save_file.write("%s\t" % item)
                    save_file.write("\n")

                    if j == i:
                        label_save_file.write("1\n")
                    else:
                        label_save_file.write("0\n")

        save_file.close()
        label_save_file.close()
    return None
def randomDescriptorForTrainingRandomForest(index_of_actor, stip_samples_cam,
                                            rf_train_cam):
    for i in range(1, number_action + 1):
        # positive descriptors
        pos_count = 0
        positive_folder = stip_samples_cam + "\\Action_" + str(i)
        temp_list_positive_file = getAllTextFileFromFolder(positive_folder)
        list_positive_file = removeTestActorFiles(temp_list_positive_file,
                                                  actor_name, index_of_actor)
        rf_train_file_path = rf_train_cam + "\\train_rf_" + str(i) + ".txt"

        save_file = open(rf_train_file_path, "a")
        save_file.truncate()

        rf_label_file_path = rf_train_folder + "labels_" + str(i) + ".txt"
        file_label = open(rf_label_file_path, "a")
        file_label.truncate()

        for j in range(0, len(list_positive_file)):
            stip_file_path = positive_folder + '\\' + list_positive_file[j]
            read_file = open(stip_file_path, "r")
            read_file.readline()
            read_file.readline()
            read_file.readline()
            while True:
                if pos_count < number_descriptor:
                    line = read_file.readline()
                    if line == "":
                        read_file.close()
                        break
                    else:
                        save_file.write(line)
                        pos_count += 1
                        file_label.write("1\n")
                else:
                    break
        # get negative descriptors by random 11 others actions
        list_action_random = []
        k = 1
        while len(list_action_random) < number_action - 1:
            if k != i:
                list_action_random.append(k)
            k += 1

        for j in range(0, len(list_action_random)):
            neg_count = 0
            negative_folder = stip_samples_cam + "\\Action_" + str(
                list_action_random[j])
            temp_list_positive_file = getAllTextFileFromFolder(negative_folder)
            list_negative_file = removeTestActorFiles(temp_list_positive_file,
                                                      actor_name,
                                                      index_of_actor)
            print(len(list_negative_file))
            while neg_count < 5 * (pos_count / (number_action - 1)):
                k = rand.choice(range(0, number_train_sample_per_action))
                stip_file_path = negative_folder + '\\' + list_negative_file[k]
                read_file = open(stip_file_path, "r")
                read_file.readline()
                read_file.readline()
                read_file.readline()
                while True:
                    if neg_count < 5 * (pos_count / (number_action - 1)):
                        line = read_file.readline()
                        if line == "":
                            read_file.close()
                            break
                        else:
                            save_file.write(line)
                            neg_count += 1
                            file_label.write("2\n")
                    else:
                        break
        file_label.close()
        save_file.close()
    return None
label_file_path = "E:\\A-DATA\\histogram-train-svm\\Cam3\\labels_"
test_label_file_path = "E:\\A-DATA\\test-label\\labels.txt"
number_descriptor = 600
number_neg_class = 8
number_train_sample_per_action = 33
number_test_sample_per_action = 3
number_action = 12

###########################################################################
# Random 800 descriptors positive and negative for training Random Forest #
###########################################################################

for i in range(1, number_action + 1):
    # positive descriptors
    pos_count = 0
    list_positive_file = getAllTextFileFromFolder(stip_folder + str(i))
    data_file_path = rf_train_data + str(i) + ".txt"
    save_file = open(data_file_path, "a")
    save_file.truncate()

    for j in range(0, len(list_positive_file)):
        stip_file_path = stip_folder + str(i) + '\\' + list_positive_file[j]
        read_file = open(stip_file_path, "r")
        read_file.readline()
        read_file.readline()
        read_file.readline()
        while True:
            if pos_count < number_descriptor:
                line = read_file.readline()
                if line == "":
                    read_file.close()