import cv2
import numpy as np
import dlib
# μΌκ΅΄ κ²μΆκΈ°μ λλλ§ν¬ κ²μΆκΈ° μμ± --- β
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')
img = cv2.imread("../img/man_face.jpg")
h, w = img.shape[:2]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# μΌκ΅΄ μμ κ²μΆ --- β‘
rects = faces = detector(gray)
points = []
for rect in rects:
# λλλ§ν¬ κ²μΆ --- β’
shape = predictor(gray, rect)
for i in range(68):
part = shape.part(i)
points.append((part.x, part.y))
# λ€λ‘λ€ μΌκ° λΆν κ°μ²΄ μμ± --- β£
x,y,w,h = cv2.boundingRect(np.float32(points))
subdiv = cv2.Subdiv2D((x,y,x+w,y+h))
# λλλ§ν¬ μ’ν μΆκ° --- β€
subdiv.insert(points)
# λ€λ‘λ€ μΌκ°ν μ’ν κ³μ° --- β₯
triangleList = subdiv.getTriangleList()
# λ€λ‘λ€ μΌκ°ν 그리기 --- β¦
h, w = img.shape[:2]
cnt = 0
for t in triangleList :
pts = t.reshape(-1,2).astype(np.int32)
# μ’ν μ€μ μ΄λ―Έμ§ μμμ λ²μ΄λλ κ²μ μ μΈ(μμ λ±) ---β§
if (pts < 0).sum() or (pts[:, 0] > w).sum() or (pts[:, 1] > h).sum():
print(pts)
continue
cv2.polylines(img, [pts], True, (255, 255,255), 1, cv2.LINE_AA)
cnt+=1
print(cnt)
cv2.imshow("Delaunay",img)
cv2.waitKey(0)