😏Face Delauney Triangle

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)