Lambang commited on
Commit
bb9d3db
1 Parent(s): 1c26982
Files changed (1) hide show
  1. main.py +233 -43
main.py CHANGED
@@ -1,9 +1,7 @@
1
- from fastapi import FastAPI
2
- import pickle
3
- import uvicorn
4
- import pandas as pd
5
  import shutil
6
  import cv2
 
 
7
  import tensorflow as tf
8
  import os
9
  from flask import Flask, jsonify, request, flash, redirect, url_for
@@ -11,12 +9,9 @@ from pyngrok import ngrok
11
  from fastapi import FastAPI, HTTPException, File, UploadFile, Request
12
  from fastapi.staticfiles import StaticFiles
13
  from fastapi.responses import JSONResponse
14
-
15
-
16
- from file_processing import FileProcess
17
- from get_load_data import GetLoadData
18
- from data_preprocess import DataProcessing
19
- from train_pred import TrainPred
20
 
21
  app = FastAPI()
22
  public_url = "https://lambang0902-test-space.hf.space"
@@ -26,37 +21,38 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
26
  filepath = ""
27
  list_class = ['Diamond','Oblong','Oval','Round','Square','Triangle']
28
  list_folder = ['Training', 'Testing']
 
 
29
  face_crop_img = True
30
  face_landmark_img = True
31
  landmark_extraction_img = True
32
- # -----------------------------------------------------
33
 
34
- # -----------------------------------------------------
35
  # Tempat deklarasi model dan sejenisnya
36
  selected_model = tf.keras.models.load_model(f'models/fc_model_1.h5', compile=False)
37
- # face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
38
- # mp_drawing = mp.solutions.drawing_utils
39
- # mp_face_mesh = mp.solutions.face_mesh
40
- # drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
41
- # -----------------------------------------------------
42
 
43
 
44
- # -----------------------------------------------------
45
  # Tempat setting server
46
  UPLOAD_FOLDER = './upload'
47
  UPLOAD_MODEL = './models'
48
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg','zip','h5'}
49
- # -----------------------------------------------------
50
- #Endpoints
51
- #Root endpoints
52
- @app.get("/")
53
- async def root():
54
- # Dapatkan URL publik dari ngrok
55
- ngrok_url = "Tidak Ada URL Publik (ngrok belum selesai memulai)"
56
-
57
- return {"message": "Hello, World!", "ngrok_url": ngrok_url}
58
-
59
 
 
 
 
 
 
60
  #-----------------------------------------------------
61
 
62
  data_processor = DataProcessing()
@@ -69,7 +65,6 @@ def preprocessing(filepath):
69
  shutil.rmtree(folder_path)
70
  os.mkdir(folder_path)
71
 
72
-
73
  data_processor.detect_landmark(data_processor.face_cropping_pred(filepath))
74
  # data_processor.enhance_contrast_histeq(data_processor.face_cropping_pred(filepath))
75
 
@@ -81,14 +76,40 @@ def preprocessing(filepath):
81
  os.rename(os.path.join(folder_path, file_name), os.path.join(folder_path, new_file_name))
82
  index += 1
83
 
84
- # print("Tungu sampai selesaiii")
85
 
86
- # train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
87
- # test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
 
 
 
 
 
 
 
 
 
 
88
 
89
- # ## -------------------------------------------------------------------------
90
- # ## API UNTUK MELAKUKAN PROSES PREDIKSI
91
- # ## -------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  @app.post('/upload/file',tags=["Predicting"])
94
  async def upload_file(picture: UploadFile):
@@ -99,14 +120,12 @@ async def upload_file(picture: UploadFile):
99
  raise HTTPException(status_code=400, detail='Invalid file extension')
100
 
101
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
102
- file_path = os.path.join(UPLOAD_FOLDER, (picture.filename))
103
-
104
-
105
  with open(file_path, 'wb') as f:
106
  f.write(picture.file.read())
107
  try:
108
- cv2.imwrite("./static/test_upload.jpg", cv2.imread(file_path))
109
- processed_img = preprocessing(cv2.imread(file_path))
110
  except Exception as e:
111
  os.remove(file_path)
112
  raise HTTPException(status_code=500, detail=f'Error processing image: {str(e)}')
@@ -118,14 +137,185 @@ def get_images():
118
  folder_path = "./static/temporary"
119
  files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
120
  urls = []
 
 
121
  for i in range(0, 3):
122
  url = f'{public_url}/static/temporary/{files[i]}'
123
  urls.append(url)
124
- bentuk, persentase = data_train_pred.prediction(selected_model)
125
- return {'urls': urls, 'bentuk_wajah':bentuk[0], 'persen':persentase}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  if __name__ == '__main__':
128
  import uvicorn
129
  public_url = ngrok.connect(8080).public_url
130
  print(f' * Running on {public_url}')
131
- uvicorn.run(app, host="0.0.0.0", port=8080)
 
 
 
 
 
 
1
  import shutil
2
  import cv2
3
+ import mediapipe as mp
4
+ from werkzeug.utils import secure_filename
5
  import tensorflow as tf
6
  import os
7
  from flask import Flask, jsonify, request, flash, redirect, url_for
 
9
  from fastapi import FastAPI, HTTPException, File, UploadFile, Request
10
  from fastapi.staticfiles import StaticFiles
11
  from fastapi.responses import JSONResponse
12
+ from pydantic import BaseModel
13
+ import subprocess
14
+ from hairstyle_recommendation import HairstyleRecommendation
 
 
 
15
 
16
  app = FastAPI()
17
  public_url = "https://lambang0902-test-space.hf.space"
 
21
  filepath = ""
22
  list_class = ['Diamond','Oblong','Oval','Round','Square','Triangle']
23
  list_folder = ['Training', 'Testing']
24
+ recommendation = HairstyleRecommendation()
25
+
26
  face_crop_img = True
27
  face_landmark_img = True
28
  landmark_extraction_img = True
29
+ #-----------------------------------------------------
30
 
31
+ #-----------------------------------------------------
32
  # Tempat deklarasi model dan sejenisnya
33
  selected_model = tf.keras.models.load_model(f'models/fc_model_1.h5', compile=False)
34
+ face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
35
+ mp_drawing = mp.solutions.drawing_utils
36
+ mp_face_mesh = mp.solutions.face_mesh
37
+ drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
38
+ # #-----------------------------------------------------
39
 
40
 
41
+ # #-----------------------------------------------------
42
  # Tempat setting server
43
  UPLOAD_FOLDER = './upload'
44
  UPLOAD_MODEL = './models'
45
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg','zip','h5'}
46
+ # app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
47
+ # app.config['UPLOAD_MODEL'] = UPLOAD_MODEL
48
+ # app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024 # 500 MB
49
+ # #-----------------------------------------------------
 
 
 
 
 
 
50
 
51
+ #
52
+ from file_processing import FileProcess
53
+ from get_load_data import GetLoadData
54
+ from data_preprocess import DataProcessing
55
+ from train_pred import TrainPred
56
  #-----------------------------------------------------
57
 
58
  data_processor = DataProcessing()
 
65
  shutil.rmtree(folder_path)
66
  os.mkdir(folder_path)
67
 
 
68
  data_processor.detect_landmark(data_processor.face_cropping_pred(filepath))
69
  # data_processor.enhance_contrast_histeq(data_processor.face_cropping_pred(filepath))
70
 
 
76
  os.rename(os.path.join(folder_path, file_name), os.path.join(folder_path, new_file_name))
77
  index += 1
78
 
79
+ print("Tungu sampai selesaiii")
80
 
81
+ train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
82
+ test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
83
+
84
+ #-----------------------------------------------------
85
+ # Fungsi untuk menjalankan ngrok
86
+ def run_ngrok():
87
+ try:
88
+ # Jalankan ngrok dan simpan prosesnya
89
+ ngrok_process = subprocess.Popen(['ngrok', 'http', '8000'])
90
+ return ngrok_process
91
+ except Exception as e:
92
+ print(f"Error running ngrok: {e}")
93
 
94
+
95
+ @app.get("/")
96
+ async def root():
97
+ # Dapatkan URL publik dari ngrok
98
+ ngrok_url = "Tidak Ada URL Publik (ngrok belum selesai memulai)"
99
+ try:
100
+ ngrok_url = subprocess.check_output(['ngrok', 'http', '8000']).decode('utf-8').strip()
101
+ except Exception as e:
102
+ print(f"Error getting ngrok URL: {e}")
103
+
104
+ return {"message": "Hello, World!", "ngrok_url": ngrok_url}
105
+
106
+ # -------------------------------------------------------------------------
107
+ # API UNTUK MELAKUKAN PROSES PREDIKSI
108
+ # -------------------------------------------------------------------------
109
+ # Use a pipeline as a high-level helper
110
+ from transformers import pipeline
111
+
112
+ pipe = pipeline("image-classification", model="rizvandwiki/gender-classification-2")
113
 
114
  @app.post('/upload/file',tags=["Predicting"])
115
  async def upload_file(picture: UploadFile):
 
120
  raise HTTPException(status_code=400, detail='Invalid file extension')
121
 
122
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
123
+ file_path = os.path.join(UPLOAD_FOLDER, secure_filename(picture.filename))
 
 
124
  with open(file_path, 'wb') as f:
125
  f.write(picture.file.read())
126
  try:
127
+ preprocessing(cv2.imread(file_path))
128
+
129
  except Exception as e:
130
  os.remove(file_path)
131
  raise HTTPException(status_code=500, detail=f'Error processing image: {str(e)}')
 
137
  folder_path = "./static/temporary"
138
  files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
139
  urls = []
140
+
141
+ # Image preprocessed url
142
  for i in range(0, 3):
143
  url = f'{public_url}/static/temporary/{files[i]}'
144
  urls.append(url)
145
+
146
+ # Face shape classification
147
+ bentuk, persentase = data_train_pred.prediction(selected_model)
148
+
149
+ # Gender classification
150
+ gender_classify = pipe('./static/result_upload0.jpg')
151
+ output_gender = max(gender_classify, key=lambda x: x['score'])['label']
152
+
153
+
154
+ # Hairstyle recommendation
155
+ recommended_styles, style_images = recommendation.get_recommendation(output_gender, bentuk[0])
156
+ hairstyleImage = [f'{public_url}/static/hairstyle_image/{file}' for file in style_images]
157
+
158
+ response = {'urls': urls,
159
+ 'bentuk_wajah': bentuk[0],
160
+ 'persen': persentase,
161
+ 'gender': output_gender,
162
+ 'hair_style': recommended_styles,
163
+ 'hair_image': hairstyleImage}
164
+ return response
165
+
166
+
167
+ # -------------------------------------------------------------------------
168
+ # API UNTUK MELAKUKAN PROSES TRAINING
169
+ # -------------------------------------------------------------------------
170
+
171
+ # Model pydantic untuk validasi body
172
+ class TrainingParams(BaseModel):
173
+ optimizer: str
174
+ epoch: int
175
+ batchSize: int
176
+
177
+ @app.post('/upload/dataset', tags=["Training"])
178
+ async def upload_data(dataset: UploadFile):
179
+ if dataset.filename == '':
180
+ raise HTTPException(status_code=400, detail='No file selected for uploading')
181
+
182
+ # Buat path lengkap untuk menyimpan file
183
+ file_path = os.path.join(UPLOAD_FOLDER, dataset.filename)
184
+
185
+ # Simpan file ke folder yang ditentukan
186
+ with open(file_path, "wb") as file_object:
187
+ file_object.write(dataset.file.read())
188
+
189
+ # Panggil fungsi untuk mengekstrak file jika perlu
190
+ FileProcess.extract_zip(file_path)
191
+
192
+ return {'message': 'File successfully uploaded'}
193
+
194
+ @app.post('/set_params', tags=["Training"])
195
+ async def set_params(request: Request, params: TrainingParams):
196
+ global optimizer, epoch, batch_size
197
+
198
+ optimizer = params.optimizer
199
+ epoch = params.epoch
200
+ batch_size = params.batchSize
201
+
202
+ response = {'message': 'Set parameter sukses'}
203
+ return response
204
+
205
+ @app.get('/get_info_data', tags=["Training"])
206
+ def get_info_prepro():
207
+ global optimizer, epoch, batch_size
208
+ training_counts = GetLoadData.get_training_file_counts().json
209
+ testing_counts = GetLoadData.get_testing_file_counts().json
210
+ response = {
211
+ "optimizer": optimizer,
212
+ "epoch": epoch,
213
+ "batch_size": batch_size,
214
+ "training_counts": training_counts,
215
+ "testing_counts": testing_counts
216
+ }
217
+ return response
218
+
219
+ @app.get('/get_images_preprocess', tags=["Training"])
220
+ def get_random_images_crop():
221
+ images_face_landmark = GetLoadData.get_random_images(tahap="Face Landmark",public_url=public_url)
222
+ images_face_extraction = GetLoadData.get_random_images(tahap="landmark Extraction", public_url=public_url)
223
+
224
+ response = {
225
+ "face_landmark": images_face_landmark,
226
+ "landmark_extraction": images_face_extraction
227
+ }
228
+ return response
229
+
230
+ @app.get('/do_preprocessing', tags=["Training"])
231
+ async def do_preprocessing():
232
+ try:
233
+ data_train_pred.do_pre1(test="")
234
+ data_train_pred.do_pre2(test="")
235
+ return {'message': 'Preprocessing sukses'}
236
+ except Exception as e:
237
+ # Tangani kesalahan dan kembalikan respons kesalahan
238
+ error_message = f'Error during preprocessing: {str(e)}'
239
+ raise HTTPException(status_code=500, detail=error_message)
240
+
241
+ @app.get('/do_training', tags=["Training"])
242
+ def do_training():
243
+ global epoch
244
+ folder = ""
245
+ if (face_landmark_img == True and landmark_extraction_img == True):
246
+ folder = "Landmark Extraction"
247
+ elif (face_landmark_img == True and landmark_extraction_img == False):
248
+ folder = "Face Landmark"
249
+ # --------------------------------------------------------------
250
+ train_dataset_path = f"./static/dataset/{folder}/Training/"
251
+ test_dataset_path = f"./static/dataset/{folder}/Testing/"
252
+
253
+ train_image_df, test_image_df = GetLoadData.load_image_dataset(train_dataset_path, test_dataset_path)
254
+
255
+ train_gen, test_gen = data_train_pred.data_configuration(train_image_df, test_image_df)
256
+ model = data_train_pred.model_architecture()
257
+
258
+ result = data_train_pred.train_model(model, train_gen, test_gen, epoch)
259
+
260
+ # Mengambil nilai akurasi training dan validation dari objek result
261
+ train_acc = result.history['accuracy'][-1]
262
+ val_acc = result.history['val_accuracy'][-1]
263
+
264
+ # Plot accuracy
265
+ data_train_pred.plot_accuracy(result=result, epoch=epoch)
266
+ acc_url = f'{public_url}/static/accuracy_plot.png'
267
+
268
+ # Plot loss
269
+ data_train_pred.plot_loss(result=result, epoch=epoch)
270
+ loss_url = f'{public_url}/static/loss_plot.png'
271
+
272
+ # Confusion Matrix
273
+ data_train_pred.plot_confusion_matrix(model, test_gen)
274
+ conf_url = f'{public_url}/static/confusion_matrix.png'
275
+
276
+ return jsonify({'train_acc': train_acc, 'val_acc': val_acc, 'plot_acc': acc_url, 'plot_loss':loss_url,'conf':conf_url})
277
+
278
+
279
+ # -------------------------------------------------------------------------
280
+ # API UNTUK PEMILIHAN MODEL
281
+ # -------------------------------------------------------------------------
282
+
283
+
284
+ @app.post('/upload/model', tags=["Model"])
285
+ def upload_model():
286
+ if 'file' not in request.files:
287
+ return {'message': 'No file part in the request'}, 400
288
+
289
+ file = request.files['file']
290
+
291
+ if file.filename == '':
292
+ return {'message': 'No file selected for uploading'}, 400
293
+
294
+ if file and FileProcess.allowed_file(file.filename):
295
+ filename = secure_filename(file.filename)
296
+ filepath = os.path.join(app.config['UPLOAD_MODEL'], filename)
297
+ file.save(filepath)
298
+
299
+ return {'message': 'File successfully uploaded'}
300
+
301
+ return {'message': 'File failed to uploaded'}
302
+
303
+ @app.post('/selected_models')
304
+ def select_models(index: int):
305
+ global selected_model
306
+ try:
307
+ global selected_model
308
+ selected_model = tf.keras.models.load_model(f'models/fc_model_{index}.h5')
309
+
310
+ # Lakukan sesuatu dengan indeks yang diterima
311
+
312
+ return {'message': 'Request berhasil diterima'}
313
+ except Exception as e:
314
+ raise HTTPException(status_code=500, detail=f'Error: {str(e)}')
315
 
316
  if __name__ == '__main__':
317
  import uvicorn
318
  public_url = ngrok.connect(8080).public_url
319
  print(f' * Running on {public_url}')
320
+ uvicorn.run(app, host="0.0.0.0", port=8080)
321
+ # app = FastAPI()