-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
162 lines (138 loc) · 5.84 KB
/
app.py
File metadata and controls
162 lines (138 loc) · 5.84 KB
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
from warnings import filterwarnings as f
f('ignore')
import os
from dotenv import load_dotenv
load_dotenv()
from typing import List
import numpy as np
from age import predict_age
import pandas as pd
from PIL import Image
import tensorflow as tf
from keras.models import load_model
import keras
from models.skin_tone.skin_tone_knn import identify_skin_tone
from models.skin_tone.skin_detection import skin_detection
import cv2
from tensorflow.keras.preprocessing import image
from flask import Flask, request, render_template,flash
from flask_restful import Api, Resource, reqparse, abort
from werkzeug.utils import secure_filename
from models.recommender.rec import recs_essentials, makeup_recommendation
import base64
from Privatefunc import *
from io import BytesIO
from PIL import Image
from skinage import get_details
from Results import get_recommendations,get_skin_summary
a = os.environ.get('MODEL1PATH')
b = os.environ.get('MODEL2PATH')
app = Flask(__name__)
# api = Api(app)
class_names1 = ['Dry_skin', 'Normal_skin', 'Oil_skin']
class_names2 = ['Low', 'Moderate', 'Severe']
skin_tone_dataset = 'models/skin_tone/skin_tone_dataset.csv'
print("here")
def get_model():
global model1, model2
model1 = load_model_private_fnc(a)
print('Model 1 loaded')
model2 = load_model_private_fnc(b)
print("---"*15,"\n\nBoth Model loaded successfully!")
def load_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
# (height, width, channels)
img_tensor = image.img_to_array(img)
# (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
img_tensor = np.expand_dims(img_tensor, axis=0)
# imshow expects values in the range [0, 1]
img_tensor /= 255.
return img_tensor
def prediction_skin(img_path):
new_image = load_image(img_path)
pred1 = model1.predict(new_image)
# print(pred1)
if len(pred1[0]) > 1:
pred_class1 = class_names1[tf.argmax(pred1[0])]
else:
pred_class1 = class_names1[int(tf.round(pred1[0]))]
return pred_class1
def prediction_acne(img_path):
new_image = load_image(img_path)
pred2 = model2.predict(new_image)
# print(pred2)
if len(pred2[0]) > 1:
pred_class2 = class_names2[tf.argmax(pred2[0])]
else:
pred_class2 = class_names2[int(tf.round(pred2[0]))]
return pred_class2
get_model()
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def home():
return render_template('home.html')
@app.route("/predict", methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
if 'file' not in request.files:
return render_template('error.html', message="No file part")
file = request.files['file']
if file.filename == '':
return render_template('error.html', message="No selected file")
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join('static/', filename)
file.save(file_path)
try:
skin_type = prediction_skin(file_path)
acne_type = prediction_acne(file_path)
skin_tone_hsv = skin_detection(file_path)
skin_tone_class = identify_skin_tone(file_path, skin_tone_dataset)
skin_tone_desc = get_skin_tone_description(skin_tone_class)
predicted_age = predict_age(file_path)
if skin_tone_class == 8: # Abnormal Color Values
flash("The skin tone analysis produced unusual results. Please retake the photo under better lighting conditions.")
recommendations = get_recommendations(skin_type, acne_type, skin_tone_desc, skin_tone_hsv, predicted_age)
skin_summary = get_skin_summary(skin_type, acne_type, skin_tone_desc, skin_tone_hsv, predicted_age)
js = get_details(file_path)
w_s_file = 'w_s_'+filename
img = Image.fromarray(js['frame'])
img.save('static/'+w_s_file)
#print(type(js['frame']))
return render_template('results.html',
skin_type=skin_type,
acne_type=acne_type,
skin_tone=skin_tone_desc,
skin_tone_class=skin_tone_class,
skin_tone_hsv=skin_tone_hsv,
wrinkles = js['wrinkles'],
spots = js['spots'],
textures = js['textures'],
skin_age = js['skin_age'],
w_s_image = w_s_file,
predicted_age=predicted_age,
recommendations=recommendations,
skin_summary=skin_summary,
image_file=filename)
except Exception as e:
return render_template('error.html', message=str(e))
else:
return render_template('error.html', message="File type not allowed")
return render_template('upload.html')
def get_skin_tone_description(tone_class):
tone_descriptions = {
1: "Translucent White",
2: "Fair",
3: "Natural",
4: "Natural Medium",
5: "Wheat",
6: "Brown",
7: "Dark Brown",
8: "Abnormal Color Values"
}
return tone_descriptions.get(tone_class, "Unknown")
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0',port=5500)