Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- backend/__init__.py +0 -0
- backend/app.py +151 -0
- backend/requirements.txt +7 -7
backend/__init__.py
ADDED
File without changes
|
backend/app.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import List
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from PIL import Image
|
6 |
+
import tensorflow as tf
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
+
from tensorflow.keras.preprocessing import image
|
9 |
+
from tensorflow.keras.preprocessing.image import load_img
|
10 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
11 |
+
from models.skin_tone.skin_tone_knn import identify_skin_tone
|
12 |
+
from flask import Flask, request
|
13 |
+
from flask_restful import Api, Resource, reqparse, abort
|
14 |
+
import werkzeug
|
15 |
+
from models.recommender.rec import recs_essentials, makeup_recommendation
|
16 |
+
import base64
|
17 |
+
from io import BytesIO
|
18 |
+
import logging
|
19 |
+
|
20 |
+
app = Flask(__name__)
|
21 |
+
api = Api(app)
|
22 |
+
|
23 |
+
# Set up logging
|
24 |
+
logging.basicConfig(level=logging.DEBUG)
|
25 |
+
logger = logging.getLogger(__name__)
|
26 |
+
|
27 |
+
class_names1 = ['Dry_skin', 'Normal_skin', 'Oil_skin']
|
28 |
+
class_names2 = ['Low', 'Moderate', 'Severe']
|
29 |
+
skin_tone_dataset = 'models/skin_tone/skin_tone_dataset.csv'
|
30 |
+
|
31 |
+
def get_model():
|
32 |
+
global model1, model2
|
33 |
+
model1 = load_model('./models/skin_model')
|
34 |
+
print('Model 1 loaded')
|
35 |
+
model2 = load_model('./models/acne_model')
|
36 |
+
print("Model 2 loaded!")
|
37 |
+
|
38 |
+
def load_image(img_path):
|
39 |
+
img = image.load_img(img_path, target_size=(224, 224))
|
40 |
+
img_tensor = image.img_to_array(img)
|
41 |
+
img_tensor = np.expand_dims(img_tensor, axis=0)
|
42 |
+
img_tensor /= 255.
|
43 |
+
return img_tensor
|
44 |
+
|
45 |
+
def prediction_skin(img_path):
|
46 |
+
new_image = load_image(img_path)
|
47 |
+
pred1 = model1.predict(new_image)
|
48 |
+
if len(pred1[0]) > 1:
|
49 |
+
pred_class1 = class_names1[tf.argmax(pred1[0])]
|
50 |
+
else:
|
51 |
+
pred_class1 = class_names1[int(tf.round(pred1[0]))]
|
52 |
+
return pred_class1
|
53 |
+
|
54 |
+
def prediction_acne(img_path):
|
55 |
+
new_image = load_image(img_path)
|
56 |
+
pred2 = model2.predict(new_image)
|
57 |
+
if len(pred2[0]) > 1:
|
58 |
+
pred_class2 = class_names2[tf.argmax(pred2[0])]
|
59 |
+
else:
|
60 |
+
pred_class2 = class_names2[int(tf.round(pred2[0]))]
|
61 |
+
return pred_class2
|
62 |
+
|
63 |
+
get_model()
|
64 |
+
|
65 |
+
# Parsing arguments for image and recommendations
|
66 |
+
img_put_args = reqparse.RequestParser()
|
67 |
+
img_put_args.add_argument("file", help="Please provide a valid image file", required=True)
|
68 |
+
|
69 |
+
rec_args = reqparse.RequestParser()
|
70 |
+
rec_args.add_argument("tone", type=int, help="Argument required", required=True)
|
71 |
+
rec_args.add_argument("type", type=str, help="Argument required", required=True)
|
72 |
+
rec_args.add_argument("features", type=dict, help="Argument required", required=True)
|
73 |
+
|
74 |
+
# Recommendation Class
|
75 |
+
class Recommendation(Resource):
|
76 |
+
logger.info(f"Received recommendation request before --------")
|
77 |
+
def put(self):
|
78 |
+
args = rec_args.parse_args()
|
79 |
+
|
80 |
+
# Log the incoming recommendation request
|
81 |
+
logger.info(f"Received recommendation request with data: {args}")
|
82 |
+
|
83 |
+
features = args['features']
|
84 |
+
tone = args['tone']
|
85 |
+
skin_type = args['type'].lower()
|
86 |
+
skin_tone = 'light to medium'
|
87 |
+
|
88 |
+
# Adjust skin tone based on the tone input
|
89 |
+
if tone <= 2:
|
90 |
+
skin_tone = 'fair to light'
|
91 |
+
elif tone >= 4:
|
92 |
+
skin_tone = 'medium to dark'
|
93 |
+
|
94 |
+
# Log the skin tone and type
|
95 |
+
logger.info(f"Skin tone: {skin_tone}, Skin type: {skin_type}")
|
96 |
+
|
97 |
+
fv = []
|
98 |
+
for key, value in features.items():
|
99 |
+
fv.append(int(value))
|
100 |
+
|
101 |
+
# Log the features sent for recommendation
|
102 |
+
logger.info(f"Features: {fv}")
|
103 |
+
|
104 |
+
try:
|
105 |
+
general = recs_essentials(fv, None)
|
106 |
+
makeup = makeup_recommendation(skin_tone, skin_type)
|
107 |
+
|
108 |
+
# Log the recommendation data being returned
|
109 |
+
logger.info(f"Generated recommendations: General: {general}, Makeup: {makeup}")
|
110 |
+
|
111 |
+
return {'general': general, 'makeup': makeup}
|
112 |
+
|
113 |
+
except Exception as e:
|
114 |
+
logger.error(f"Error during recommendation generation: {str(e)}")
|
115 |
+
return {'error': 'Error processing recommendations'}, 400
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
# Skin Metrics Class
|
120 |
+
class SkinMetrics(Resource):
|
121 |
+
def put(self):
|
122 |
+
args = img_put_args.parse_args()
|
123 |
+
|
124 |
+
# Log the incoming image request
|
125 |
+
logger.info(f"Received image for skin metrics analysis: {args}")
|
126 |
+
|
127 |
+
file = args['file']
|
128 |
+
starter = file.find(',')
|
129 |
+
image_data = file[starter+1:]
|
130 |
+
|
131 |
+
image_data = bytes(image_data, encoding="ascii")
|
132 |
+
im = Image.open(BytesIO(base64.b64decode(image_data+b'==')))
|
133 |
+
|
134 |
+
filename = 'image.png'
|
135 |
+
file_path = os.path.join('./static', filename)
|
136 |
+
im.save(file_path)
|
137 |
+
|
138 |
+
skin_type = prediction_skin(file_path).split('_')[0]
|
139 |
+
acne_type = prediction_acne(file_path)
|
140 |
+
tone = identify_skin_tone(file_path, dataset=skin_tone_dataset)
|
141 |
+
|
142 |
+
# Log the predictions for skin type, acne type, and skin tone
|
143 |
+
logger.info(f"Predicted skin type: {skin_type}, acne type: {acne_type}, tone: {tone}")
|
144 |
+
|
145 |
+
return {'type': skin_type, 'tone': str(tone), 'acne': acne_type}, 200
|
146 |
+
|
147 |
+
api.add_resource(SkinMetrics, "/upload")
|
148 |
+
api.add_resource(Recommendation, "/recommend")
|
149 |
+
|
150 |
+
if __name__ == "__main__":
|
151 |
+
app.run(debug=False)
|
backend/requirements.txt
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
numpy
|
2 |
-
pandas
|
3 |
-
matplotlib
|
4 |
-
scikit-learn
|
5 |
Flask==2.0.2
|
6 |
Flask-RESTful==0.3.9
|
7 |
-
tensorflow
|
8 |
opencv-python
|
9 |
-
Werkzeug
|
10 |
Jinja2
|
11 |
click
|
12 |
reparse==3.0
|
13 |
-
Pillow
|
|
|
1 |
+
numpy==1.24.3
|
2 |
+
pandas==1.5.0
|
3 |
+
matplotlib==3.9.2
|
4 |
+
scikit-learn==1.3.0
|
5 |
Flask==2.0.2
|
6 |
Flask-RESTful==0.3.9
|
7 |
+
tensorflow-macos==2.13.0
|
8 |
opencv-python
|
9 |
+
Werkzeug==2.1.2
|
10 |
Jinja2
|
11 |
click
|
12 |
reparse==3.0
|
13 |
+
Pillow==9.3.0
|