블로그 이미지
devtang
Instagram : @taebr0 devtaehyeong@gmail.com

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2020. 2. 25. 12:54 Raspberry Pi

*참고한 사이트는 게시글 하단에 남겨놓겠습니다.

 

이번에 게시할 내용은 바로 dlib 와 OpenCV을 이용한 얼굴의 특징점을 찾아내는 예제입니다.

 

위와같이 얼굴의 특징점을 실시간으로 잡아주는 예제를 실행해봤습니다.

 

Facial Landmarks?

 

 

68개의 특징점을 추출한 포인트

사람의 얼굴에 특징점을 찍어낸 방법입니다.

Face Landmark estimation 알고리즘의 원리는 2014년도에 발명된 접근 방식입니다.

이것을 이용하여 카메라를 이용한 다양한 어플리케이션을 만들수도 있습니다.

 

저는 이것을 라즈베리파이로 실시간 캠영상을 받아, 얼굴에 랜드마크를 표시하는 예제를 실행해봤습니다.

실행한 환경으로는

 

Raspberry Pi 3B+

Picamera

OS : Raspbian Stretch 9.11

OpenCV, imutils, dlib,라이브러리 설치

(라즈베리파이 세팅, 라이브러리 설치 방법 등은 추후에 포스트할 예정입니다.)

 

shape_predictor_68_face_landmarks.dat 파일은 게시물 하단의 참고사이트나 메일로 연락주세요.

 

제가 실행해본 소스 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# USAGE
 
# import the necessary packages
from imutils.video import VideoStream
from imutils import face_utils
import datetime
import argparse
import imutils
import time
import dlib
import cv2
 
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p""--shape-predictor", required=True,
    help="path to facial landmark predictor")
ap.add_argument("-r""--picamera", type=int, default=-1,
    help="whether or not the Raspberry Pi camera should be used")
args = vars(ap.parse_args())
 
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
 
# initialize the video stream and allow the cammera sensor to warmup
print("[INFO] camera sensor warming up...")
vs = VideoStream(usePiCamera=args["picamera"> 0).start()
 
# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream, resize it to
    # have a maximum width of 400 pixels, and convert it to
    # grayscale
    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
    # detect faces in the grayscale frame
    rects = detector(gray, 0)
 
    # loop over the face detections
    for rect in rects:
        # determine the facial landmarks for the face region, then
        # convert the facial landmark (x, y)-coordinates to a NumPy
        # array
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)
 
        # loop over the (x, y)-coordinates for the facial landmarks
        # and draw them on the image
        for (x, y) in shape:
            cv2.circle(frame, (x, y), 1, (00255), -1)
      
    # show the frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1& 0xFF
 
    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
 
# do a bit of cleanup
cv2.destroyAllWindows()
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

소스코드의 자세한 내용은 , 이곳을 참조하세요.

 

소스코드를 저장한후 꼭 소스파일과 landmark 데이터 파일이 한 폴더 내에 있어야합니다.

 

터미널 창을 열어 명령어를 입력해줍니다.

1
pi@raspberrypi:~/Desktop $ python real_landmarks.py -shape_predictor_68_face_landmarks.dat
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

성공적으로 감지가 됩니다!!

 

라즈베리파이에서 구현시 프레임이 저하되긴 하지만 활용할만한 알고리즘인것 같습니다. 

다음 게시물엔 라즈베리파이에서 조금더 효율적으로 인식이되는 방법에대해 포스팅 할 예정입니다.

서투른 글 읽어주셔서 감사합니다.

참고사이트

기계 학습(Machine Learning, 머신러닝)은 즐겁다! Part 4

 

기계 학습(Machine Learning, 머신 러닝)은 즐겁다! Part 4

딥러닝(Deep Learning)을 사용한 최신 얼굴 인식(Face Recognition)

medium.com

Real-time facial landmark detection with OpenCV, Python, and dlib

 

Real-time facial landmark detection with OpenCV, Python, and dlib - PyImageSearch

In this tutorial, I demonstrate how to detect facial landmarks in video streams in real-time using OpenCV, Python, and dlib.

www.pyimagesearch.com

 

posted by devtang