jatin-tec commited on
Commit
95e98bb
·
1 Parent(s): 34762ce

initial commit

Browse files
Files changed (9) hide show
  1. .env +2 -0
  2. .gitignore +5 -0
  3. Dockerfile +15 -0
  4. README.md +3 -3
  5. app.py +75 -0
  6. get_patches.py +334 -0
  7. index.py +190 -0
  8. model.py +36 -0
  9. requirements.txt +111 -0
.env ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ BASE_URL = "http://127.0.0.1:7860"
2
+ OPENAI_API_KEY = "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"
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ models
2
+ __pycache__
3
+ Acne_Classifyer_N_Resnet.h5
4
+ haarcascade_eye.xml
5
+ shape_predictor_68_face_landmarks.dat
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10.6
2
+ WORKDIR /code
3
+
4
+ ADD requirements.txt /code/requirements.txt
5
+ ADD app.py index.py model.py get_patches.py /code/
6
+
7
+ RUN apt-get update && apt-get upgrade -y
8
+ RUN apt-get install -y software-properties-common
9
+ RUN apt-get install -y build-essential cmake pkg-config \
10
+ && apt-get install -y libx11-dev libatlas-base-dev \
11
+ && apt-get install -y libgtk-3-dev libboost-python-dev
12
+
13
+ RUN pip install --timeout=100000 -r requirements.txt
14
+
15
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: SkinZen
3
- emoji: 📉
4
- colorFrom: yellow
5
- colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  license: mit
 
1
  ---
2
  title: SkinZen
3
+ emoji: 👁
4
+ colorFrom: blue
5
+ colorTo: red
6
  sdk: docker
7
  pinned: false
8
  license: mit
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from io import BytesIO
4
+ import PIL
5
+ import numpy as np
6
+ import os
7
+
8
+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
9
+
10
+ def sentence_builder(age, sex, skin_type, allergies, diet, file):
11
+ import index
12
+
13
+ print(age, sex, skin_type, allergies, diet)
14
+
15
+ response = index.predict(file)
16
+ predictions = response['prediction']
17
+ prediction = np.array(predictions)
18
+
19
+ data = response
20
+ data["prediction"] = prediction
21
+
22
+ labels = ["Low", "Moderate", "Severe"]
23
+ show_prediction = np.zeros((4, 3))
24
+
25
+
26
+ for in_, pred in enumerate(prediction):
27
+ show_prediction[in_] = pred
28
+
29
+ output1 = {labels[i]: float(show_prediction[0][i]) for i in range(3)}
30
+ output2 = {labels[i]: float(show_prediction[1][i]) for i in range(3)}
31
+ output3 = {labels[i]: float(show_prediction[2][i]) for i in range(3)}
32
+ output4 = {labels[i]: float(show_prediction[3][i]) for i in range(3)}
33
+
34
+ data['age'] = age
35
+ data['gender'] = sex
36
+ data['skin_type'] = skin_type
37
+ data['allergies'] = allergies
38
+ data['diet'] = diet
39
+
40
+
41
+ try:
42
+ response = index.recommendation(data)
43
+ data = response.json()
44
+ content = data['choices'][0]['message']['content']
45
+ return content, output1, output2, output3, output4
46
+ except:
47
+ return "No recommendation found", output1, output2, output3, output4
48
+
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("Flip text or image files using this demo.")
51
+ with gr.Row():
52
+ with gr.Column():
53
+ age = gr.Number(value=20, label="Age")
54
+ sex = gr.Radio(["Male", "Female", "Other"], label="Gender", info="Your Gender")
55
+ skin_type = gr.CheckboxGroup(["Oily", "Dry", "Normal"], label="Skin", info="Skin Type")
56
+ allergy = gr.Dropdown(
57
+ ["benzoyl peroxide", "salicylic acid", "Sun-exposure", "Itching", "Swelling", "Redness"],
58
+ multiselect=True, label="Allergies",
59
+ info="Tell us your allergies and symptoms"
60
+ )
61
+ diet = gr.CheckboxGroup(["Veg", "Non-Veg",], label="Diet", info="Select your diet preference")
62
+ img = gr.Image(source="upload", type="pil", label="Face Image (with open eye)")
63
+ submit = gr.Button("Submit")
64
+
65
+ with gr.Tab("Model:Severity Prediction"):
66
+ chin = gr.Label(num_top_classes=3, label="Chin|Acne Level")
67
+ fh = gr.Label(num_top_classes=3, label="Fore Head|Acne Level")
68
+ lc = gr.Label(num_top_classes=3, label="Left Cheek|Acne Level")
69
+ rc = gr.Label(num_top_classes=3, label="Right Cheek|Acne Level")
70
+ with gr.Tab("Recommendation:Treatment Plan"):
71
+ html_output = gr.HTML('Recommendation will be shown here')
72
+
73
+ submit.click(sentence_builder, inputs=[age, sex, skin_type, allergy, diet, img], outputs=[html_output, rc, lc, chin, fh])
74
+
75
+ demo.launch()
get_patches.py ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ from skimage import io
4
+ import matplotlib.pyplot as plt
5
+ from os import listdir
6
+ from os.path import join, isfile, splitext
7
+ from scipy import misc
8
+ import sys
9
+ import dlib
10
+ import os
11
+ from PIL import Image
12
+ import urllib.request
13
+ import imageio
14
+
15
+ width_ratio = 1.5
16
+ top_ratio = 1.5
17
+ gap_ratio = 0.1
18
+ down_ratio = 4.5
19
+ chin_width_ratio = 2.8
20
+ forehead_ratio = 0.3
21
+ verb = False
22
+
23
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24
+ PREDICTOR_PATH = os.path.join(BASE_DIR, "shape_predictor_68_face_landmarks.dat")
25
+ eye_cascade = cv2.CascadeClassifier(os.path.join(BASE_DIR, "haarcascade_eye.xml"))
26
+
27
+ assert not eye_cascade.empty()
28
+
29
+ SCALE_FACTOR = 1
30
+ FEATHER_AMOUNT = 11
31
+
32
+ FACE_POINTS = list(range(17, 68))
33
+ MOUTH_POINTS = list(range(48, 61))
34
+ RIGHT_BROW_POINTS = list(range(17, 22))
35
+ LEFT_BROW_POINTS = list(range(22, 27))
36
+ RIGHT_EYE_POINTS = list(range(36, 42))
37
+ LEFT_EYE_POINTS = list(range(42, 48))
38
+ NOSE_POINTS = list(range(27, 35))
39
+ JAW_POINTS = list(range(0, 17))
40
+
41
+ OVERLAY_POINTS = [
42
+ LEFT_EYE_POINTS + RIGHT_EYE_POINTS + LEFT_BROW_POINTS + RIGHT_BROW_POINTS,
43
+ NOSE_POINTS + MOUTH_POINTS,
44
+ ]
45
+
46
+ detector = dlib.get_frontal_face_detector()
47
+ predictor = dlib.shape_predictor(PREDICTOR_PATH)
48
+
49
+ class TooManyFaces(Exception):
50
+ pass
51
+
52
+ class NoFaces(Exception):
53
+ pass
54
+
55
+ def get_landmarks(im):
56
+ rects = detector(im, 1)
57
+
58
+ if len(rects) > 1:
59
+ raise TooManyFaces
60
+ if len(rects) == 0:
61
+ raise NoFaces
62
+
63
+ return np.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])
64
+
65
+ def read_imgURL(URL):
66
+ with urllib.request.urlopen(URL) as url:
67
+ with open('temp.jpg', 'wb') as f:
68
+ f.write(url.read())
69
+
70
+ img = Image.open('temp.jpg')
71
+ img = np.array(img)
72
+ return img
73
+
74
+ def draw_convex_hull(im, points, color):
75
+ points = cv2.convexHull(points)
76
+ cv2.fillConvexPoly(im, points, color=color)
77
+
78
+ def get_face_mask(im, landmarks):
79
+ im = np.zeros(im.shape[:2], dtype=np.float64)
80
+
81
+ for group in OVERLAY_POINTS:
82
+ draw_convex_hull(im,
83
+ landmarks[group],
84
+ color=1)
85
+
86
+ im = np.array([im, im, im]).transpose((1, 2, 0))
87
+
88
+ im = (cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0) > 0) * 1.0
89
+ im = cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0)
90
+
91
+ return im
92
+
93
+
94
+ def read_im_and_landmarks(fname):
95
+ im = np.array(fname)
96
+ im = cv2.resize(im, (im.shape[1] * SCALE_FACTOR,
97
+ im.shape[0] * SCALE_FACTOR))
98
+ s = get_landmarks(im)
99
+ return im, s
100
+
101
+ def warp_im(im, M, dshape):
102
+ output_im = np.zeros(dshape, dtype=im.dtype)
103
+ cv2.warpAffine(im,
104
+ M[:2],
105
+ (dshape[1], dshape[0]),
106
+ dst=output_im,
107
+ borderMode=cv2.BORDER_TRANSPARENT,
108
+ flags=cv2.WARP_INVERSE_MAP)
109
+ return output_im
110
+
111
+ def infer_chin_region(eye, width_ratio, down_ratio, left_or_right):
112
+ region1 = [0] * 4
113
+ if left_or_right == 'right': #assuming it is the absolute right chin
114
+ region1[0] = int(max(0, int(eye[0] - 0.5 * eye[2]))) #chin region should go lefwards
115
+ region1[2] = int(0.5 * eye[2])
116
+ else: # assuming it is the absolute left chin
117
+ region1[0] = int(eye[0] + eye[2]) # chin region should go rightwards
118
+ region1[2] = int(0.5 * eye[2])
119
+ region1[1] = int(eye[1] + eye[3])
120
+ region1[3] = int(1.5 * eye[3])
121
+
122
+ return region1
123
+
124
+ def detect_face_direction(gray, face, eye, down_ratio, chin_width_ratio):
125
+ region1 = [0] * 4 # assuming this is the left eye, forhead should go rightward
126
+ region2 = [0] * 4 # assuming this is the right eye, forhead should go leftward
127
+ print(eye[0])
128
+ region1 = infer_chin_region(eye[0], chin_width_ratio, down_ratio, 'left') #region1 is from eye to right
129
+ region2 = infer_chin_region(eye[0], chin_width_ratio, down_ratio, 'right') # region2 is from eye to left
130
+
131
+ std1 = np.std(gray[region1[1]:(region1[1]+region1[3]), region1[0]:(region1[0]+region1[2])])
132
+ std2 = np.std(gray[region2[1]:(region2[1]+region2[3]), region2[0]:(region2[0]+region2[2])])
133
+ face_direction = ""
134
+
135
+ if std1 > std2: #eye right has higher variance than eye left
136
+ face_direction = "right"
137
+ else:
138
+ face_direction = "left"
139
+ return face_direction
140
+
141
+ def extract_cheek_region(face_x_min, face_x_max, face_y_max, eye_landmarks, left_or_right):
142
+ if left_or_right == "Left":
143
+ cheek_region_min_x = eye_landmarks[0,0]
144
+ cheek_region_max_x = int(face_x_max - 0.05 * (face_x_max - min(eye_landmarks[:,0])))
145
+ else:
146
+ cheek_region_max_x = max(eye_landmarks[:,0])[0,0]
147
+ #print (max(eye_landmarks[:,0])[0,0])
148
+ #cheek_region_max_x = max(eye_landmarks[:, 0])
149
+ cheek_region_min_x = int(face_x_min + 0.1 * (cheek_region_max_x - face_x_min))
150
+ cheek_region_min_y = int(max(eye_landmarks[:,1]) + 0.2 * (max(eye_landmarks[:,1]) - min(eye_landmarks[:,1])))
151
+ cheek_region_max_y = int(face_y_max - 0.1 * (face_y_max - max(eye_landmarks[:,1])))
152
+ return [cheek_region_min_x, cheek_region_min_y, cheek_region_max_x, cheek_region_max_y]
153
+
154
+ def extract_patches(imagefile, dimension_dict, face_loc_dict, image_dim, croppedFaces_Dir):
155
+
156
+ imageName = "temp"
157
+
158
+ img, landmarks = read_im_and_landmarks(imagefile)
159
+ face_detected = True
160
+
161
+ img_height, img_width = img.shape[0:2]
162
+ image_dim = [img_height, img_width]
163
+ min_dim = min(img_height, img_width)
164
+ min_face_size = min(min_dim * 0.2, min_dim * 0.2)
165
+ min_eye = min_face_size * 0.2
166
+ min_eye_area = min_eye ** 2
167
+
168
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
169
+
170
+ if face_detected:
171
+ mask = get_face_mask(img, landmarks)
172
+ face_x_min = int(max(0, np.asarray(min(landmarks[:,0])).flatten()[0]))
173
+ face_x_max = int(min(img_width, np.asarray(max(landmarks[:,0])).flatten()[0]))
174
+ face_y_min = int(max(0, np.asarray(min(landmarks[:,1])).flatten()[0]))
175
+ face_y_max = int(min(img_height, np.asarray(max(landmarks[:,1])).flatten()[0]))
176
+ face_loc_dict['face_loc'] = [face_x_min, face_x_max, face_y_min, face_y_max]
177
+ face_height = face_y_max - face_y_min
178
+ forehead_height = int(face_height * forehead_ratio)
179
+ new_face_y_min = max(0, face_y_min - forehead_height)
180
+ right_brow_landmarks = landmarks[RIGHT_BROW_POINTS,:]
181
+ left_brow_landmarks = landmarks[LEFT_BROW_POINTS,:]
182
+ right_eye_landmarks = landmarks[RIGHT_EYE_POINTS,:]
183
+ left_eye_landmarks = landmarks[LEFT_EYE_POINTS,:]
184
+ mouse_landmarks = landmarks[MOUTH_POINTS,:]
185
+ ########################
186
+ # Get the forehead patch
187
+ ########################
188
+ [right_brow_min_x, left_brow_max_x] = \
189
+ [max(0, np.min(np.array(right_brow_landmarks[:,0]))), min(img_width, np.max(np.array(left_brow_landmarks[:,0])))]
190
+ brow_min_y = min(np.min(np.array(right_brow_landmarks[:,1])),np.min(np.array(left_brow_landmarks[:,1])))
191
+ forehead_x_min = right_brow_min_x
192
+ forehead_x_max = left_brow_max_x
193
+ forehead_y_min = max(0, brow_min_y - forehead_height)
194
+ forehead_y_max = min(brow_min_y, forehead_y_min + forehead_height)
195
+ forehead_region = img[forehead_y_min:forehead_y_max, forehead_x_min:forehead_x_max, :]
196
+ #print ('forehead dim (x_min, x_max, y_min, y_max): %i,%i, %i, %i' % (forehead_x_min, forehead_x_max, forehead_y_min, forehead_y_max))
197
+ key_name = 'landmark_fh'
198
+ dimension_dict[key_name] = [forehead_x_min, forehead_x_max, forehead_y_min, forehead_y_max]
199
+ forehead_file_name = join(croppedFaces_Dir, key_name +".jpg")
200
+ #forehead_region = cv2.cvtColor(forehead_region, cv2.COLOR_BGR2RGB)
201
+ imageio.imwrite(forehead_file_name, forehead_region)
202
+
203
+ chin_x_min = np.max(np.array(right_eye_landmarks[:,0]))
204
+ chin_x_max = np.min(np.array(left_eye_landmarks[:,0]))
205
+ chin_y_min = np.max(np.array(mouse_landmarks[:,1]))
206
+ chin_y_max = face_y_max
207
+ chin_region = img[chin_y_min:chin_y_max, chin_x_min:chin_x_max, :]
208
+ #print ('chin dim (x_min, x_max, y_min, y_max): %i,%i, %i, %i' % (chin_x_min, chin_x_max, chin_y_min, chin_y_max))
209
+ key_name = 'landmark_chin'
210
+ dimension_dict[key_name] = [chin_x_min, chin_x_max, chin_y_min, chin_y_max]
211
+ chin_file_name = join(croppedFaces_Dir, key_name +".jpg")
212
+ #chin_region = cv2.cvtColor(chin_region, cv2.COLOR_BGR2RGB)
213
+ imageio.imwrite(chin_file_name, chin_region)
214
+
215
+ ##########################
216
+ # Get the cheeks patch
217
+ ##########################
218
+ # Decide whether it is a side view or not
219
+ left_eye_width = np.max(np.array(left_eye_landmarks[:,0])) - np.min(np.array(left_eye_landmarks[:,0]))
220
+ right_eye_width = np.max(np.array(right_eye_landmarks[:,0])) - np.min(np.array(right_eye_landmarks[:,0]))
221
+ right_face = True
222
+ left_face = True
223
+ if float(right_eye_width) / float(left_eye_width) >= 1.15: # right eye is bigger than left eye, showing the right face
224
+ left_face = False
225
+ elif float(left_eye_width) / float(right_eye_width) >= 1.15: # left eye is bigger than right eye, showing the left face
226
+ right_face = False
227
+
228
+ if right_face:
229
+ right_cheek_region = extract_cheek_region(face_x_min, face_x_max, face_y_max, right_eye_landmarks, "Right")
230
+ cheek_region = img[right_cheek_region[1]:right_cheek_region[3], right_cheek_region[0]:right_cheek_region[2], :]
231
+ #print ('right cheek dim (x_min, x_max, y_min, y_max): %i,%i, %i, %i' % (right_cheek_region[0], right_cheek_region[2], right_cheek_region[1], right_cheek_region[3]))
232
+ key_name = 'landmark_rc'
233
+ dimension_dict[key_name] = [right_cheek_region[0], right_cheek_region[2], right_cheek_region[1], right_cheek_region[3]]
234
+ cheek_file_name = join(croppedFaces_Dir, key_name +".jpg")
235
+ #cheek_region = cv2.cvtColor(cheek_region, cv2.COLOR_BGR2RGB)
236
+ imageio.imwrite(cheek_file_name, cheek_region)
237
+ if left_face:
238
+ left_cheek_region = extract_cheek_region(face_x_min, face_x_max, face_y_max, left_eye_landmarks, "Left")
239
+ cheek_region = img[left_cheek_region[1]:left_cheek_region[3], left_cheek_region[0]:left_cheek_region[2], :]
240
+ #print ('left cheek dim (x_min, x_max, y_min, y_max): %i,%i, %i, %i' % (left_cheek_region[0], left_cheek_region[2], left_cheek_region[1], left_cheek_region[3]))
241
+ key_name = 'landmark_lc'
242
+ dimension_dict[key_name] = [left_cheek_region[0], left_cheek_region[2], left_cheek_region[1], left_cheek_region[3]]
243
+ cheek_file_name = join(croppedFaces_Dir, key_name +".jpg")
244
+ #cheek_region = cv2.cvtColor(cheek_region, cv2.COLOR_BGR2RGB)
245
+ imageio.imwrite(cheek_file_name, cheek_region)
246
+
247
+
248
+ if not face_detected:
249
+ print("Face not detected by landmarks model...")
250
+ # Use the OneEye model to detect one eye, and infer the face region based on the eye location
251
+ eye_detected = False
252
+ roi_gray = gray
253
+ roi_color = img
254
+ roi_color = cv2.cvtColor(roi_color, cv2.COLOR_BGR2RGB)
255
+ eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 5)
256
+ max_area = 0
257
+ eye_count = 0
258
+ max_index = 0
259
+
260
+ for (ex,ey,ew,eh) in eyes: # there might be multiple eyes detected. Choose the biggest one
261
+ if ew*eh >= max_area and ex >= img_width * 0.1 and ex <= img_width * 0.9:
262
+ max_area = ew*eh
263
+ max_index = eye_count
264
+ eye_count += 1
265
+ if max_area >= min_eye_area:
266
+ eye_detected = True
267
+ (ex, ey, ew, eh) = eyes[max_index]
268
+ if float(ew) / float(img_width) > 0.15 or float(eh) / float(img_height) > 0.15: # detected eye too large
269
+ # resize the detected eye
270
+ center_x = ex + ew/2
271
+ center_y = ey + eh/2
272
+ resized_w = min(img_width * 0.15, img_height * 0.15)
273
+ ex = int(center_x - resized_w/2)
274
+ ey = int(center_y - resized_w/2)
275
+ ew = int(resized_w)
276
+ eh = int(resized_w)
277
+ eyes1 = np.array([ex, ey, resized_w, resized_w]).reshape((1,4))
278
+ else:
279
+ eyes1 = np.array(eyes[max_index]).reshape((1,4))
280
+ face1 = np.array(())
281
+ face_direction = detect_face_direction(gray, face1, eyes1, down_ratio, chin_width_ratio)
282
+ if face_direction == "left":
283
+ print("Left eye detected")
284
+ face_min_x = eyes1[0, 0]
285
+ face_max_x = min(img_width, int(eyes1[0,0] + (chin_width_ratio + 0.5) * eyes1[0, 2]))
286
+ forehead_max_x = min(img_width, int(eyes1[0,0] + width_ratio * eyes1[0, 2]))
287
+ forehead_min_x = face_min_x
288
+ cheek_min_x = int(eyes1[0, 0] + 0.5 * eyes1[0,2])
289
+ cheek_max_x = face_max_x
290
+ else:
291
+ print("Right eye detected")
292
+ face_min_x = max(0, int(eyes1[0, 0] - chin_width_ratio * eyes1[0, 2]))
293
+ face_max_x = eyes1[0, 0] + eyes1[0, 2]
294
+ forehead_min_x = max(0, int(eyes1[0, 0] - width_ratio * eyes1[0, 2]))
295
+ forehead_max_x = min(img_width, int(eyes1[0, 0] + width_ratio * eyes1[0, 2]))
296
+ cheek_max_x = int(eyes1[0,0] + 0.5*eyes1[0,2])
297
+ cheek_min_x = face_min_x
298
+ forehead_min_y = max(0, int(eyes1[0, 1] - top_ratio * eyes1[0,3]))
299
+ forehead_max_y = max(0, int(eyes1[0, 1] - 0.5 * eyes1[0, 3]))
300
+ forehead_ok = False
301
+ # Get the forehead region
302
+ if forehead_max_y - forehead_min_y >= 0.7 * eyes1[0, 3]:
303
+ forehead_ok = True
304
+ forehead_region = img[forehead_min_y:forehead_max_y, forehead_min_x: forehead_max_x, :]
305
+ #print ('forehead dim (x_min, x_max, y_min, y_max): %i,%i, %i, %i' % (forehead_min_x, forehead_max_x, forehead_min_y, forehead_max_y))
306
+ key_name = 'oneeye_fh'
307
+ dimension_dict[key_name] = [forehead_min_x, forehead_max_x, forehead_min_y, forehead_max_y]
308
+ forehead_file_name = join(croppedFaces_Dir, key_name +".jpg")
309
+ imageio.imwrite(forehead_file_name, forehead_region)
310
+ # Get the cheek region
311
+ cheek_min_y = int(eyes1[0, 1] + eyes1[0, 3])
312
+ cheek_max_y = min(img_height, int(eyes1[0, 1] + down_ratio * eyes1[0, 3]))
313
+ cheek_region = img[cheek_min_y: cheek_max_y, cheek_min_x: cheek_max_x, :]
314
+ #print ('cheek dim (x_min, x_max, y_min, y_max): %i,%i, %i, %i' % (cheek_min_x, cheek_max_x, cheek_min_y, cheek_max_y))
315
+ key_name = 'oneeye_cheek'
316
+ dimension_dict[key_name] = [cheek_min_x, cheek_max_x, cheek_min_y, cheek_max_y]
317
+ face_loc_dict['face_loc'] = [face_min_x, face_max_x, forehead_min_y, cheek_max_y]
318
+ #cheek_region = cv2.cvtColor(cheek_region, cv2.COLOR_BGR2RGB)
319
+ if face_direction == "left":
320
+ cheek_file_name = join(croppedFaces_Dir, key_name +".jpg")
321
+ elif face_direction == "right":
322
+ cheek_file_name = join(croppedFaces_Dir, key_name +".jpg")
323
+ else:
324
+ cheek_file_name = join(croppedFaces_Dir, key_name +".jpg")
325
+ imageio.imwrite(cheek_file_name, cheek_region)
326
+
327
+
328
+ if (not face_detected) and (not eye_detected):
329
+ print("No chin or forehead detected, output the original file %s.jpg"%imageName)
330
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
331
+ outfile = join(croppedFaces_Dir, imageName+".jpg")
332
+ imageio.imwrite(outfile, img)
333
+
334
+ return dimension_dict, face_loc_dict, image_dim
index.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import get_patches
3
+ import model
4
+ from PIL import Image
5
+ import numpy as np
6
+ import os
7
+ import shutil
8
+ import openai
9
+ from dotenv import load_dotenv
10
+ load_dotenv()
11
+
12
+ def convert_image(file_path):
13
+ img = Image.open(file_path)
14
+ img_re = img.resize((128, 128))
15
+ numpy_arr = np.asarray(img_re)
16
+ return numpy_arr
17
+
18
+ def predict(image):
19
+ def extract_patches(image):
20
+ patch_path = 'patches'
21
+ dimension_dict = dict()
22
+ face_dict = dict()
23
+ image_dim = []
24
+ # try:
25
+ dim, face, img = get_patches.extract_patches(
26
+ image, dimension_dict, face_dict, image_dim, patch_path)
27
+ print("extract patches pass")
28
+ return dim, face, img
29
+ # except:
30
+ # print("extract patches fail")
31
+ # return None, None, None
32
+
33
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
34
+ os.mkdir(os.path.join(BASE_DIR, 'patches'))
35
+
36
+ dim, face, img = extract_patches(image)
37
+
38
+ if dim is None and face is None and img is None:
39
+ return json.dumps({"msg": "fail"})
40
+
41
+ model_path = os.path.join(
42
+ BASE_DIR, 'Acne_Classifyer_N_Resnet.h5')
43
+
44
+ resnet_model = model.load_trained_model(model_path)
45
+ imageFiles = [os.path.join(BASE_DIR, 'patches', f)
46
+ for f in os.listdir(os.path.join(BASE_DIR, 'patches'))]
47
+
48
+ images = []
49
+ landmarks = []
50
+ for f in imageFiles:
51
+ if f.endswith('.jpg'):
52
+ landmark = f.split('/')[-1].split('_')[-1]
53
+ landmarks.append(landmark)
54
+ images.append(convert_image(f))
55
+
56
+ images = np.array(images)
57
+
58
+ prediction = resnet_model.predict(images)
59
+ prediction = prediction.tolist()
60
+ shutil.rmtree(os.path.join(BASE_DIR, 'patches'))
61
+ return {"msg": "success", "prediction": prediction, "landmarks": landmarks}
62
+
63
+
64
+ def recommendation(response):
65
+ openai.api_key = os.getenv("OPENAI_API_KEY")
66
+ json = response
67
+
68
+ if json.get('msg') == "success":
69
+ prediction = json.get('prediction')
70
+ prediction = np.array(prediction)
71
+ prediction = prediction.argmax(axis=1)
72
+
73
+ landmarks = json.get('landmarks')
74
+
75
+ output = {landmarks[i].split(
76
+ '.')[0]: f"level{prediction[i]}" for i in range(len(landmarks))}
77
+ output['age'] = json.get('age')
78
+ output['gender'] = json.get('gender')
79
+ output['skin_type'] = json.get('skin_type')
80
+ output['allergies'] = json.get('allergies')
81
+ output['diet'] = json.get('diet')
82
+ completion = openai.ChatCompletion.create(
83
+ model="gpt-3.5-turbo",
84
+ messages=[
85
+ {"role": "system", "content": "you are a medical AI assistant that assists patients with acne on face with getting rid of it, okay? don't give me tips just nod"},
86
+
87
+ {"role": "user", "content": "what do you know about acne severity levels like level 0, level 1 and level 2 ?"},
88
+
89
+ {"role": "assistant", "content": """Acne severity levels are used to classify the severity of acne, with level 0 being the mildest and level 4 being the most severe. However, the commonly used system usually consists of four levels, which are described as follows:
90
+ Level 0: This level is used to describe skin with no signs of acne. The skin is generally clear with no visible pimples or blemishes.
91
+ Level 1: This level is used to describe skin with minor acne. The skin may have some blackheads and whiteheads, but they are not widespread and there are no visible pimples.
92
+ Level 2: This level is used to describe skin with moderate acne. The skin may have more blackheads and whiteheads, as well as a few visible pimples.
93
+ Level 3: This level is used to describe skin with severe acne. The skin may have many blackheads, whiteheads, and visible pimples, as well as some nodules and cysts.
94
+ It is worth noting that acne severity can vary greatly from person to person, and there may be other factors to consider when assessing the severity of acne, such as the presence of scarring or inflammation. Therefore, it is important to consult with a dermatologist or healthcare professional for an accurate diagnosis and personalized treatment plan."""},
95
+
96
+ {"role": "user", "content": "okay let me teach u something new"},
97
+
98
+ {"role": "assistant", "content": "Sure, I'm always open to learning something new! What would you like to teach me?"},
99
+
100
+ {"role": "user", "content": """so level 0 is This level is used to describe skin with no signs of acne. The skin is generally clear with no visible pimples or blemishes.
101
+ level 1 This level is used to describe skin with minor acne. The skin may have some blackheads and whiteheads, but they are not widespread and there are no visible pimples.
102
+ level 2 This level is used to describe skin with moderate acne to severe acne. The skin may have few to many blackheads and whiteheads, as well as a few or many visible pimples, as well as some nodules and cysts. there does not exist any other level these 3 are the only levels you know about okay"""},
103
+
104
+ {"role": "assistant", "content": """I apologize for the error in my previous response. You are correct that the commonly used acne severity classification system consists of three levels, not four. Here is the correct information:
105
+ Level 0: This level is used to describe skin with no signs of acne. The skin is generally clear with no visible pimples or blemishes.
106
+ Level 1: This level is used to describe skin with mild acne. The skin may have some blackheads and whiteheads, but they are not widespread and there are no visible pimples.
107
+ Level 2: This level is used to describe skin with moderate to severe acne. The skin may have many blackheads and whiteheads, as well as a few or many visible pimples, as well as some nodules and cysts.
108
+ Thank you for bringing this to my attention and allowing me to correct my mistake."""},
109
+
110
+ {"role": "user", "content": "okay now i will tell you some instructions and the responses with boudries to which you will adhere"},
111
+
112
+ {"role": "assistant", "content": "Sure, please go ahead and give me the instructions and boundaries to adhere to."},
113
+
114
+ {"role": "user",
115
+ "content": """so we have divided a face into patches these are namely fh(forehead), lc(left cheek), rc(right cheek), ch(chin). Each time my system will analyze the face image extract the patches from it and will send them to you along with some other info of the user which are Age, Gender(Male, Female, other), Skin-Type(Oily, Dry, Normal), Allergies(salicylic acid, benzoyl peroxide, Sun-exposure, Itching, Swelling, Redness), Diet(Veg, Non-veg) these are a total of 5 meta-data values about the person or individual for whom you will generate the Diet, Daily Skin-care Routine, it is not necessary that each time you will be provided with all the 4 mentioned patches above(but you will always be provided with the 5 meta-data values of the individual) some time the message may contain only 1 or 2 patches. I will also send the acne severity level associated with that patch like {'fh':'level0', 'lc':'level2', 'rc':'level2', 'chin':'level0', 'age':20, 'gender':'male', 'skin':'oily', 'allergies':['itching'], 'diet':'veg'} this json data means that the patch forehead has level0 acne and left cheek has level2 acne and he is of age 20 and he has oily skin and have itching allergy and his diet is veg. Now that you now what level of acne does the parts of a particular face has you will generate a week long custom made remedy, diet, good habits, bad habits and daily skincare routine for the acne treatment. I can ask you to generate a general acne routine and remedy but i am providing you with this classified details so that the output you will generate will be more optimized and personalized. okay that's it if you understand completely what i just instructed to you then generate a sample response for me for the above example data. also next time i will not be sending such long detailed message just the json message that i sent above so you will not get confused and do the task as i told you. also the response should be in a raw mark up language format the language should be raw html with <br> tag at places where line break is necessary so i can copy it as it is and use it some where else. Also i need you to generate answers in way that they don't sound like they are being generated by a chat bot, leave out any sentences like "Sure, here's a customized routine for the given example:" so you need to sound like the answers are being generated by a deep learning NLP model not by a chat bot. You don't need to greet to me in the response just straight out tell the answer don't greet to me like 'here is the routine for the example' or anything like that."""},
116
+
117
+ {"role": "assistant", "content": """
118
+ <h3>Skincare Routine:</h3>
119
+ <ul>
120
+ <li>Wash your face with a gentle cleanser twice a day.</li>
121
+ <li>Use an alcohol-free toner after cleansing to balance your skin.</li>
122
+ <li>Apply a moisturizer with SPF in the morning.</li>
123
+ <li>Apply a retinoid cream at night to help unclog pores.</li>
124
+ <li>Use a clay mask once a week to draw out impurities.</li>
125
+ </ul>
126
+ <h3>Diet:</h3>
127
+ <p>Since the individual has oily skin, it's recommended to follow a low-glycemic index diet which includes more fruits, vegetables, whole grains, and lean proteins. Also, since they have a vegetarian diet, they can consume more nuts, seeds, and legumes as sources of protein. It's also important to drink plenty of water and avoid sugary and processed foods as they can cause inflammation in the body and lead to acne breakouts.</p>
128
+ <h3>Good Habits:</h3>
129
+ <ul>
130
+ <li>Keep your hands off your face to prevent bacteria transfer.</li>
131
+ <li>Change your pillowcases regularly to avoid buildup of oils and bacteria.</li>
132
+ <li>Avoid touching or picking at acne as it can lead to scarring.</li>
133
+ <li>Wash your makeup brushes regularly to avoid buildup of bacteria.</li>
134
+ <li>Wear sunscreen every day to protect your skin from harmful UV rays.</li>
135
+ </ul>
136
+ <h3>Bad Habits:</h3>
137
+ <ul>
138
+ <li>Avoid smoking as it can cause premature aging and damage to the skin.</li>
139
+ <li>Avoid excessive alcohol consumption as it can dehydrate the skin.</li>
140
+ <li>Avoid using harsh scrubs or exfoliants as they can irritate the skin.</li>
141
+ <li>Avoid using products with salicylic acid or benzoyl peroxide if the individual has an allergy to them.</li>
142
+ <li>Avoid consuming processed and sugary foods as they can cause inflammation and lead to acne breakouts.</li>
143
+ </ul>
144
+ <h3>Additional Recommendations:</h3>
145
+ <p>Since the individual has an itching allergy, it's recommended to avoid products with fragrance and other potential irritants. Also, since they have oily skin, it's recommended to use non-comedogenic and oil-free products to avoid clogging pores.</p>"""},
146
+
147
+ {"role": "user",
148
+ "content": "{'fh':'level2', 'lc':'level1', 'rc':'level0', 'chin':'level2', 'age':29, 'gender':'female', 'skin':'dry', 'allergies':['itching', 'swelling', 'sun-exposure', 'redness'], 'diet':'non-veg'"},
149
+
150
+ {"role": "assistant", "content": """
151
+ <h3>Week-long Acne Treatment Plan</h3>
152
+ <p>Age: 29 | Gender: Female | Skin Type: Dry | Allergies: Itching,Swelling,Sun-exposure,Redness | Diet: Non-Veg</p>
153
+ <h4>Daily Skincare Routine:</h4>
154
+ <ul>
155
+ <li>Wash your face twice a day using a gentle, non-comedogenic cleanser.</li>
156
+ <li>Apply a moisturizer with SPF 30 or higher to protect your skin from sun damage.</li>
157
+ <li>Use a toner containing salicylic acid to help unclog pores and reduce acne.</li>
158
+ <li>Apply a spot treatment containing benzoyl peroxide to acne-prone areas.</li>
159
+ </ul>
160
+ <h4>Diet Recommendations:</h4>
161
+ <ul>
162
+ <li>Include foods rich in vitamin A, such as sweet potatoes, carrots, and leafy greens, which can help reduce inflammation and promote healthy skin.</li>
163
+ <li>Avoid dairy products and high-glycemic foods, such as white bread and sugary snacks, which may worsen acne.</li>
164
+ <li>Incorporate foods rich in omega-3 fatty acids, such as salmon and walnuts, which can help reduce inflammation and improve skin health.</li>
165
+ </ul>
166
+ <h4>Good Habits:</h4>
167
+ <ul>
168
+ <li>Get enough sleep each night to help reduce stress, which can trigger acne.</li>
169
+ <li>Drink plenty of water to stay hydrated and flush out toxins.</li>
170
+ <li>Exercise regularly to help reduce stress and improve overall health.</li>
171
+ </ul>
172
+ <h4>Bad Habits to Avoid:</h4>
173
+ <ul>
174
+ <li>Avoid touching your face, as this can transfer oil and bacteria to the skin and worsen acne.</li>
175
+ <li>Avoid using harsh scrubs or exfoliants, which can irritate the skin and worsen acne.</li>
176
+ <li>Avoid picking or squeezing pimples, as this can lead to scarring and further breakouts.</li>
177
+ </ul>
178
+ <h4>Acne Treatment:</h4>
179
+ <p>Based on the severity of acne on different patches of the face:</p>
180
+ <ul>
181
+ <li>For level 0 acne on fh, chin: continue with daily skincare routine and good habits.</li>
182
+ <li>For level 1 acne on lc: use an over-the-counter retinoid cream in the evening to help unclog pores and reduce acne.</li>
183
+ <li>For level 2 acne on fh, rc, chin: consult a dermatologist for prescription-strength topical or oral medications.</li>
184
+ </ul>"""},
185
+ {"role": "user", "content": f"{output}"},
186
+ ]
187
+ )
188
+
189
+ return completion
190
+ return ""
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import os
3
+
4
+ resnet101 = tf.keras.applications.ResNet101(include_top=False, weights="imagenet")
5
+
6
+ for layer in resnet101.layers:
7
+ layer.trainable = False
8
+ if layer.name == "conv3_block1_1_conv":
9
+ break
10
+
11
+ IMG_HEIGHT = 128
12
+ IMG_WIDTH = 128
13
+ IMG_CHANNELS = 3
14
+
15
+ def load_trained_model(weights_path):
16
+ input = tf.keras.layers.Input(shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS))
17
+
18
+ # data augmentation
19
+ a1 = tf.keras.layers.experimental.preprocessing.RandomRotation(0.4)(input)
20
+ a2 = tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical")(a1)
21
+
22
+ # resnet
23
+ out = resnet101(a2)
24
+ x = tf.keras.layers.GlobalMaxPooling2D()(out)
25
+
26
+ # dense layer
27
+ d1 = tf.keras.layers.Dense(256, activation="relu")(x)
28
+ d2 = tf.keras.layers.Dense(128, activation="relu")(d1)
29
+ d3 = tf.keras.layers.Dense(64, activation="relu")(d2)
30
+ prediction = tf.keras.layers.Dense(3, activation="softmax")(d3)
31
+
32
+ model = tf.keras.Model(inputs=input, outputs=prediction)
33
+ model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
34
+
35
+ model.load_weights(weights_path)
36
+ return model
requirements.txt ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==1.4.0
2
+ aiofiles==23.1.0
3
+ aiohttp==3.8.4
4
+ aiosignal==1.3.1
5
+ altair==4.2.2
6
+ anyio==3.6.2
7
+ astunparse==1.6.3
8
+ async-timeout==4.0.2
9
+ attrs==22.2.0
10
+ autopep8==2.0.2
11
+ cachetools==5.3.0
12
+ certifi==2022.12.7
13
+ charset-normalizer==3.1.0
14
+ click==8.1.3
15
+ contourpy==1.0.7
16
+ cycler==0.11.0
17
+ dlib==19.24.1
18
+ entrypoints==0.4
19
+ fastapi==0.95.0
20
+ ffmpy==0.3.0
21
+ filelock==3.11.0
22
+ Flask==2.2.3
23
+ flatbuffers==23.3.3
24
+ fonttools==4.39.3
25
+ frozenlist==1.3.3
26
+ fsspec==2023.3.0
27
+ gast==0.4.0
28
+ google-auth==2.17.2
29
+ google-auth-oauthlib==1.0.0
30
+ google-pasta==0.2.0
31
+ gradio==3.24.1
32
+ gradio_client==0.0.8
33
+ grpcio==1.53.0
34
+ h11==0.14.0
35
+ h5py==3.8.0
36
+ httpcore==0.16.3
37
+ httpx==0.23.3
38
+ huggingface-hub==0.13.4
39
+ idna==3.4
40
+ imageio==2.27.0
41
+ itsdangerous==2.1.2
42
+ jax==0.4.8
43
+ Jinja2==3.1.2
44
+ jsonschema==4.17.3
45
+ keras==2.12.0
46
+ kiwisolver==1.4.4
47
+ lazy_loader==0.2
48
+ libclang==16.0.0
49
+ linkify-it-py==2.0.0
50
+ Markdown==3.4.3
51
+ markdown-it-py==2.2.0
52
+ MarkupSafe==2.1.2
53
+ matplotlib==3.7.1
54
+ mdit-py-plugins==0.3.3
55
+ mdurl==0.1.2
56
+ ml-dtypes==0.0.4
57
+ multidict==6.0.4
58
+ networkx==3.1
59
+ numpy==1.23.5
60
+ oauthlib==3.2.2
61
+ openai==0.27.4
62
+ opencv-python==4.7.0.72
63
+ opt-einsum==3.3.0
64
+ orjson==3.8.9
65
+ packaging==23.0
66
+ pandas==2.0.0
67
+ Pillow==9.5.0
68
+ protobuf==4.22.1
69
+ pyasn1==0.4.8
70
+ pyasn1-modules==0.2.8
71
+ pycodestyle==2.10.0
72
+ pydantic==1.10.7
73
+ pydub==0.25.1
74
+ pyparsing==3.0.9
75
+ pyrsistent==0.19.3
76
+ python-dateutil==2.8.2
77
+ python-dotenv==1.0.0
78
+ python-multipart==0.0.6
79
+ pytz==2023.3
80
+ PyWavelets==1.4.1
81
+ PyYAML==6.0
82
+ requests==2.28.2
83
+ requests-oauthlib==1.3.1
84
+ rfc3986==1.5.0
85
+ rsa==4.9
86
+ scikit-image==0.20.0
87
+ scipy==1.10.1
88
+ semantic-version==2.10.0
89
+ six==1.16.0
90
+ sniffio==1.3.0
91
+ starlette==0.26.1
92
+ tensorboard==2.12.1
93
+ tensorboard-data-server==0.7.0
94
+ tensorboard-plugin-wit==1.8.1
95
+ tensorflow==2.12.0
96
+ tensorflow-estimator==2.12.0
97
+ tensorflow-io-gcs-filesystem==0.32.0
98
+ termcolor==2.2.0
99
+ tifffile==2023.3.21
100
+ tomli==2.0.1
101
+ toolz==0.12.0
102
+ tqdm==4.65.0
103
+ typing_extensions==4.5.0
104
+ tzdata==2023.3
105
+ uc-micro-py==1.0.1
106
+ urllib3==1.26.15
107
+ uvicorn==0.21.1
108
+ websockets==11.0.1
109
+ Werkzeug==2.2.3
110
+ wrapt==1.14.1
111
+ yarl==1.8.2