저번 포스트에서는 웹캠을 통해 얼굴의 위치를 찾는 코드를 구현해보았다.
이번 포스트에서는 해당 코드에서 이어 인식된 얼굴에서 눈의 위치를 찾는 것을 구현해보도록 하겠다.
먼저 사람의 얼굴을 68개의 점으로 표현한 데이터가 있다.

위처럼 표현된 사람의 얼굴을 통해 사람의 눈의 위치를 찾도록 하겠다.
import cv2
import numpy as np
import dlib #68개의 점으로 얼굴을 인식한다
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
# x, y = face.left(), face.top()
# x1, y1 = face.right(), face.bottom()
# cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
landmarks = predictor(gray, face)
left_point = (landmarks.part(36).x, landmarks.part(36).y)
right_point = (landmarks.part(39).x, landmarks.part(39).y)
hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
predictor .dat파일을 설정한 후 위처럼 얼굴을 68개의 점으로 활용할 수 있도록 landmarks 변수에 지정한다.
위의 그림처럼 눈의 끝 꼬리의 점인 36번과 39번을 line으로 연결하여 나타내었다.
실행 결과 아래와 같이 눈의 끝과 끝을 잘 검출하는 것을 알 수 있었다.

다음 포스트에서는 눈의 깜빡임 감지를 구현해보겠다.
'Study > Python' 카테고리의 다른 글
| Opencv를 활용한 Eye-Tracking(1) (0) | 2020.05.11 |
|---|
