-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
298 lines (269 loc) · 9.34 KB
/
server.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from flask import Flask
from flask import render_template
import dataHTML
import facial_features
import base64
from decision import predict_image
from flask import Response, request, jsonify, redirect, url_for, session
import os
import cv2
from werkzeug.utils import secure_filename
from datetime import datetime
app = Flask(__name__)
app.secret_key = "sk"
# global var
data = []
# Hex codes associated with each season
colors = {'Winter': {
'Pure White': '#F0EDE1',
'Light True Gray': '#D1D3D4',
'Medium True Gray': '#71716F',
'Charcoal Gray': '#6C6868',
'Black': '#111810',
'Gray-Beige': '#927C66',
'Navy Blue': '#403F6F',
'True Blue': '#1F4477',
'Ice Green': '#7ED9C3',
'Ice Yellow': '#FEFECD',
'Iced Aqua': '#ACD3DC',
'Violet Ice': '#C2ACB1',
'Icy Pink': '#EBBECC',
'Blue Ice': '#6F7A9F',
'Royal Blue': '#3E428B',
'Hot Turquoise': '#26FFE9',
'China Blue': '#546477',
'Light True Green': '#00A35B',
'True Green': '#089404',
'Emerald Green': '#009874',
'Pine Green': '#3A795E',
'Shocking Pink': '#DE5B8C',
'Deep Hot Pink': '#E55982',
'Magenta': '#D0417E',
'Fuchsia': '#DF88B7',
'Royal Purple': '#603F83',
'Bright Burgundy': '#64313E',
'Blue-Red': '#47191B',
'True Red': '#BC243C',
'Silver-tone Accessories': '#B1B3B3'
}, 'Summer' : {
'Soft White': '#E9E0C9',
'Rose-Beige': '#E2CAC2',
'Cocoa': '#6C5043',
'Rose-Brown': '#80565B',
'Light Blue-Gray': '#B7C9E2',
'Charcoal Blue-Gray': '#2D4256',
'Grayed Navy': '#656B83',
'Gray-Blue': '#565F7E',
'Powder Blue': '#96B3D2',
'Sky Blue': '#8ABAD3',
'Medium Blue': '#0000CD',
'Periwinkle Blue': '#6667AB',
'Pastel Aqua': '#D8F3E9',
'Pastel Blue-Green': '#A5E3E0',
'Middle Blue-Green': '#8DD9CC',
'Deep Blue-Green': '#006A6E',
'Lemon Yellow': '#FAFA33',
'Powder Pink': '#ECB7B7',
'Pastel Pink': '#F2B8D8',
'Rose Pink': '#FF66CC',
'Deep Rose': '#BB5A58',
'Watermelon': '#FD4659',
'Blue-Red': '#47191B',
'Burgundy': '#64313E',
'Lavender': '#E6E6FA',
'Orchid Bloom': '#C5AECF',
'Pale Mauve': '#C6A4A4',
'Raspberry': '#D32E5E',
'Soft Fuchsia': '#D496BD',
'Plum': '#5A315D',
'Silver-tone Accessories': '#B1B3B3'
}, 'Fall' : {
'Oyster White': '#E3D9C6',
'Warm Beige': '#D3B69C',
'Coffee Brown': '#4A2C2A',
'Dark Chocolate Brown': '#490206',
'Mahogany': '#824D46',
'Camel': '#AD8166',
'Gold': '#C8B273',
'Medium Warm Bronze': '#825E2F',
'Yellow-Gold': '#E5A01D',
'Mustard': '#D8AE48',
'Pumpkin': '#C34121',
'Terra Cotta': '#D38377',
'Rust': '#B55A30',
'Deep Peach/Apricot': '#F9C5B8',
'Salmon': '#FAAA94',
'Orange': '#FF5800',
'Orange-Red': '#F05627',
'Bittersweet Red': '#D93744',
'Dark Tomato Red': '#CE2939',
'Lime Green': '#32CD32',
'Chartreuse': '#B5BF50',
'Bright Yellow-Green': '#9ACD32',
'Moss Green': '#857946',
'Grayed Yellow-Green': '#A29D7D',
'Olive Green': '#8D8B55',
'Jade Green': '#759465',
'Forest Green': '#228B22',
'Turquoise': '#45B8AC',
'Teal Blue': '#478589',
'Deep Periwinkle Blue': '#7C83BC',
'Gold-tone Accessories': '#B49B57'
}, 'Spring' : {
'Ivory': '#F0DFCC',
'Buff': '#EBC396',
'Light Warm Beige': '#FAF0DC',
'Camel': '#B0846A',
'Golden Tan(honey)': '#DCCA98',
'Medium Golden Brown': '#91672F',
'Light Warm Gray': '#D7D2CB',
'Light Clear Navy': '#5C6B9C',
'Light Clear Gold': '#F1E5AC',
'Bright Golden Yellow': '#CB8E16',
'Pastel Yellow-Green': '#EAEDA6',
'Medium Yellow-Green': '#9ACD32',
'Apricot': '#FFC0A0',
'Light Orange': '#FF7231',
'Peach': '#F99584',
'Clear Salmon': '#FA8072',
'Bright Coral': '#FA7268',
'Warm Pastel Pink': '#F6C6BD',
'Coral Pink': '#EAAC9C',
'Clear Bright Warm Pink': '#F6C6BD',
'Clear Bight Red': '#F93822',
'Orange-Red': '#F05627',
'Medium Violet': '#4E008E',
'Light Periwinkle Blue': '#C5CBE1',
'Dark Periwinkle Blue': '#7C83BC',
'Light True Blue': '#2D68C4',
'Light Warm Aqua': '#ACD3DC',
'Clear Bright Aqua': '#30A299',
'Medium Warm Turquoise': '#40E0D0',
'Gold-tone Accessories': '#B49B57'
}}
# Define the path to save images
IMAGE_SAVE_PATH = "./static/images/"
# Ensure the folder exists
os.makedirs(IMAGE_SAVE_PATH, exist_ok=True)
@app.route("/upload_captured_photo", methods=["POST"])
def upload_captured_photo():
if request.method == "POST":
captured_photo_data = request.form.get("captured_photo")
if captured_photo_data:
# Decode the base64 data to binary
captured_photo_data = captured_photo_data.split(",")[1]
img_data = base64.b64decode(captured_photo_data)
# Save the image to the static folder
now = datetime.now()
formatted_time = now.strftime("%Y%m%d%H%M%S%f")
img_filename = "captured_image" + formatted_time +".jpg"
img_path = os.path.join(IMAGE_SAVE_PATH, img_filename)
with open(img_path, "wb") as f:
f.write(img_data)
# Store the image path in the session
session["uploaded_img_file_path"] = os.path.join(
"./static/images/", img_filename
)
global data
data = facial_features.facial_features_and_values(
f"static/images/{img_filename}", False, True, 1
)
_, buffer = cv2.imencode(".jpg", data["eye_left"])
eye_left = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["eye_right"])
eye_right = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["forehead"])
forehead = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["cheek_right"])
cheek_right = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["cheek_left"])
cheek_left = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["hair_mask"])
hair_mask = base64.b64encode(buffer).decode("utf-8")
return render_template(
"sucessful_photo.html",
user_image=img_path,
forehead=forehead,
eye_left=eye_left,
eye_right=eye_right,
cheek_left=cheek_left,
cheek_right=cheek_right,
hair_mask=hair_mask,
)
return "Failed to upload photo", 400
# return redirect(url_for("get_photo"))
return "Failed to upload photo", 400
# ROUTES
@app.route("/")
def home():
return render_template("home.html")
@app.route("/", methods=["POST"])
def back():
return render_template("home.html")
@app.route("/submit", methods=["POST"])
def submit_form():
# if request.form['action'] == 'submit' and request.form['nextpg']:
return redirect(url_for("get_photo"))
@app.route("/upload", methods=["POST"])
def upload_photo():
global data
if request.method == "POST":
uploaded_img = request.files["upload"]
img_filename = secure_filename(uploaded_img.filename)
uploaded_img.save(os.path.join("./static/images/", img_filename))
session["uploaded_img_file_path"] = os.path.join(
"./static/images/", img_filename
)
img_path = session.get("uploaded_img_file_path", None)
data = facial_features.facial_features_and_values(
f"static/images/{img_filename}", False, True, 1
)
_, buffer = cv2.imencode(".jpg", data["eye_left"])
eye_left = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["eye_right"])
eye_right = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["forehead"])
forehead = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["cheek_right"])
cheek_right = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["cheek_left"])
cheek_left = base64.b64encode(buffer).decode("utf-8")
_, buffer = cv2.imencode(".jpg", data["hair_mask"])
hair_mask = base64.b64encode(buffer).decode("utf-8")
return render_template(
"sucessful_photo.html",
user_image=img_path,
forehead=forehead,
eye_left=eye_left,
eye_right=eye_right,
cheek_left=cheek_left,
cheek_right=cheek_right,
hair_mask=hair_mask,
)
return 0
@app.route("/get_photo")
def get_photo():
return render_template("get_photo.html")
@app.route("/color_analysis", methods=["POST"])
def color_analysis():
global data
global colors
if request.method == "POST":
img_path = session.get("uploaded_img_file_path", None)
predict = predict_image(img_path, data)
num = int(predict)
if num == 0:
predict = "Winter"
elif num == 1:
predict = "Summer"
elif num == 2:
predict = "Fall"
else:
predict = "Spring"
color_swatches = colors[predict]
return render_template(
"analysis.html", predict=predict, color_swatches=color_swatches
)
return 0
if __name__ == "__main__":
app.run(debug=True)