# Data File Download # https://github.com/italojs/facial-landmarks-recognition/blob/master/shape_predictor_68_face_landmarks.dat # import cv2, dlib, sys import numpy as np import math # 이미지 크기 비율 조절 scaler = 1.0 # 얼굴 디텍터 모듈 초기화 detector = dlib.get_frontal_face_detector() # 얼굴 특징점 모듈 초기화 # shape_predictor_68_face_landmarks.dat 파일은 머신러닝으로 학습된 데이터임 predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') img = cv2.imread('mid_exam.jpg', cv2.IMREAD_ANYCOLOR) img = cv2.resize(img, (int(img.shape[1]*scaler), int(img.shape[0]*scaler))) # detect faces 이미지에서 얼굴모양 찾기 faces = detector(img) # 0번 인덱스, 첫번째 인식되는 얼굴 하나만 인식하기 face = faces[0] # 얼굴 영역에서 특징점 찾기 dlib_shape = predictor(img, face) # dlib 객체를 numpy 객체로 변환 shape_2d = np.array([[p.x, p.y] for p in dlib_shape.parts()]) img = cv2.rectangle(img, pt1=(face.left(), face.top()), pt2=(face.right(), face.bottom()), color=(255, 255, 255), thickness = 2, lineType=cv2.LINE_AA ) cv2.imshow('img', img) # 지연시간(1ms)을 두어서 출력이 용이하게 key = cv2. waitKey(0) #if(key == 27): cv2.destroyAllWindows() # 이미지 크기가 scaler 크기만큼 조절되어서 출력