반응형

안녕하세요. 웹나우테스입니다.

이번 영상에서는 dlib를 사용하여 검출한 얼굴의 랜드마크를 분리하여 보여주는 코드를 설명합니다. 



유튜브 영상입니다.

https://youtu.be/C6qZXg4fLPY?feature=shared




# 패키지 설치
# pip install dlib opencv-python
#
# 학습 모델 다운로드
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
import dlib
import cv2 as cv
import numpy as np

 
detector = dlib.get_frontal_face_detector()

predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')


cap = cv.VideoCapture(0)


# range는 끝값이 포함안됨  
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 = ALL

while True:

    ret, img_frame = cap.read()

    img_gray = cv.cvtColor(img_frame, cv.COLOR_BGR2GRAY)


    dets = detector(img_gray, 1)


    for face in dets:

        shape = predictor(img_frame, face) #얼굴에서 68개 점 찾기

        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[index]):

            pt_pos = (pt[0], pt[1])
            cv.circle(img_frame, pt_pos, 2, (0, 255, 0), -1)

       
        cv.rectangle(img_frame, (face.left(), face.top()), (face.right(), face.bottom()),
            (0, 0, 255), 3)


    cv.imshow('result', img_frame)

   
    key = cv.waitKey(1)

    if key == 27:
        break
   
    elif key == ord('1'):
        index = ALL
    elif key == ord('2'):
        index = LEFT_EYEBROW + RIGHT_EYEBROW
    elif key == ord('3'):
        index = LEFT_EYE + RIGHT_EYE
    elif key == ord('4'):
        index = NOSE
    elif key == ord('5'):
        index = MOUTH_OUTLINE+MOUTH_INNER
    elif key == ord('6'):
        index = JAWLINE


cap.release()

 

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts