새소식

2018 소공전

Anaconda Python Dlib 사용하여 얼굴인식 (눈, 입, 코, 눈썹)

  • -





이번 프로젝트를 진행하는데 입술을 인식하는 코드가 필요했습니다. 


하지만 기존 opencv 코드로 입술을 인식하는 것의 인식률이 좋지 않아 


dlib로 입술을 인식하기로 하였습니다.









Anaconda Python dlib 사용하여 얼굴인식하기



아래 코드는 Github에 있는 코드를 수정한 코드입니다.

코드의 원본은 제 Github에 올려놨습니다.


# import the necessary packages
from collections import OrderedDict
import numpy as np
import cv2
import argparse
import dlib
import imutils


facial_features_cordinates = {}


# define a dictionary that maps the indexes of the facial
# landmarks to specific face regions
FACIAL_LANDMARKS_INDEXES = OrderedDict([
("Mouth", (48, 68)),
("Right_Eyebrow", (17, 22)),
("Left_Eyebrow", (22, 27)),
("Right_Eye", (36, 42)),
("Left_Eye", (42, 48)),
("Nose", (27, 35)),
("Jaw", (0, 17))
])



def shape_to_numpy_array(shape, dtype="int"):

# initialize the list of (x, y)-coordinates
coordinates = np.zeros((68, 2), dtype=dtype)

# loop over the 68 facial landmarks and convert them
# to a 2-tuple of (x, y)-coordinates
for i in range(0, 68):
coordinates[i] = (shape.part(i).x, shape.part(i).y)

# return the list of (x, y)-coordinates
return coordinates


def visualize_facial_landmarks(image, shape, colors=None, alpha=0.75):

# create two copies of the input image -- one for the
# overlay and one for the final output image
overlay = image.copy()
output = image.copy()

# if the colors list is None, initialize it with a unique

# color for each facial landmark region
if colors is None:
colors = [(19, 199, 109), (79, 76, 240), (230, 159, 23),
(168, 100, 168), (158, 163, 32),
(163, 38, 32), (180, 42, 220)]

# loop over the facial landmark regions individually
for (i, name) in enumerate(FACIAL_LANDMARKS_INDEXES.keys()):

# grab the (x, y)-coordinates associated with the
# face landmark
(j, k) = FACIAL_LANDMARKS_INDEXES[name]

pts = shape[j:k]
facial_features_cordinates[name] = pts

# check if are supposed to draw the jawline
if name == "Jaw":

# since the jawline is a non-enclosed facial region,
# just draw lines between the (x, y)-coordinates
for l in range(1, len(pts)):
ptA = tuple(pts[l - 1])
ptB = tuple(pts[l])
cv2.line(overlay, ptA, ptB, colors[i], 2)

# otherwise, compute the convex hull of the facial
# landmark coordinates points and display it
else:
hull = cv2.convexHull(pts)
cv2.drawContours(overlay, [hull], -1, colors[i], -1)

# apply the transparent overlay
cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)

# return the output image
print(facial_features_cordinates)
return output


# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')



# load the input image, resize it, and convert it to grayscale
image = cv2.imread('images/image_1.jpg')

image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


# detect faces in the grayscale image
rects = detector(gray, 1)

# loop over the face detections
for (i, rect) in enumerate(rects):

# determine the facial landmarks for the face region, then
# convert the landmark (x, y)-coordinates to a NumPy array
shape = predictor(gray, rect)
shape = shape_to_numpy_array(shape)

output = visualize_facial_landmarks(image, shape)

cv2.imshow("Image", output)
cv2.waitKey(0)








 코드 실행방법




위 코드를 실행하기 위해서는 shape_predictor_68_face_landmarks.dat 

파일이 필요합니다. 아래 링크에서 다운받아 주세요.


https://drive.google.com/open?id=1sM08AcIv2Hi7RY184i9oCi9uvgn6JFvD



프로젝트 파일에 image 디렉터리를 만들어주시고 그 안에 분석할 이미지를 

넣어주시면 됩니다. 



이미지 이름은


 image = cv2.imread('images/image_1.jpg')


이 코드에서 바꿔주시면 됩니다.





 코드 실행결과






코드를 실행하시면 코, 입술, 눈, 눈썹이 인식되어 색칠되어 나타납니다.

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.