AI

face recognition 모델을 통한 얼굴 유사도 측정 (Python DeepFace)

KIM DEON 2022. 6. 22. 00:23

얼굴 인식 및 유사도 측정은 주로 다음과 같은 단계로 이루어진다. 

1. face detect (얼굴 탐지)
2. align (얼굴이 아닌 부분 제거 및 정렬)
3. representation (얼굴 이미지를 1차원 vector로 표현)
4. verification (두 개의 representation의 distance 측정)

DeepFace

위 과정을 python 라이브러리를 통해 한번에 테스트 해볼 수 있다. DeepFace 라이브러리는 아주 간단한 인터페이스로 face detector 및 recognition model을 제공한다. 파이썬 패키지만 설치해 간단하게 사용해볼 수 있다.

https://viso.ai/computer-vision/deepface/

 

DeepFace - The Most Popular Open Source Facial Recognition Library - viso.ai

How to use the DeepFace Library to apply Deep Face Recognition and Facial Attribute Analysis with the most powerful Face Recognition models.

viso.ai


Detection & Alignment

Face detection은 OpenCV, Dli, SSD  등 다양한 알고리즘을 사용해 볼 수 있다. 

얼굴을 탐지하지 못하는 경우 enforce_detection 파라미터를 False로 지정해 얼굴 탐지를 skip한다. 기존의 이미지가 저장된다.

detect 후 저장된 이미지를 확인하면, 얼굴을 찾아 align한 이미지를 확인할 수 있다. 

from deepface import DeepFace
import cv2

def detect_face(path):
    filename, ext = path.rsplit('.', 1)[0], path.rsplit('.', 1)[1]
    detected = DeepFace.detectFace(path, enforce_detection = False)
    detected = detected*255
    cv2.imwrite(f'{filename}_detected.{ext}', detected[:, :, ::-1])

detect_face('./data/anya1.png')

Representation & Verification

verify 함수를 사용하면 모든 과정을 한번에 수행한다. face detector를 통해 얼굴을 탐지하고, pre-trained 모델을 로드해 이미지를 feature map을 얻고 이미지간 distance를 계산해 유사도를 출력한다. recognition 모델은 다양한 유명 모델들을 지원하고 있다. recognition 모델 및 detector, 유사도 측정 알고리즘은 직접 지정할 수 있다.

 

유사도는 기본적으로 Euclidean distance 또는 Cosine Distance 측정을 통해 비교한다. 각 모델 별 threshold는 미리 정의되어 있다. 

from deepface import DeepFace

# 모든 모델 테스트
model_list = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'ArcFace', 'Ensemble', 'Dlib']

for model in model_list:
    verification = DeepFace.verify(img1_path='./data/anya1.png', img2_path='./data/anya3.png',
                                   model_name=model)
    print(f'{model}: {verification}')

각 모델의 결과는 아래와 같은 형식으로 결과를 얻을 수 있다. 모델 간의 결과 비교를 통해 용도에 적합한 모델을 찾아볼 수 있다. 

{
	'verified': True, 
	'distance': 0.224444881472464, 
	'threshold': 0.4, 
	'model': 'VGG-Face', 
	'detector_backend': 'opencv', 
	'similarity_metric': 'cosine'
 }

 

이 외에도 Facial Attribute 분석 및 실시간 비디오의 얼굴 인식 모듈을 제공한다.