블로그 이미지
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. 14:00 Raspberry Pi

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

 

저번 게시물에서 얼굴의 68개의 특징점을 잡아낸 알고리즘을 이용한 예제를 실행해봤습니다.

이번에는 5개의 랜드마크를 추출하는 알고리즘을 사용했습니다.

 

 

68개의 랜드마크를 추출하는 방식과 달리 ,이번 알고리즘은 

왼쪽 눈 2점, 오른쪽 눈 2점, 코 1점을 검출하여, 라즈베리파이에서 보다 더 나은 속도를 보여줍니다.

속도는 68개 추출에 비해 8-10% 향상되었으며, 잠재적으로 정확성은 조금 떨어질 수 있다고 합니다.

인식률이 상당히 좋습니다. 라즈베리파이에서 이정도 속도만 나와도 감사할 따름입니다...

실시간으로 웹캠을 통해 얼굴의 특징점이 인식됩니다.

또한

68 포인트 랜드마크 데이터 파일은 99.7MB정도였지만

5 포인트 랜드마크 데이터 파일은 9.2MB로 기존 파일에 비해 약 10배 정도 작아졌습니다.

 

구성환경으로는 전 게시물과 동일합니다.

Raspberry Pi 3B+

Picamera

OS : Raspbian Stretch 9.11

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

 

소스코드입니다.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
# USAGE
 
# import the necessary packages
from imutils.video import VideoStream
from imutils import face_utils
import argparse
import imutils
import time
import dlib
import cv2
 
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p""--shape-predictor", required=True,
    help="/home/pi/Desktop/faster_facial_landmarks/shape_predictor_5_face_landmarks.dat")
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 sleep for a bit, allowing the
# camera sensor to warm up
print("[INFO] camera sensor warming up...")
#vs = VideoStream(src=0).start()
vs = VideoStream(usePiCamera=True).start() # Raspberry Pi
 
# 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)
 
    # check to see if a face was detected, and if so, draw the total
    # number of faces on the frame
    if len(rects) > 0:
        text = "{} face(s) found".format(len(rects))
        cv2.putText(frame, text, (1020), cv2.FONT_HERSHEY_SIMPLEX,
            0.5, (00255), 2)
 
    # loop over the face detections
    for rect in rects:
        # compute the bounding box of the face and draw it on the
        # frame
        (bX, bY, bW, bH) = face_utils.rect_to_bb(rect)
        cv2.rectangle(frame, (bX, bY), (bX + bW, bY + bH),
            (02550), 1)
 
        # 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 each of them
        for (i, (x, y)) in enumerate(shape):
            cv2.circle(frame, (x, y), 1, (00255), -1)
            cv2.putText(frame, str(i + 1), (x - 10, y - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.35, (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

 

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

 

코드 복사후, 동일한 폴더 내에 데이터 파일과 파이썬파일을 넣어줍니다.

(5 포인트 얼굴 랜드마크 추출 데이터 파일은 하단의 참조사이트에서 받을 수 있습니다.)

 

저는 Desktop 에 faster_facial_landmarks 라는 폴더를 생성하여 넣어주었습니다.

터미널에 명령어를 입력해줍니다.

1
2
pi@raspberrypi:~/Desktop/faster_facial_landmarks $ python faster_facial_landmarks.py -shape_predictor_5_face_landmarks.dat
 
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

 

이번 게시물은 5개의 특징점을 잡아내는 예제를 실행해봤습니다.

다음엔 졸음운전을 감지하는 시스템(?) 을 포스팅 하도록 하겠습니다.

 

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

 

참고사이트

(Faster) Facial landmark detector with dlib

 

(Faster) Facial landmark detector with dlib - PyImageSearch

In this tutorial you'll learn how to use dlib's 5-point facial landmark model, over 10x smaller and 8-10% faster than the original 68-point facial landmark detector.

www.pyimagesearch.com

 

posted by devtang