File size: 5,908 Bytes
364ca9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import random
import pandas as pd
from pathlib import Path
from flask import jsonify

list_class = ['Diamond','Oblong','Oval','Round','Square','Triangle']

# public_url = "https://yamanaka1.pagekite.me"

class GetLoadData:
    @staticmethod
    def get_training_file_counts():
        path = "./static/dataset/Face Shape"
        training_file_counts = []

        # Loop melalui folder Training
        for sub_folder in ["Diamond", "Oblong", "Oval", "Round", "Square", "Triangle"]:
            # Tentukan path ke folder sub_folder dalam folder Training
            sub_path = os.path.join(path, "Training", sub_folder)

            # Gunakan fungsi listdir untuk membaca semua file dalam folder sub_folder
            num_files = len([f for f in os.listdir(sub_path) if os.path.isfile(os.path.join(sub_path, f))])

            # Tambahkan jumlah file ke dalam array training_file_counts
            training_file_counts.append(num_files)

        total_file = sum(training_file_counts)
        training_file_counts.append(total_file)

        # Return hasil dalam bentuk JSON
        return jsonify(training_file_counts)

    @staticmethod
    def get_testing_file_counts():
        path = "./static/dataset/Face Shape"
        testing_file_counts = []

        # Loop melalui folder Testing
        for sub_folder in ["Diamond", "Oblong", "Oval", "Round", "Square", "Triangle"]:
            # Tentukan path ke folder sub_folder dalam folder Testing
            sub_path = os.path.join(path, "Testing", sub_folder)

            # Gunakan fungsi listdir untuk membaca semua file dalam folder sub_folder
            num_files = len([f for f in os.listdir(sub_path) if os.path.isfile(os.path.join(sub_path, f))])

            # Tambahkan jumlah file ke dalam array testing_file_counts
            testing_file_counts.append(num_files)

        total_file = sum(testing_file_counts)
        testing_file_counts.append(total_file)

        # Return hasil dalam bentuk JSON
        return jsonify(testing_file_counts)

    @staticmethod
    def folder_maker(preprocessing_name):
        folder_path = f'./static/dataset/{preprocessing_name}'
        training_path = f'./static/dataset/{preprocessing_name}/Training'
        testing_path = f'./static/dataset/{preprocessing_name}/Testing'

        # Membuat folder dataset/Landmark Face Shape jika belum ada
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

            # Membuat folder dataset/Landmark Face Shape/Training jika belum ada
            if not os.path.exists(training_path):
                os.makedirs(training_path)
                for i in range(0, len(list_class)):
                    os.mkdir(f'{training_path}/{list_class[i]}')

        # Membuat folder dataset/Landmark Face Shape/Testing jika belum ada
        if not os.path.exists(testing_path):
            os.makedirs(testing_path)
            for i in range(0, len(list_class)):
                os.mkdir(f'{testing_path}/{list_class[i]}')

    @staticmethod
    def load_image_data(image_dir):
        # Get file paths for all images in the directory
        jpeg = list(image_dir.glob(r'**/*.jpeg'))
        JPG = list(image_dir.glob(r'**/*.JPG'))
        jpg = list(image_dir.glob(r'**/*.jpg'))
        PNG = list(image_dir.glob(r'**/*.PNG'))
        png = list(image_dir.glob(r'**/*.png'))
        filepaths_ori = jpeg + JPG + jpg + PNG + png

        # Get labels for each image
        labels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1], filepaths_ori))

        # Convert filepaths and labels to Pandas series
        filepaths_ori = pd.Series(filepaths_ori, name='Filepath').astype(str)
        labels = pd.Series(labels, name='Label')

        return filepaths_ori, labels

    @staticmethod
    def get_random_images(tahap, public_url):
        root_path = f'./static/dataset/{tahap}/Training/'
        num_images = 1
        random_images = []
        folder_count = 1

        # Iterasi melalui folder di dalam folder "training"
        for folder_name in os.listdir(root_path):
            folder_path = os.path.join(root_path, folder_name)

            print(folder_path)

            # Jika folder_name bukan folder, skip
            if not os.path.isdir(folder_path):
                continue

            # Mengambil daftar file di dalam folder dan mengacaknya
            file_names = os.listdir(folder_path)
            random.shuffle(file_names)

            # Memilih 1 file pertama setelah diacak
            for i in range(len(file_names)):
                if i < num_images:
                    url = f'{public_url}/static/dataset/{tahap}/Training/{folder_name}'
                    print(url)
                    random_images.append(os.path.join(url, file_names[i]))
                    print(random_images)

            # Hentikan loop setelah mengambil 5 gambar dari folder ke-5
            if folder_count == 5:
                break

            folder_count += 1

        # Mengirimkan daftar file acak sebagai respons ke Flutter
        print(random_images)
        return random_images

    @staticmethod
    def load_image_dataset(train_dataset_path, test_dataset_path):
        list_data_path = [train_dataset_path, test_dataset_path]

        # Get filepaths and labels
        image_dir_train = Path(list_data_path[0])
        filepaths_train, labels_train = GetLoadData.load_image_data(image_dir_train)

        # Concatenate filepaths and labels
        train_image_df = pd.concat([filepaths_train, labels_train], axis=1)

        # Get filepaths and labels
        image_dir_test = Path(list_data_path[1])
        filepaths_test, labels_test = GetLoadData.load_image_data(image_dir_test)

        # Concatenate filepaths and labels
        test_image_df = pd.concat([filepaths_test, labels_test], axis=1)

        # Return filepaths and labels
        return train_image_df, test_image_df