Lambang commited on
Commit
4012d7a
·
1 Parent(s): f439788
Files changed (1) hide show
  1. main.py +57 -57
main.py CHANGED
@@ -65,60 +65,60 @@ async def root():
65
  data_processor = DataProcessing()
66
  data_train_pred = TrainPred()
67
 
68
- import random
69
- def preprocessing(filepath):
70
- folder_path = './static/temporary'
71
-
72
- shutil.rmtree(folder_path)
73
- os.mkdir(folder_path)
74
-
75
- # data_processor.detect_landmark(data_processor.face_cropping_pred(filepath))
76
- data_processor.enhance_contrast_histeq(data_processor.face_cropping_pred(filepath))
77
-
78
- files = os.listdir(folder_path)
79
- index = 0
80
- for file_name in files:
81
- file_ext = os.path.splitext(file_name)[1]
82
- new_file_name = str(index) + "_" + str(random.randint(1, 100000)) + file_ext
83
- os.rename(os.path.join(folder_path, file_name), os.path.join(folder_path, new_file_name))
84
- index += 1
85
-
86
- print("Tungu sampai selesaiii")
87
-
88
- train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
89
- test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
90
-
91
- ## -------------------------------------------------------------------------
92
- ## API UNTUK MELAKUKAN PROSES PREDIKSI
93
- ## -------------------------------------------------------------------------
94
-
95
- @app.post('/upload/file',tags=["Predicting"])
96
- async def upload_file(picture: UploadFile):
97
- file_extension = picture.filename.split('.')[-1].lower()
98
-
99
-
100
- if file_extension not in ALLOWED_EXTENSIONS:
101
- raise HTTPException(status_code=400, detail='Invalid file extension')
102
-
103
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
104
- file_path = os.path.join(UPLOAD_FOLDER, secure_filename(picture.filename))
105
- with open(file_path, 'wb') as f:
106
- f.write(picture.file.read())
107
- try:
108
- processed_img = preprocessing(cv2.imread(file_path))
109
- except Exception as e:
110
- os.remove(file_path)
111
- raise HTTPException(status_code=500, detail=f'Error processing image: {str(e)}')
112
-
113
- return JSONResponse(content={'message': 'File successfully uploaded'}, status_code=200)
114
-
115
- @app.get('/get_images', tags=["Predicting"])
116
- def get_images():
117
- folder_path = "./static/temporary"
118
- files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
119
- urls = []
120
- for i in range(0, 3):
121
- url = f'{public_url}/static/temporary/{files[i]}'
122
- urls.append(url)
123
- bentuk, persentase = data_train_pred.prediction(selected_model)
124
- return {'urls': urls, 'bentuk_wajah':bentuk[0], 'persen':persentase}
 
65
  data_processor = DataProcessing()
66
  data_train_pred = TrainPred()
67
 
68
+ # import random
69
+ # def preprocessing(filepath):
70
+ # folder_path = './static/temporary'
71
+
72
+ # shutil.rmtree(folder_path)
73
+ # os.mkdir(folder_path)
74
+
75
+ # # data_processor.detect_landmark(data_processor.face_cropping_pred(filepath))
76
+ # data_processor.enhance_contrast_histeq(data_processor.face_cropping_pred(filepath))
77
+
78
+ # files = os.listdir(folder_path)
79
+ # index = 0
80
+ # for file_name in files:
81
+ # file_ext = os.path.splitext(file_name)[1]
82
+ # new_file_name = str(index) + "_" + str(random.randint(1, 100000)) + file_ext
83
+ # os.rename(os.path.join(folder_path, file_name), os.path.join(folder_path, new_file_name))
84
+ # index += 1
85
+
86
+ # print("Tungu sampai selesaiii")
87
+
88
+ # train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
89
+ # test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.)
90
+
91
+ # ## -------------------------------------------------------------------------
92
+ # ## API UNTUK MELAKUKAN PROSES PREDIKSI
93
+ # ## -------------------------------------------------------------------------
94
+
95
+ # @app.post('/upload/file',tags=["Predicting"])
96
+ # async def upload_file(picture: UploadFile):
97
+ # file_extension = picture.filename.split('.')[-1].lower()
98
+
99
+
100
+ # if file_extension not in ALLOWED_EXTENSIONS:
101
+ # raise HTTPException(status_code=400, detail='Invalid file extension')
102
+
103
+ # os.makedirs(UPLOAD_FOLDER, exist_ok=True)
104
+ # file_path = os.path.join(UPLOAD_FOLDER, secure_filename(picture.filename))
105
+ # with open(file_path, 'wb') as f:
106
+ # f.write(picture.file.read())
107
+ # try:
108
+ # processed_img = preprocessing(cv2.imread(file_path))
109
+ # except Exception as e:
110
+ # os.remove(file_path)
111
+ # raise HTTPException(status_code=500, detail=f'Error processing image: {str(e)}')
112
+
113
+ # return JSONResponse(content={'message': 'File successfully uploaded'}, status_code=200)
114
+
115
+ # @app.get('/get_images', tags=["Predicting"])
116
+ # def get_images():
117
+ # folder_path = "./static/temporary"
118
+ # files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
119
+ # urls = []
120
+ # for i in range(0, 3):
121
+ # url = f'{public_url}/static/temporary/{files[i]}'
122
+ # urls.append(url)
123
+ # bentuk, persentase = data_train_pred.prediction(selected_model)
124
+ # return {'urls': urls, 'bentuk_wajah':bentuk[0], 'persen':persentase}