本文记录了通过开源人脸识别引擎face_recognition进行人脸识别,并封装成django的utils进行服务化。 里面用到了dlib的人脸检测服务、opencv-python的摄像头调用和抓拍服务以及face_recognition的人脸识别服务。 完整代码在下面,供有需要的同学参考参考。

环境依赖

pip3 install -r requirement

requirement doc

1
2
3
4
face_recognition
dlib
numpy
opencv-python

利用opencv-python进行摄像头设备连接

以下代码示范了读取摄像头信息,转换成jpg格式并循环yield调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import cv2

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0) # 读取摄像头
    
    def __del__(self):
        self.video.release() # 释放摄像头资源

    def get_frame(self):
        ret,image = self.video.read() # 读取摄像头每一帧的画面
        ret,jpeg = cv2.imencode('.jpg',image) # 转换cv to jpg
        return jpeg.tobytes()

if __name__ == "__main__":
    CAM = VideoCamera()
    while True:
        frame = CAM.get_frame()
        yield(b'--frame\r\n'
        b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

利用dlib进行人脸抓拍

使用dlib自带的frontal_face_detector作为我们的特征提取器

关键代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import dlib

# class VideoCamera(object):
# 此处省略

CAM = VideoCamera()
#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
while True:
        img = CAM.dlib_check()
        # 转为灰度图片
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # 使用detector进行人脸检测
        dets = detector(gray_img, 1)
        status = True
        # 进行图像抓取动作
        for i, d in enumerate(dets):
            x1 = d.top() if d.top() > 0 else 0
            y1 = d.bottom() if d.bottom() > 0 else 0
            x2 = d.left() if d.left() > 0 else 0
            y2 = d.right() if d.right() > 0 else 0

            face = img[x1:y1,x2:y2]
            face = cv2.resize(face,(64,64))

利用face_recognition进行人脸识别

从已知图片库中获取人脸特征信息

 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
import face_recognition
import dlib

def facerecog():
    lxp_image = face_recognition.load_image_file(os.getcwd() + "/know/lixueping.jpg")
    lxp_face_encoding = face_recognition.face_encodings(lxp_image)[0]

    # Create arrays of known face encodings and their names
    known_face_encodings = [
        lxp_face_encoding
    ]
    known_face_names = [
        "Li XuePing"
    ]

    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True

    CAM = VideoCamera()
    while True:
        frame_tmp = CAM.face_recognition()
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(frame_tmp)
            face_encodings = face_recognition.face_encodings(frame_tmp, face_locations)

            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"

                # # If a match was found in known_face_encodings, just use the first one.
                # if True in matches:
                #     first_match_index = matches.index(True)
                #     name = known_face_names[first_match_index]

                # Or instead, use the known face with the smallest distance to the new face
                face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_face_names[best_match_index]
                print('name check',name)
                face_names.append(name)
            print('face_names',face_names)
        process_this_frame = not process_this_frame

完整代码

  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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 图像处理
import cv2
import os
import time
import face_recognition
import numpy as np
import base64
import dlib
from django.conf import settings
from app.models import Pic

def cv2_base64(image):
    base64_str = cv2.imencode('.jpg',image)[1].tostring()
    base64_str = base64.b64encode(base64_str)
    # print('base64',base64_str)
    return base64_str

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)
    
    def __del__(self):
        self.video.release()

    def get_frame(self):
        ret,image = self.video.read()
        ret,jpeg = cv2.imencode('.jpg',image)
        return jpeg.tobytes()

    def face_recognition(self):
        # Grab a single frame of video
        ret, frame = self.video.read()
        
        # # Resize frame of video to 1/4 size for faster face recognition processing
        # small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        # rgb_small_frame = small_frame[:, :, ::-1]
        # return rgb_small_frame

        rgb_small_frame = frame[:, :, ::-1]
        return rgb_small_frame

    def dlib_check(self):
        # 从摄像头读取照片
        success, img = self.video.read()
        return img

CAM = VideoCamera()
#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()

def gen():
    while True:
        frame = CAM.get_frame()
        yield(b'--frame\r\n'
        b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

def facerecog():
    lxp_image = face_recognition.load_image_file(os.getcwd() + "/know/lixueping.jpg")
    lxp_face_encoding = face_recognition.face_encodings(lxp_image)[0]

    ff_image = face_recognition.load_image_file(os.getcwd() + "/know/ff.png")
    ff_face_encoding = face_recognition.face_encodings(ff_image)[0]

    y_image = face_recognition.load_image_file(os.getcwd() + "/know/ywm.jpg")
    y_face_encoding = face_recognition.face_encodings(y_image)[0]

    # Create arrays of known face encodings and their names
    known_face_encodings = [
        lxp_face_encoding,
        ff_face_encoding,
        y_face_encoding
        # obama_face_encoding,
        # biden_face_encoding
    ]
    known_face_names = [
        "Li XuePing",
        "FangFang",
        "YuWenMiao"
        # "Barack Obama",
        # "Joe Biden"
    ]

    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True

    # while True:
    #     frame = CAM.get_frame()
    #     yield(b'--frame\r\n'
    #     b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

    while True:
        frame_tmp = CAM.face_recognition()
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(frame_tmp)
            face_encodings = face_recognition.face_encodings(frame_tmp, face_locations)

            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"

                # # If a match was found in known_face_encodings, just use the first one.
                # if True in matches:
                #     first_match_index = matches.index(True)
                #     name = known_face_names[first_match_index]

                # Or instead, use the known face with the smallest distance to the new face
                face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_face_names[best_match_index]
                print('name check',name)
                face_names.append(name)
            print('face_names',face_names)
        process_this_frame = not process_this_frame


        # Display the results
        # for (top, right, bottom, left), name in zip(face_locations, face_names):
        #     # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        #     top *= 4
        #     right *= 4
        #     bottom *= 4
        #     left *= 4

        #     # Draw a box around the face
        #     cv2.rectangle(frame_tmp, (left, top), (right, bottom), (0, 0, 255), 2)

        #     # Draw a label with a name below the face
        #     cv2.rectangle(frame_tmp, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        #     font = cv2.FONT_HERSHEY_DUPLEX
        #     cv2.putText(frame_tmp, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        ret,jpeg = cv2.imencode('.jpg',frame_tmp)

        yield(b'--frame\r\n'
        b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n')

def dlibCheck():
    while True:
        img = CAM.dlib_check()
        # 转为灰度图片
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # 使用detector进行人脸检测
        dets = detector(gray_img, 1)
        status = True
        # 进行图像抓取动作
        for i, d in enumerate(dets):
            x1 = d.top() if d.top() > 0 else 0
            y1 = d.bottom() if d.bottom() > 0 else 0
            x2 = d.left() if d.left() > 0 else 0
            y2 = d.right() if d.right() > 0 else 0

            face = img[x1:y1,x2:y2]
            face = cv2.resize(face,(64,64))

            name = time.strftime("%Y%m%d%H%M%S")

            cv2.imwrite(os.path.join(settings.BASE_DIR, "static")+"/"+name+'.jpg',face)

            pic = Pic.objects.create(name=name)
            pic.resource = '抓拍'
            pic.path = os.path.join(settings.BASE_DIR, "static")+"/"+name+'.jpg'
            pic.base64 = cv2_base64(face)
            pic.save()

            print('dlib found person')
            ret,jpeg = cv2.imencode('.jpg',img)
            status = False
            yield(b'--frame\r\n'
            b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n')
        
        if status:
            # print('dlib not found')
            ret,jp = cv2.imencode('.jpg',gray_img)
            yield(b'--frame\r\n'
            b'Content-Type: image/jpeg\r\n\r\n' + jp.tobytes() + b'\r\n\r\n')