"얼굴인식"의 두 판 사이의 차이

Pywiki
둘러보기로 가기 검색하러 가기
 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
10번째 줄: 10번째 줄:
 
detector = dlib.get_frontal_face_detector()  # 얼굴탐지모델
 
detector = dlib.get_frontal_face_detector()  # 얼굴탐지모델
 
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')  # 얼굴 랜드마크 탐지 모델. 학습된 모델을 가져온다.
 
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')  # 얼굴 랜드마크 탐지 모델. 학습된 모델을 가져온다.
facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat')  # 인식모델. 랜드마크에서 shape를 받아 구분한다.
+
facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat')  # 인식모델. 랜드마크에서 shape를 받아 구분한다. 얼굴의 특징을 받아 분류정보를 배열로 저장한다.
 
# 위 모델로 특정인을 인식할 수 있다.
 
# 위 모델로 특정인을 인식할 수 있다.
  
19번째 줄: 19번째 줄:
 
위 과정에서 에러가 난다면 [[얼굴추적]] 문서를 참고하자.
 
위 과정에서 에러가 난다면 [[얼굴추적]] 문서를 참고하자.
  
 +
=== 얼굴 입력하기 ===
 +
분류할 얼굴을 특정 디렉터리 안에 그림파일로 넣고, 이들의 특징을 담은 넘파이 배열을 descs.npy에 저장한다.
 +
 +
이는 최초에 얼굴에 대해 교육할 때만 사용한다.
 +
 +
유의 : png는 인식이 잘 안되는듯;;? jpg로 넣도록 하자. +교육할 땐 얼굴 하나만 있는 사진을 사용하자. 여러 명이 있는 사진의 경우, 얼굴 하나만 받는다.(얼굴이 없으면 에러가 난다.)<syntaxhighlight lang="python">
 +
def educate():
 +
    import os
 +
    path_dir = 'C:\\Users\\id843\\PycharmProjects\\AI_practice\\img\\to_recognize\\'
 +
    img_list = os.listdir(path_dir)
 +
    people = {}
 +
    for name in img_list:
 +
        img_bgr = cv2.imread(path_dir + name)
 +
        name = name.split('.')[0]  # 확장자를 쳐내고 앞의 이름만 따온다.
 +
 +
        face = detector(img_bgr)[0]  # 얼굴 하나만 받는다.
 +
        dlib_shape = predictor(img_bgr, face)  # 특징점을 리턴받는다.
 +
        face_descriptor = facerec.compute_face_descriptor(img_bgr, dlib_shape)
 +
        people[name] =  np.array(face_descriptor)  # 연산을 위해 배열로 저장.
 +
 +
    np.save('img\descs.npy', people)
 +
</syntaxhighlight>
 
=== 함수정의 ===
 
=== 함수정의 ===
 
{| class="wikitable"
 
{| class="wikitable"
28번째 줄: 50번째 줄:
 
|얼굴의 특징점을 찾아 배열로 반환한다.
 
|얼굴의 특징점을 찾아 배열로 반환한다.
 
|<syntaxhighlight lang="python">
 
|<syntaxhighlight lang="python">
def detect_faces(img):
+
def recognize_faces(img):
 +
    '''얼굴의 특징점을 찾아 기존의 기억된 인물들과 대조한다'''
 
     faces = detector(img, 1)
 
     faces = detector(img, 1)
 
     if len(faces) == 0:
 
     if len(faces) == 0:
54번째 줄: 77번째 줄:
 
     if not ret:  # 잘 찍히면 ret은 True를 반환한다.
 
     if not ret:  # 잘 찍히면 ret은 True를 반환한다.
 
         break  # 프레임이 없다면 종료.
 
         break  # 프레임이 없다면 종료.
     detect_faces(img)
+
     recognize_faces(img)
 
     cv2.imshow('window', img)  # 창에 해당하는 이미지를 띄운다.
 
     cv2.imshow('window', img)  # 창에 해당하는 이미지를 띄운다.
 
     cv2.waitKey(1)  # 이게 있어야 창이 제대로 열린다.
 
     cv2.waitKey(1)  # 이게 있어야 창이 제대로 열린다.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[분류:딥러닝 트레킹]]
 
[[분류:딥러닝 트레킹]]

2021년 10월 1일 (금) 16:48 기준 최신판

1 개요[편집 | 원본 편집]

얼굴추적을 한 후 해당 얼굴이 어떤 사람의 얼굴인지 파악하는 기술이다. 얼굴을 찾거나 따라가는 기술을 찾는다면 얼굴추적 문서를 참고하자.

2 구현방법[편집 | 원본 편집]

첫 번째 단계로 얼굴찾기를 수행해주어야 한다.

import dlib  # 얼굴인식
import cv2  # 이미지처리
import numpy as np  # 연산

detector = dlib.get_frontal_face_detector()  # 얼굴탐지모델
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')  # 얼굴 랜드마크 탐지 모델. 학습된 모델을 가져온다.
facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat')  # 인식모델. 랜드마크에서 shape를 받아 구분한다. 얼굴의 특징을 받아 분류정보를 배열로 저장한다.
# 위 모델로 특정인을 인식할 수 있다.

descs = np.load('img/descs.npy', allow_pickle=True)[()]
cap = cv2.VideoCapture(0)  # 영상 캡쳐. 경로 대신 0을 넣으면 웹캠이 켜진다.

dlib_face_recognition_reset_model 은 다음 링크를 통해 받자. 링크

위 과정에서 에러가 난다면 얼굴추적 문서를 참고하자.

2.1 얼굴 입력하기[편집 | 원본 편집]

분류할 얼굴을 특정 디렉터리 안에 그림파일로 넣고, 이들의 특징을 담은 넘파이 배열을 descs.npy에 저장한다.

이는 최초에 얼굴에 대해 교육할 때만 사용한다.

유의 : png는 인식이 잘 안되는듯;;? jpg로 넣도록 하자. +교육할 땐 얼굴 하나만 있는 사진을 사용하자. 여러 명이 있는 사진의 경우, 얼굴 하나만 받는다.(얼굴이 없으면 에러가 난다.)

def educate():
    import os
    path_dir = 'C:\\Users\\id843\\PycharmProjects\\AI_practice\\img\\to_recognize\\'
    img_list = os.listdir(path_dir)
    people = {}
    for name in img_list:
        img_bgr = cv2.imread(path_dir + name)
        name = name.split('.')[0]  # 확장자를 쳐내고 앞의 이름만 따온다.

        face = detector(img_bgr)[0]  # 얼굴 하나만 받는다.
        dlib_shape = predictor(img_bgr, face)  # 특징점을 리턴받는다.
        face_descriptor = facerec.compute_face_descriptor(img_bgr, dlib_shape)
        people[name] =  np.array(face_descriptor)  # 연산을 위해 배열로 저장.

    np.save('img\descs.npy', people)

2.2 함수정의[편집 | 원본 편집]

의도 설명 방법
특징점을 배열로 반환하기 얼굴의 특징점을 찾아 배열로 반환한다.
def recognize_faces(img):
    '''얼굴의 특징점을 찾아 기존의 기억된 인물들과 대조한다'''
    faces = detector(img, 1)
    if len(faces) == 0:
        return np.empty(0)

    for k, face in enumerate(faces):
        shape = predictor(img, face)  # 주어진 얼굴의 특징점을 찾는다.
        face_descriptor = facerec.compute_face_descriptor(img, shape)

    result = {'name':'unkown', '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 < result['dist']:  # 일치값이 0.6보다 낮다면..(일반적으로 0.6이 잘 된다고 알려져 있음)
            result = {'name': name, 'dist': dist, 'color': (255, 255, 255)}

    cv2.putText(img, result['name'], org=(face.left(), face.top()), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                fontScale=1, color=result['color'], thickness=2)

2.3 실행[편집 | 원본 편집]

while True:  # 기본적으로 계속 진행
    ret, img = cap.read()  # 캡처한 영상을 프레임 단위로 읽는다.
    if not ret:  # 잘 찍히면 ret은 True를 반환한다.
        break  # 프레임이 없다면 종료.
    recognize_faces(img)
    cv2.imshow('window', img)  # 창에 해당하는 이미지를 띄운다.
    cv2.waitKey(1)  # 이게 있어야 창이 제대로 열린다.