-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
472 lines (429 loc) · 18.7 KB
/
app.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/python
# -*- encoding: utf-8 -*-
from models.model import BiSeNet
import src.usersManagement as UM
import src.lipstickRecommendation as LR
from utils.colorMethods import ColourDistance
from utils.colorMethods import HexadecimalColor2RGB
import torch
import torchvision.transforms as transforms
from flask import Flask, request, Response, send_file, jsonify
from flask_pymongo import PyMongo
import numpy as np
from PIL import Image
import cv2
import math
import time
import json
import io
import os
# Lip makeup function works in the HSV color space
def LipMakeUp(image, parsing, color):
b, g, r = color
tar_color = np.zeros_like(image)
tar_color[:, :, 0] = b
tar_color[:, :, 1] = g
tar_color[:, :, 2] = r
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
tar_hsv = cv2.cvtColor(tar_color, cv2.COLOR_BGR2HSV)
image_hsv[:, :, 0:2] = tar_hsv[:, :, 0:2]
changed = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR)
not12and13 = (parsing != 12) & (parsing != 13)
changed[not12and13] = image[not12and13]
return changed
# Load into memory
# Load model
n_classes = 19
net = BiSeNet(n_classes=n_classes)
save_pth = './res/cp/79999_iter.pth'
net.load_state_dict(torch.load(save_pth, map_location=torch.device('cpu')))
net.eval()
# Tensor
to_tensor = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
# Other para
predictDataDir = './res/data/face-parsing-predictImgDir/'
makeupDataDir = './res/data/face-parsing-makeupImgDir/'
# Flask with MongoDB connected, you can access collection with mongo.db.collection_name.find(query)
app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb://%s:%s@localhost:27017/%s?authMechanism=SCRAM-SHA-1' % (
os.environ.get('LIPSTICKFINDER_USER'), os.environ.get('LIPSTICKFINDER_PSW'), os.environ.get('LIPSTICKFINDER_DATABSE')
)
mongo = PyMongo(app)
# Add RGB to lipsticks
lipsticks_db = list(mongo.db.lipsticks.find({}, {'_id': 0, 'link': 0}))
for lipstick in lipsticks_db:
lipstick['RGB'] = HexadecimalColor2RGB(lipstick['color'])
# Triggers
# Return last _id and total number to user to check database lipstick updation
@app.route('/checkLipstickUpdate', methods=['GET'])
def checkLipstickUpdate():
return jsonify('%s-%d' % ( # _id-count
str(list(mongo.db.lipsticks.find().sort([('_id', -1)]).limit(1))[0]['_id']),
mongo.db.lipsticks.estimated_document_count()
))
# Lipstick recognition
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
# Save image
image = request.files['file']
fileName = str(time.time())
filePath = predictDataDir+fileName+'.jpg'
image.save(filePath)
# Exact rgb value
with torch.no_grad():
image = Image.open(filePath).convert('RGB')
imgArray = np.array(image)
image = to_tensor(image)
image = torch.unsqueeze(image, 0)
out = net(image)[0]
parsing = out.squeeze(0).cpu().numpy().argmax(0)
upperLipPos = np.where(parsing == 12)
lowerLipPos = np.where(parsing == 13)
if len(upperLipPos[0]) > 0 or len(lowerLipPos[0]) > 0:
pointsCount = len(upperLipPos[0])+len(lowerLipPos[0])
rgb = [0, 0, 0]
if len(upperLipPos) > 0:
rgbUpper = np.sum(imgArray[upperLipPos[0], upperLipPos[1], :], axis=0)
rgb = np.sum([rgb, rgbUpper], axis=0)
if len(lowerLipPos) > 0:
rgbLower = np.sum(imgArray[lowerLipPos[0], lowerLipPos[1], :], axis=0)
rgb = np.sum([rgb, rgbLower], axis=0)
res = [math.floor(i/pointsCount) for i in rgb]
# Calculate the distance between extracted RGB and all lipsticks' RGB
for lipstick in lipsticks_db:
lipstick['distance'] = ColourDistance(res, lipstick['RGB'])
result = sorted(lipsticks_db, key=lambda x: x['distance'])[:3]
return jsonify(result)
else: return jsonify([])
# Lip makeup
@app.route('/makeup', methods=['POST'])
def makeup():
if request.method == 'POST':
# Save image
image = request.files['file']
predictFileName = str(time.time())
predictFilePath = makeupDataDir + predictFileName + '.jpg'
image.save(predictFilePath)
# Get RGB
hexadecimalColor = request.values.get("color")
rgb = HexadecimalColor2RGB(hexadecimalColor)
# Make up lip
with torch.no_grad():
image = Image.open(predictFilePath)
image = to_tensor(image)
image = torch.unsqueeze(image, 0)
out = net(image)[0]
parsing = out.squeeze(0).cpu().numpy().argmax(0)
upperLipPos = np.where(parsing == 12)
lowerLipPos = np.where(parsing == 13)
if len(upperLipPos[0]) > 0 or len(lowerLipPos[0]) > 0:
bgr = list(reversed(rgb))
ori = cv2.imread(predictFilePath)
ori = LipMakeUp(ori, parsing, bgr)
# Save img
makeupFileName = str(time.time())
makeupFilePath = makeupDataDir + makeupFileName + '.jpg'
cv2.imwrite(makeupFilePath, ori)
with open(makeupFilePath, 'rb') as bites:
return send_file(
io.BytesIO(bites.read()),
attachment_filename='makeup.jpg',
mimetype='image/jpg'
)
else:
with open(filePath, 'rb') as bites:
return send_file(
io.BytesIO(bites.read()),
attachment_filename='nolip.jpg',
mimetype='image/jpg'
)
# Return object used for recommendation
class recommendLipstickInfoVos:
def __init__(self):
self.recommendLipstickInfoVos = []
# Get recommend lipsticks
@app.route('/getRecommendLipsticks', methods=['POST'])
def getRecommendLipsticks():
lipsticks = recommendLipstickInfoVos()
data = request.get_data(parse_form_data=False)
data = json.loads(data, encoding='utf-8')
userID = int(data["userID"])
password = data["password"]
if mongo.db.Users.count_documents({'ID': userID,'password':password})==1:
for lipstick in mongo.db.lipsticks.find({}, {'_id': 0}).limit(2):
lipstick['like'] = False
lipsticks.recommendLipstickInfoVos.append(lipstick)
return jsonify(lipsticks.__dict__)
# Get lipsticks' detailed information
@app.route('/getRecommendLipstickInfo', methods=['POST'])
def getRecommendLipstickInfo():
lipsticks = recommendLipstickInfoVos()
data = request.get_data(parse_form_data=False)
data = json.loads(data, encoding='utf-8')
userID = int(data["userID"])
password = data["password"]
if mongo.db.Users.count_documents({'ID': userID,'password':password})==1:
likelist = []
if mongo.db.likes.count_documents({'userID': userID})==1:
reply = mongo.db.likes.find({'userID': userID})[0]
likelist = reply["lipstickID"]
for lipstick in LR.getRecommendation(mongo, userID):
lipstick['like'] = (lipstick["lipstick_id"] in likelist)
lipsticks.recommendLipstickInfoVos.append(lipstick)
return jsonify(lipsticks.__dict__)
# Get lipstick brand and brand_id
@app.route('/lipstick-brand', methods=['GET'])
def getLipstickBrand():
if request.method == 'GET':
return jsonify(list(mongo.db.mapping.find(
{'type': 'brand'},
{'name': 1, 'id': 1, '_id': 0}
)))
# Given brand_id, return corresponding series and series_id
@app.route('/lipstick-series', methods=['GET'])
def getLipstickSeries():
if request.method == 'GET':
brand_id = int(request.args.get("brand_id"))
return jsonify(list(mongo.db.mapping.find(
{'brand_id': brand_id, 'type': 'series'},
{'name': 1, 'id': 1, '_id': 0}
)))
# Given brand_id and series_id, return corresponding lipsticks
@app.route('/lipsticks', methods=['GET'])
def getLipsticks():
if request.method == 'GET':
brand_id = int(request.args.get("brand_id"))
series_id = int(request.args.get("series_id"))
lipsticks = []
for row in mongo.db.lipsticks.find(
{'brand_id': brand_id, 'series_id': series_id},
{'_id': 0, 'brand': 1, 'series': 1, 'liquid': 1, 'texture': 1, 'color': 1, 'price': 1, 'name': 1, 'lipstick_id': 1}
):
row['color'] = row['color'].replace(' ', '')
lipsticks.append(row)
return jsonify(lipsticks)
# Given userID and profile image, save image to file system and image path to MongoDB
@app.route('/updateProfileImage', methods=['POST'])
def updateProfileImage(root = "./res/data/profiles/"):
if request.method == 'POST':
userID = int(request.values.get("userID"))
pwd = request.values.get("pwd")
image = request.files['file']
password = mongo.db.Users.find({'ID': userID}, {'_id': 0, 'password': 1})[0]
if (pwd==password['password']):
path = os.path.join(root, '%d.%s' % (userID, image.content_type.replace('image/', '')))
image.save(path)
if os.path.exists(path):
result = mongo.db.Users.update_one(
{'ID': userID}, {"$set": { "path": path}}
)
if result.modified_count == 1:
return Response("OK", status=200)
return Response("Fail to update database with profileImage_path", status=500)
else:
return Response("Fail to save image", status=500)
else:
return Response("Wrong Password", status=500)
# Given userID and password, save user info to MongoDB
@app.route('/updateProfileInfo', methods=['POST'])
def updateProfileInfo(root = "./res/data/profiles"):
data = request.get_json()
userID = data["userID"]
pwd = data["pwd"]
name = data["name"]
gender = data["gender"]
password = mongo.db.Users.find({'ID': userID}, {'_id': 0, 'password': 1})[0]
if (pwd==password['password']):
result = mongo.db.Users.update_one(
{'ID': userID}, {"$set": {"name": name, "gender": gender}}
)
if result.modified_count == 1:
return Response("OK", status=200)
return Response("Fail to update database with name and gender", status=500)
# Given userID and password, return user information
@app.route('/getUserInfo', methods=['GET'])
def getUserInfo():
if request.method == 'GET':
userID = int(request.args.get("userID"))
pwd = request.args.get("pwd")
password = mongo.db.Users.find({'ID': userID}, {'_id': 0, 'password': 1})[0]
if (pwd==password['password']):
record = list(mongo.db.Users.find({'ID': userID}, {'_id': 0, 'path': 0}))[0]
return jsonify(record)
else:
return Response("Wrong Password", status=500)
# Given userID and password, return user image
@app.route('/getUserProfileImage', methods=['GET'])
def getUserProfileImage():
if request.method == 'GET':
userID = int(request.args.get("userID"))
pwd = request.args.get("pwd")
user = mongo.db.Users.find_one({'ID': userID,'password':pwd})
if user is None:
return Response("Wrong Password", status=500)
else:
if 'path' not in user or user['path'] is None:
print("No image")
return Response("No image", status=500)
else:
if os.path.exists(user['path']):
with open(user['path'], 'rb') as bites:
return send_file(
io.BytesIO(bites.read()),
attachment_filename='profile.jpg',
mimetype='image/jpg'
)
else:
return Response("No image", status=500)
# Given userID and password, return user like list (full lipstick info)
@app.route('/getLipstickLike', methods=['GET'])
def getLipstickLike():
if request.method == 'GET':
userID = int(request.args.get("userID"))
pwd = request.args.get("pwd")
user = mongo.db.Users.find({'ID': userID, 'password':pwd})
if user is None:
return Response("Wrong Password", status=500)
else:
if mongo.db.likes.count_documents({'userID': userID})==1:
reply = mongo.db.likes.find({'userID': userID}, {'_id': 0})[0]
lipsticks = []
for lipstickID in reply['lipstickID']:
lipstickInfo = list(mongo.db.lipsticks.find({'lipstick_id': lipstickID},{'_id': 0, 'brand': 1, 'series': 1, 'liquid': 1, 'texture': 1, 'color': 1, 'price': 1, 'name': 1, 'lipstick_id': 1}))[0]
lipsticks.append(lipstickInfo)
return jsonify(lipsticks)
else:
lipsticks_example = [{'brand': 'Here is a example', 'series': 'Storge', 'name': ' Lipstick You Like', 'liquid': True, 'texture': 'Texture', 'color': '#CA7476', 'price': 280, 'lipstick_id': '00000'},]
return jsonify(lipsticks_example)
# Marked as unlike. Given userID, pwd and specific lipstickID, remove this lipstickID from like-lipstick-list
@app.route('/markIfLike', methods=['POST'])
def markIfLike():
if request.method == 'POST':
data = request.get_json()
userID = int(data["userID"])
pwd = data["pwd"]
likeLipstickID = data["likeLipstickID"] #string
likeLabel = data["likeLabel"]
user = mongo.db.Users.find({'ID': userID, 'password':pwd})
print("here")
if user is None:
print("Wa")
return Response("Wrong Password", status=500)
else:
if mongo.db.likes.count_documents({'userID': userID}) is 1:
reply = mongo.db.likes.find({'userID': userID})[0]
newLikeLipsticks = reply['lipstickID']
# labeled as like
if likeLabel is True:
# add this new lipstick in like list
if likeLipstickID not in newLikeLipsticks:
newLikeLipsticks.append(likeLipstickID)
print(newLikeLipsticks)
# update rating
if mongo.db.rating.count_documents({"userID": userID, 'lipstickID': likeLipstickID}) is 1:
mongo.db.rating.update_one({'userID': userID, 'lipstickID': likeLipstickID}, {"$set": {"rating": 10}})
else:
mongo.db.rating.insert_one({'userID': userID, 'lipstickID': likeLipstickID, 'rating': 10})
# labeled as unlike
else:
# remove this lipstick in like list
newLikeLipsticks.remove(likeLipstickID)
# update rating
mongo.db.rating.update_one({'userID': userID, 'lipstickID': likeLipstickID}, {"$set": {"rating": 0}})
mongo.db.likes.update_one({'userID': userID}, {"$set": {"lipstickID": newLikeLipsticks}})
print(mongo.db.rating.find({'userID': userID, 'lipstickID': likeLipstickID})[0])
else:
newLikeLipsticks = []
# add this new lipstick in like list
newLikeLipsticks.append(likeLipstickID)
# update rating
mongo.db.rating.insert_one({'userID': userID, 'lipstickID': likeLipstickID, 'rating': 10})
mongo.db.likes.insert_one({'userID': userID, "lipstickID": newLikeLipsticks})
return Response("OK")
# Given userID, pwd and specific lipstickID, rating+1
@app.route('/focusOnLipstick', methods=['POST'])
def focusOnLipstick():
if request.method == 'POST':
data = request.get_json()
print(data)
userID = int(data["userID"])
pwd = data["pwd"]
lipstickID = data["lipstickID"] #string
print(lipstickID)
user = mongo.db.Users.find({'ID': userID, 'password':pwd})
if user is None:
return Response("Wrong Password", status=500)
else:
if mongo.db.rating.count_documents({"userID": userID, 'lipstickID': lipstickID}) is 1:
reply = mongo.db.rating.find({'userID': userID, 'lipstickID': lipstickID})[0]
if reply['rating']<10:
newrating = reply['rating']+1
mongo.db.rating.update_one({'userID': userID, 'lipstickID': lipstickID}, {"$set": {"rating": newrating}})
print(mongo.db.rating.find({'userID': userID, 'lipstickID': lipstickID})[0])
else:
mongo.db.rating.insert_one({'userID': userID, 'lipstickID': lipstickID, 'rating': 1})
print(mongo.db.rating.find({'userID': userID, 'lipstickID': lipstickID})[0])
return Response("OK")
# Reset Password
@app.route('/resetPwd', methods=['POST'])
def resetPwd():
data = request.get_json()
userID = int(data["userID"])
pwd = data["pwd"]
newPwd = data["newPwd"]
password = mongo.db.Users.find({'ID': userID}, {'_id': 0, 'password': 1})[0]
if (pwd==password['password']):
result = mongo.db.Users.update_one(
{'ID': userID}, {"$set": {"password": newPwd}}
)
return Response("OK")
# Login Check the Email and Password
@app.route('/checkLoginInfo', methods=['POST'])
def checkLoginInfo():
data = request.get_json()
email = data["email"]
password = data["password"]
res = {"email":email, "password":password}
uid, flag = UM.login(mongo, email, password);
if flag:
res["uid"] = uid
res["result"] = "True"
else:
res["result"] = "False"
return jsonify(res)
# Login Check the Email and Password
@app.route('/signUp', methods=['POST'])
def signUp():
data = request.get_json()
email = data["email"]
password = data["password"]
res = {"email":email, "password":password}
uid, flag = UM.signup(mongo, email, password);
if flag:
res["uid"] = uid
res["result"] = "True"
else:
res["result"] = "False"
return jsonify(res)
# Upload questionaire
@app.route('/uploadQuestionnaire', methods=['POST'])
def uploadQuestionnaire():
data = request.get_json()
email = data["email"]
password = data["password"]
gender = data["gender"]
kind = data["kind"]
texture = data["texture"]
color = data["color"]
res = {"email":email, "password":password}
if UM.answerquestions(mongo, email, password, gender, kind, texture, color):
res["result"] = "True"
else:
res["result"] = "False"
return jsonify(res)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)