이미지를 이용하여 얼굴을 인식한 이미지 배열 정보를  Numpy에 저장 하는 예제입니다.

참조한 모델은 이미 학습된  davisking/dlib-models 의 shape_predictor_68_face_landmarks.dat 와 dlib_face_recognition_resnet_model_v1.dat 을 사용 하였습니다.

얼굴이미지는 한국영화 "걸캅스"의 주인공 라미란, 이성경의 포스터 이미지를 사용하여 얼굴인식

을 학습 저장 하였습니다.

 

얼굴을 인식한 이미지 배열을 Numpy 저장하는 딥러닝 모델 예제

 

# Face Recognition

import dlib, cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat')
facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat')



def find_faces(img):
    dets = detector(img, 1)

    # face Not Found empty 0 return
    if len(dets) == 0:
        return np.empty(0), np.empty(0), np.empty(0)

    rests, shapes = [], []
    shapes_np = np.zeros((len(dets), 68, 2), dtype=np.int)

    for k, d in enumerate(dets):
        rect = ((d.left(), d.top()), (d.right(), d.bottom()))
        rests.append(rect)
        shape = sp(img, d)

        # convert dlib shape to numpy array
        for i in range(0, 68):
             shapes_np[k][i] = (shape.part(i).x, shape.part(i).y)

        shapes.append(shape)
    return rests, shapes, shapes_np



def encode_faces(img, shapes):
    face_descriptors = []
    for shape in shapes:
        face_descriptor = facerec.compute_face_descriptor(img, shape)
        face_descriptors.append(np.array(face_descriptor))
    return np.array(face_descriptors)



# Compute Saved Face Description
img_paths = {
    'Ramiran': 'images/Ramiran_face_image.png',
    'LeeSungKyung': 'images/LeeSungKyung_face_Image.png'
}

descs = {
    'Ramiran': None,
    'LeeSungKyung': None
}

for name, img_paths in img_paths.items():
    img_bgr = cv2.imread(img_paths)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

plt.imshow(img_rgb)
plt.show()

_, img_shapes, _ = find_faces(img_rgb)

print('img_shapes : ', img_shapes)

descs[name] = encode_faces(img_rgb, img_shapes)[0]
np.save('images/Gril_Cops.npy', descs)

 

얼굴인식 이미지

한국영화 '걸캅스' 의 포스터에서 라미란과 이성경의 얼굴이미지를 발췌하여 딥러닝 얼굴인식 모델을 이용하여

학습된 이미지 정보를 Numpy 로 저장을 합니다.

출처 : 한국영화 '걸캅스' 포스터에서 발췌

 

출처 : 한국영화 '걸캅스' 포스터에서 발췌

실행 결과

 

배우 라미란의 얼굴이미지의 numpy 배열 정보 결과
배우 이성경 얼굴이미지의 numpy 배열 정보 결과

 

+ Recent posts