-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultFront.py
More file actions
481 lines (371 loc) · 16.1 KB
/
ResultFront.py
File metadata and controls
481 lines (371 loc) · 16.1 KB
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
473
474
475
476
477
478
479
480
481
import imutils
import mediapipe as mp
from torchvision import transforms
import torch
from PIL import Image
import numpy as np
import dlib
import cv2
import numpy as np
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
from firebase_admin import firestore
from uuid import uuid4
from os import system
# 얼굴형 분류해서 return 에는 얼굴형 인덱스가 출력될 것임!
def faceline(image_front, PATH):
class_names = ["각진형", "계란형 ", "둥근형", "마름모형", "하트형"]
transforms_test = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
device = torch.device("cpu")
# 모델 초기화
model = torch.load(PATH, map_location=torch.device('cpu'))
# 모델 불러오기
# 드롭아웃 및 배치 정규화를 평가 모드로 설정
model.eval()
# 이미지 불러오기
image = transforms_test(image_front).unsqueeze(0).to(device)
# 불러온 이미지를 얼굴형 분류 모델에 집어넣기
with torch.no_grad():
outputs = model(image)
_, preds = torch.max(outputs, 1)
num = preds[0].tolist()
return num
def face_detection(image_front):
gray = cv2.cvtColor(image_front, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
for face in rects:
shape = predictor(gray, face)
list_points = []
for p in shape.parts():
list_points.append([p.x, p.y])
list_points = np.array(list_points)
for i, pt in enumerate(list_points[ALL]):
pt_pos = (pt[0], pt[1])
cv2.circle(image_front, pt_pos, 2, (0, 255, 0), -1)
center = (list_points[NOSE][6] - -list_points[RIGHT_EYEBROW][4])[1]
low = (list_points[JAWLINE][8] - list_points[NOSE][6])[1]
return center, low, list_points
def hair_up (img1,list_points):
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
def get_head_mask(img):
"""
Get the mask of the head
Cuting BG
:param img: source image
:return: Returns the mask with the cut out BG
"""
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
faces = faceCascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50)) # Find faces
if len(faces) != 0:
x, y, w, h = faces[0]
(x, y, w, h) = (x - 40, y - 100, w + 80, h + 200)
rect1 = (x, y, w, h)
cv2.grabCut(img1, mask, rect1, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) # Crop BG around the head
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') # Take the mask from BG
return mask2
def is_bold(pnt, hair_mask):
"""
Check band or not
:param pnt: The upper point of the head
:param hair_mask: Mask with hair
:return: True if Bald, else False
"""
roi = hair_mask[pnt[1]:pnt[1] + 40, pnt[0] - 40:pnt[0] + 40] # Select the rectangle under the top dot
cnt = cv2.countNonZero(roi) # Count the number of non-zero points in this rectangle
# If the number of points is less than 25%, then we think that the head is bald
# print("cntttt", cnt)
if cnt < 0:
# print("Bald human on photo")
return True
else:
# print("Not Bold")
return False
mask = get_head_mask(img1)
cnts = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
cnt = cnts[0]
topmost = tuple(cnt[cnt[:, :, 1].argmin()][0])
lower = np.array([0, 0, 100], dtype="uint8") # Lower limit of skin color
upper = np.array([255, 255, 255], dtype="uint8") # Upper skin color limit
converted = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # We translate into HSV color format
skinMask = cv2.inRange(converted, lower, upper) # Write a mask from places where the color is between the outside
mask[skinMask == 255] = 0 # We remove the face mask from the mask of the head
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
mask = cv2.dilate(mask, kernel1, iterations=1)
i1 = cv2.bitwise_and(img1, img1, mask=mask)
if is_bold(topmost, mask):
cv2.rectangle(img1, topmost, topmost, (0, 0, 255), 5)
#print(topmost)
# Otherwise we write that we are not bald and display the coordinates of the largest contour
else:
cnts = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
len_cnts = len(cnts[0])
max_y = 0
for c in range(len_cnts):
point = (cnts[0][c][0][0], cnts[0][c][0][1])
if (point[0] >= list_points[NOSE][0][0] - 5 and point[0] <= (list_points[NOSE][0][0]) + 5):
if (max_y < point[1]):
max_y = point[1]
hair_line_point = list_points[NOSE][0][0], max_y
cv2.circle(img1, (list_points[NOSE][0][0], max_y), 2, (0, 255, 0), -1)
up = (list_points[RIGHT_EYEBROW][4] - hair_line_point)[1]
return up, hair_line_point
def face_length_ratio (up, center, low):
# 상중하안부 비율의 기준 정하기
if (up < center):
if (up < low):
criteria = up
else:
criteria = low
elif (center < low):
if (center < up):
criteria = center
else:
criteria = up
elif (low < up):
if (low < center):
criteria = low
else:
criteria = center
# 상중하안부 비율
upper_ratio = round(abs(up / criteria), 1)
center_ratio = round(abs(center / criteria), 1)
lower_ratio = round(abs(low / criteria), 1)
#print(upper_ratio, center_ratio, lower_ratio)
if (upper_ratio == center_ratio and upper_ratio == lower_ratio):
ratio = 0 # 1:1:1
elif (upper_ratio >= center_ratio and upper_ratio >= lower_ratio):
ratio = 1 # 상안부 길 때
elif (center_ratio >= lower_ratio and center_ratio >= upper_ratio):
ratio = 2 # 중안부 길 때
elif (lower_ratio >= upper_ratio and lower_ratio >= center_ratio):
ratio = 3 # 하안부 길 때
elif (lower_ratio == center_ratio and upper_ratio < center_ratio and upper_ratio < lower_ratio):
ratio = 4 # 상안부가 가장 짧을 때
return ratio
# 옆광대 여부
def side_cheekbone_have(list_points):
flag = 0
x = (list_points[JAWLINE][1] - list_points[JAWLINE][2])[0]
y = (list_points[JAWLINE][1] - list_points[JAWLINE][2])[1]
#print(abs(y/x))
if (abs(y / x) >= 6.8): flag = 1
return flag
def nose_detection(list_points,image_front):
#pTime = 0
mpDraw = mp.solutions.drawing_utils
mpFaceMesh = mp.solutions.face_mesh
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
drawSpec = mpDraw.DrawingSpec(thickness=1, circle_radius=2)
imgRGB = cv2.cvtColor(image_front, cv2.COLOR_BGR2RGB)
results = faceMesh.process(imgRGB)
nose = []
if results.multi_face_landmarks:
for faceLms in results.multi_face_landmarks:
for id,lm in enumerate(faceLms.landmark):
ih, iw, ic = image_front.shape
x, y = int(lm.x * iw), int(lm.y * ih)
if (id == 102 or id == 278): # 콧볼
#print("콧볼", id, x, y)
cv2.circle(image_front, (x, y), 2, (0, 255, 0), -1)
nose.append([x, y])
nose_w = abs(nose[0][0] - nose[1][0])
ratio_nose = abs(face_w/nose_w)
# print("콧볼", ratio_nose)
if (ratio_nose >= 3.2 and ratio_nose <= 4.0): # 평균 3.825
nose_result = 0
nose_percent = 0
#print("콧볼 크기는 평균입니다")
elif (ratio_nose < 3.2):
nose_result = 1
nose_percent = round(abs(3.6 - ratio_nose),2)
#print("콧볼", ratio_nose)
#print("콧볼 크기는 평균보다 %.1f%% 큰 편입니다" % (abs(nose[0][0] - list_points[RIGHT_EYE][3][0]) / face_w))
elif (ratio_nose >= 4.0):
nose_result = -1
nose_percent = round(abs(3.6 - ratio_nose),2)
#print("콧볼", ratio_nose)
#print("콧볼 크기 %.1f%% 작은 편입니다" % (abs(nose[0][0] - list_points[RIGHT_EYE][3][0]) / face_w))
return nose_result, nose_percent
#눈 가로
def eyew_detection(list_points):
reye_w = abs(list_points[RIGHT_EYE][0] - list_points[RIGHT_EYE][3])[0] # 오른쪽 눈 가로
ratio_eyew = abs(face_w / reye_w)
#print("눈 가로", ratio_eyew)
if (ratio_eyew >= 5.0 and ratio_eyew <= 5.3): # 평균값 = 5.675
eyew_result = 0
eyew_percent = 0
#print("눈 가로 길이는 평균")
elif (ratio_eyew < 5.0):
eyew_result = 1
eyew_percent = round(abs(5.15 - ratio_eyew),2)
#print("눈 가로 길이 평균보다 %.1f%% 긴 편" % (abs(5.675 - ratio_eyew)))
elif (ratio_eyew > 5.3):
eyew_result = -1
eyew_percent = round(abs(5.15 - ratio_eyew),2)
#print("눈 가로 길이 평균보다 %.1f%% 짧은 편" % (abs(5.675 - ratio_eyew)))
return eyew_result, eyew_percent
#눈 세로
def eyeh_detection(list_points):
reye_h = abs(list_points[RIGHT_EYE][1] - list_points[RIGHT_EYE][5])[1] # 오른쪽 눈 세로
ratio_eyeh = abs(face_h / reye_h)
#print("눈 세로", ratio_eyeh)
if (ratio_eyeh >= 19 and ratio_eyeh <= 23.6): # 평균비 = 24
eyeh_result = 0
eyeh_percent = 0
# print("눈 세로 길이 평균")
elif (ratio_eyeh < 19):
eyeh_result = 1
eyeh_percent = round(abs(21.3 - ratio_eyeh),2)
# print("눈 세로 길이 평균보다 %.1f%% 긴 편" % (abs(23.8 - ratio_eyeh)))
elif (ratio_eyeh > 23.6):
eyeh_result = -1
eyeh_percent = round(abs(21.3 - ratio_eyeh),2)
# print("눈 세로 길이 평균보다 %.1f%% 짧은 편" % (abs(23.8 - ratio_eyeh)))
return eyeh_result, eyeh_percent
#입술 가로
def lips_detection(list_points):
lips_w = abs(list_points[MOUTH_OUTLINE][0] - list_points[MOUTH_OUTLINE][6])[0] # 입술 가로
ratio_lips = face_w / lips_w
#print("입술", ratio_lips)
if (ratio_lips >= 2.3 and ratio_lips <= 3.3):
lips_result = 0 # 입술 가로 길이 평균
elif (ratio_lips < 2.3):
lips_result = 1 # 입술 가로 길이 긴 편
elif (ratio_lips > 3.3):
lips_result = -1 # 입술 가로 길이 짧은 편
return lips_result
#미간거리
def between_detection(list_points):
eyetoeye = abs(list_points[RIGHT_EYE][3] - list_points[LEFT_EYE][0])[0] # 미간 거리
between_ratio = abs(face_w / eyetoeye)
#print("미간", between_ratio)
if (between_ratio >= 3.2 and between_ratio < 3.4):
between_result = 0
between_percent = 0
#print("미간 평균 ", between_ratio)
elif (between_ratio < 3.2):
between_result = -1
between_percent = round(abs(3.3 - between_ratio), 2)
#print("미간 짧은 편 ", between_ratio)
#미간 긴 편
elif (between_ratio > 3.4):
between_result = 1
between_percent = round(abs(3.3 - between_ratio), 2)
#print("미간 긴 편 ", between_ratio)
#미간 짧은편
return between_result, between_percent
#눈꼬리
def eyeshape_detection(list_points):
p1 = list_points[RIGHT_EYE][0][0]
p2 = list_points[RIGHT_EYE][1][1]
p3 = list_points[RIGHT_EYE][2][1]
p4 = list_points[RIGHT_EYE][3][0]
p5 = list_points[RIGHT_EYE][4][1]
p6 = list_points[RIGHT_EYE][5][1]
if (p1 == p4): #p4가 내려갈수록 값이 크게 나옴
eyeupdown = 0
else:
#print(p1, p2, p3, p4, p5, p6)
eyeupdown = (abs(p2 - p6) + abs(p3 - p5)) / (2 * (abs(p4 - p1)))
#print("눈의 비율", eyeupdown)
if (eyeupdown >= 0.36):
eyeshape_result = 1 # 눈꼬리 올라감
elif (eyeupdown >= 0.32):
eyeshape_result = 0 # 눈꼬리 보통
else:
eyeshape_result = -1 # 눈꼬리 내려감
return eyeshape_result
# 앞광대 여부
def front_cheekbone_have(list_points,image_side):
#pTime = 0
mpDraw = mp.solutions.drawing_utils
mpFaceMesh = mp.solutions.face_mesh
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
drawSpec = mpDraw.DrawingSpec(thickness=1, circle_radius=2)
imgRGB = cv2.cvtColor(image_side, cv2.COLOR_BGR2RGB)
results = faceMesh.process(imgRGB)
cheekbone = []
if results.multi_face_landmarks:
for faceLms in results.multi_face_landmarks:
for id,lm in enumerate(faceLms.landmark):
ih, iw, ic = image_side.shape
x, y = int(lm.x * iw), int(lm.y * ih)
if (id == 116 or id == 123 or id == 147 or id == 192 or id == 213): # test
# print(id,x,y)
#pt_pos2 = (x, y)
# cheekbone.append([id, pt_pos2])
cheekbone.append([id, x, y])
cv2.circle(image_side, (x, y), 2, (0, 255, 0), -1)
m = (cheekbone[3][2] - cheekbone[1][2]) / (cheekbone[3][1] - cheekbone[1][1]) # 123번-192번 기울기
#print("기울기", m)
if (m <= 3.45):
#print("앞광대 O")
cheek_front = 1
else:
#print("앞광대 X")
cheek_front = 0
return cheek_side
error_index = 0 # 0:정상 1:랜드마크 검출 오류 2:얼굴형 오류 3:얼굴 비율 오류 4:헤어라인 오류 5:이목구비 계산 오류
# 이미지 읽어오기
image_front_origin = cv2.imread("front.png")
image_faceline = Image.open("front.png")
# 얼굴형 분류 모델의 위치 = PATH
PATH = 'model_26.pt'
image_front = imutils.resize(image_front_origin, height=500) # image 크기 조절
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
ALL = list(range(0, 68))
RIGHT_EYEBROW = list(range(17, 22))
LEFT_EYEBROW = list(range(22, 27))
RIGHT_EYE = list(range(36, 42))
LEFT_EYE = list(range(42, 48))
NOSE = list(range(27, 36))
MOUTH_OUTLINE = list(range(48, 61))
MOUTH_INNER = list(range(61, 68))
JAWLINE = list(range(0, 17)) # index 1, 15 = 옆광대
index = ALL
error_index = 0
try:
faceline_index = faceline(image_faceline, PATH)
center, low, list_points = face_detection(image_front)
# 얼굴 비율 구할 때 필요한 맨 위 점 (헤어 라인 점)
up, hair_line_point = hair_up(image_front,list_points)
if (hair_line_point[1] >= list_points[LEFT_EYEBROW][0][1] or hair_line_point[1] >= list_points[RIGHT_EYEBROW][4][1]):
error_index = 1
#cv2.imshow("front result", image_front)
cv2.imwrite('front_result.png', image_front)
face_w = abs(list_points[JAWLINE][1]-list_points[JAWLINE][15])[0] # 얼굴 가로
face_h = abs(list_points[JAWLINE][8]-hair_line_point)[1] # 얼굴 세로
# 얼굴 상중하 비율 0: 1:1:1 1:상안부 길때 2:중안부 길 때 3:하안부 길때 4:상안부 제일 짧을때
ratio = face_length_ratio(up, center, low)
# 옆광대 유무 1: 있음 , 0: 없음
cheek_side = side_cheekbone_have(list_points)
# 콧볼 0: 평균 , 1: 큼 , -1: 작음
nose_result, nose_percent = nose_detection(list_points,image_front)
# 눈 세로 0: 평균 , 1: 큼 , -1: 작음
eyeh_result, eyeh_percent = eyeh_detection(list_points)
# 눈 가로 0: 평균 , 1: 큼 , -1: 작음
eyew_result, eyew_percent = eyew_detection(list_points)
# 미간 0: 평균 , 1: 큼 , -1: 작음
between_result, between_percent = between_detection(list_points)
# 눈꼬리 1: 올라감 0: 보통 -1: 내려감
eyeshape_result = eyeshape_detection(list_points)
# 꼬막눈 1:꼬막눈 0:꼬막눈 아님
if(eyeshape_result == 1 and eyew_result == -1): #올라간 눈이면서 눈 짧으면
shorteye_result = 1 # 꼬막눈 O
else:
shorteye_result = 0 # 꼬막눈 X
# 입술가로 1: 큼 0: 평균 -1: 작음
lips_result = lips_detection(list_points)
except NameError as e:
error_index = 1