-
Notifications
You must be signed in to change notification settings - Fork 0
/
skinApp.py
210 lines (179 loc) · 6.63 KB
/
skinApp.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from flask import Flask,render_template,request,jsonify
from flask_cors import CORS
import os
from werkzeug.utils import secure_filename
import torch
from torchvision import transforms
from torch.autograd import Variable
import torch.nn.functional as F
from modelResnet18 import *
from classNames import className
import pandas as pd
from PIL import Image
import numpy as np
import shutil
from urllib.parse import urlparse
app = Flask(__name__, static_url_path='/static')
cors = CORS(app, resources={r"*": {"origins": "*"}})
##### Model Resnet18 ####
path = "skin-90/"
face_classes = className(path)
# Check if GPU is available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
num_classes = 14
model = ResNet(num_classes)
##### End Model Resnet18 ####
## Default Data ##
faceClasses = 'Normal / Not Detect'
diagnosis = '(none)'
akurasi = '(none)'
gambar_prediksi = '(none)'
data_recomm = '(none)'
skinntone = '(none)'
## End Default Data ##
app.config['UPLOAD_PATH'] = '/static/images/results/'
# Home page
@app.route("/")
def home():
member= [
{
"name": "Clara Adriana",
"position":"Co-Detection",
"Image": "Clara Adriana",
"github":"https://github.com/claraa24",
"linkedin":"https://www.linkedin.com/in/claraadrianasidauruk"
},
{
"name": "Joel Binsar",
"position":"Team-Leader",
"Image": "Joel Binsar",
"github":"https://github.com/bijoaja",
"linkedin":"https://www.linkedin.com/in/joelbinsar"
},
{
"name": "Pujaningsih",
"position":"Co-Detection",
"Image": "Pujaningsih",
"github":"https://github.com/Pujaningsih39",
"linkedin":"https://www.linkedin.com/in/puja-ningsih-088781268"
},
{
"name": "Putri Wulandari",
"position":"Co-Recommendations",
"Image": "Putri",
"github":"https://github.com/putriwulan05",
"linkedin":"https://www.linkedin.com/in/putri-wulandari-148a1b23b"
},
{
"name": "Wisnu Wijaya",
"position":"Front-End",
"Image": "Wisnu",
"github":"https://github.com/Wisnuoke34",
"linkedin":"https://www.linkedin.com/in/wisnuwiz"
},
{
"name": "Rita Dwi Pangesti",
"position":"Co-Recommendations",
"Image": "Rita",
"github":"https://github.com/ritapangesti",
"linkedin":"https://www.linkedin.com/in/ritadwipangesti"
},
]
default_recom = medication("Normal / Not Detect")
response = {
"member": member,
"recom": default_recom
}
return jsonify(response)
# return render_template("index.html", memberData=member, default_recom=default_recom)
# Proses image
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
# TODO: Process a PIL image for use in a PyTorch model
# tensor.numpy().transpose(1, 2, 0)
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
image = preprocess(image)
return image
# predict product recommendation
def predict2(uploaded_file_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
# TODO: Implement the code to predict the class from an image file
img = Image.open(uploaded_file_path)
img.convert("RGB")
img = process_image(img)
# Convert 2D image to 1D vector
img = np.expand_dims(img, 0)
img = torch.from_numpy(img)
model.eval()
inputs = Variable(img).to(device)
logits = model.forward(inputs)
ps = F.softmax(logits,dim=1)
topk = ps.cpu().topk(topk)
return (e.data.numpy().squeeze().tolist() for e in topk)
# For Read Data Medication
def medication(skin_condition):
df_pd = pd.read_excel("Rekomendasi Obat Penyakit pada Wajah.xlsx", engine="openpyxl")
data_recomm = df_pd[df_pd["Skin Condition"].apply(lambda x: x.lower()) == skin_condition.lower()]
data_recomm["Medication"] = data_recomm["Medication"].apply(lambda x: x.split('\n'))
data_recomm["Skincare Ingredients"] = data_recomm["Skincare Ingredients"].apply(lambda x: x.split('\n'))
data_recomm["Resources"] = data_recomm["Resources"].apply(lambda x: x.split('\n'))
data_recomm["Domain"] = data_recomm["Resources"].apply(lambda x: [urlparse(url).netloc for url in x])
return data_recomm.to_dict(orient='records')
# [Routing untuk API Face Analysis]
@app.route("/api/faceDetect",methods=['POST'])
def apiDeteksi():
# Get File Gambar yg telah diupload pengguna
uploaded_file = request.files['file']
filename = secure_filename(uploaded_file.filename)
# Set/mendapatkan extension dan path dari file yg diupload
gambar_prediksi = f'./static/images/results/{filename}'
if filename != '':
# Simpan Gambar
save_path = os.path.join("static/images/results/", filename)
uploaded_file.save(save_path)
# Conversi file ke jpg
filename, extension = os.path.splitext(save_path)
new_path = f"{filename}.jpg"
shutil.move(gambar_prediksi, new_path)
# Predict Image
probs, classes = predict2(new_path, model)
probsMax = max(probs)
if probsMax>0.85:
faceClasses = face_classes[classes[probs.index(probsMax)]]
else:
faceClasses = 'Normal / Not Detect'
diagnosis = face_classes[classes[probs.index(probsMax)]]
akurasi = "{:.2f}%".format(probsMax*100)
else:
faceClasses = "Upload jpeg file"
data_recomm = medication(faceClasses)
results = {
"prediksi": faceClasses,
"diagnosis": diagnosis,
"akurasi": akurasi,
"gambar_prediksi" : new_path,
"data_rekomendasi": data_recomm
}
# print(results)
# Return hasil prediksi dengan format JSON
return jsonify(results)
if __name__ == '__main__':
# Load model yang telah ditraining
try:
model.load_state_dict(torch.load('model.pth'))
except RuntimeError as error:
if 'Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False' in str(error):
model.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu')))
else:
raise error
# Run Flask di localhost
app.run(port=5001, debug=True)