forked from EmmanuelEzenwere/MagicMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserProfile.py
151 lines (121 loc) · 5.68 KB
/
userProfile.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
import os
import datetime as dt
from PIL import Image
from database import Database
class UserProfile(object):
def __init__(self, user_name):
"""
:param user_name: username for the user.
"""
self.user_name = user_name
Database.initialize()
def profile(self):
"""
:return: users face shape, skin_color.
# we could extend the user's profile to include other details in the future such as appearance rating, make up metrics, ...
"""
user_name = self.user_name
profile_dict = Database.find_one('users', {'users': user_name})
return profile_dict
def add_user(self, password, gender, users_face_path):
"""
:param users_face_path: string, full path to an image of the user's face.
:param password: string, user's password.
:param gender: string, sex of the user, strictly male or female.
"""
from faceShape import getFaceShape
face_shape = getFaceShape(users_face_path)
print("face_shape:", face_shape)
from skinComplexion import SkinComplexion
skin_complexion = SkinComplexion(users_face_path).identify()
img_dir = os.path.dirname(__file__) + '/file_storage/users/' + str(gender) + '/'
no_of_users = self.count(img_dir) + 1
img_path = img_dir + str(self.user_name) + '.jpg'
profile_dict = {"user_name": self.user_name, "password": password, "gender": gender, "face_shape": face_shape, "skin_complexion": skin_complexion, 'index': no_of_users, 'storage_location': img_path}
Database.insert('users', profile_dict)
selfie = Image.open(users_face_path)
selfie.save(img_path)
status = 'user profile created and added to database'
return status
def sign_in(self, password):
"""
:param password: string, user's password.
:return status: string, authenticated, invalid or sign-in duplicate.
"""
search_dict = Database.find_one('users', {'username': self.user_name, 'password': password})
if len(search_dict) == 1:
status = 'authenticated'
elif len(search_dict) == 0:
status = 'invalid_user'
else:
status = 'authenticated, but more that one users share this username & password'
return status
@staticmethod
def ensure_dir(img_dir):
directory = os.path.dirname(img_dir)
if not os.path.isdir(directory):
os.makedirs(directory)
def construct_path(self, category, style):
"""
:return:
"""
user_profile = self.profile()
gender = user_profile['gender']
face_shape = user_profile['face_shape']
skin_complexion = user_profile['skin_complexion']
img_dir = os.path.dirname(__file__)+'/file_storage/hairstyles/' + face_shape + '/' + skin_complexion + '/' + gender + '/' + category + '/' + style
return img_dir
def count(self, img_dir):
"""
:return:int, number of hairstyles in a given brands directory.
loops over the content of img_dir and establishes a count.
"""
count = 0
self.ensure_dir(img_dir)
for f in os.listdir(img_dir):
if os.path.isfile(os.path.join(img_dir, f)):
count += 1
return count
@staticmethod
def retrieve(category, style, index):
"""
Goes into the MagicMirror.ai hairstyle database and retrieves a specified hairstyle.
:param index: this is the index of hairstyle models to be retrieved
:param category:
:param style:
:return: image bytes representation of the given hairstyle
"""
# save hairstyle to database.
hairstyle_profile = Database.find_one('hairstyles', {'category': category, 'style': style, 'index': index})
image_path = hairstyle_profile['storage_location']
try:
image = Image.open(image_path)
return image
except FileNotFoundError:
return 'requested hairstyle model does not exist'
def upload_hairstyle(self, hair_model_path, gender, category, style):
"""Saves a newly uploaded hairstyle image to the hairstyle database considering the user's profile. and hairstyle details.
"""
from faceShape import getFaceShape
# hair_model_path is expected to be the full path to the uploaded hair model.
face_shape = getFaceShape(hair_model_path)
from skinComplexion import SkinComplexion
skin_complexion = SkinComplexion(hair_model_path).identify()
img_dir = os.path.dirname(
__file__) + '/file_storage/hairstyles/' + gender + '/' + category + '/' + style + '/' + face_shape + '/' + skin_complexion +'/'
self.ensure_dir(img_dir)
no_of_hairmodels = self.count(img_dir) + 1
image_path = img_dir + str(no_of_hairmodels) + '.jpg'
hair_model = Image.open(hair_model_path)
hair_model.save(image_path)
created_date = dt.datetime.now()
default_relevancy_score = 1
profile_dict = {"category": category, 'style': style, 'index': no_of_hairmodels, "creator": self.user_name, "gender": gender,
"face_shape": face_shape, "skin_complexion": skin_complexion, "created_date": created_date,
'relevancy_score': default_relevancy_score, 'likes': 1, 'storage_location': image_path}
# save hairstyle to database.
Database.insert('hairstyles', profile_dict)
return 'hair models created and added to database.'
# Dev Notes.
# brainstorming on possible information to learn from a users face for drea.ai
# Cosmetic products they need. This will be used for recommendation.