앞의 예제에서 Numpy에 저장된 이미지 파일을 읽어와서 다른 이미지로 얼굴인식하는 딥러닝 

모델 예제입니다.

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

 

Numpy 저장된 이미지 파일을 읽어와서 다른 이미지로 얼굴인식하는 딥러닝 모델 예제

 

# Face Detection
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')


# Numpy 로 저장된 얼굴이미지 화일 읽어오기
descs = np.load('images/Gril_Cops.npy', allow_pickle=True)[()]



def read_img(img_path):
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img



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

    if len(dets) == 0:
    return np.empty(0)

    for k, d in enumerate(dets):
        shape = sp(img, d)
        face_descriptor = facerec.compute_face_descriptor(img, shape)
        return np.array(face_descriptor)

#  얼굴인식을 할 이미지를 읽어오기
img1_path = 'images/GirlCops_Poster_1.jpg'
img_bgr = read_img(img1_path)


plt.imshow(img_bgr)
plt.show()

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dets = detector(img_bgr, 1)

for k, d in enumerate(dets):
    shape = sp(img_rgb, d)
    face_descriptor = facerec.compute_face_descriptor(img_rgb, shape)

    last_found = {'name': 'unknown', 'dist': 0.6, 'color': (0, 0, 255)}

    for name, saved_desc in descs.items():
        dist = np.linalg.norm([face_descriptor] - saved_desc, axis=1)

        if dist < last_found['dist']:
        last_found = {'name': name, 'dist': dist, 'color': (255, 255, 255)}

        cv2.rectangle(img_bgr, pt1=(d.left(), d.top()), pt2=(d.right(), d.bottom()), color=last_found['color'],
        thickness=2)

        cv2.putText(img_bgr, last_found['name'], org=(d.left(), d.top()), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
        fontScale=1, color=last_found['color'], thickness=2)


plt.imshow(img_bgr)
plt.show()

 


얼굴인식을 위한 한국영화 '걸캅스' 포스터 이미지
얼굴인식을 위한 한국영화 '걸캅스' 포스터 이미지

 

실행결과

 

라미란, 이성경의 얼굴인식된 실행결과
라미란, 이성경의 얼굴인식된 실행결과

+ Recent posts