采用开源方案的实时人脸识别实验

作者: gaoshine | 来源:发表于2018-02-08 16:09 被阅读554次

    实时人脸识别实验

    采用开源方案的实时人脸识别实验,本方案采用大名鼎鼎的dlib库,使用深度学习的目前最高水平的人脸识别代码,号称此模型在使用LFW人脸库的测试中, 正确率可达到惊人的99.38%.

    原文:(Built using dlib's state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.)


    42c65360-025d-11e7-94ea-b12f28cb34b4.png

    本代码来自 Adam Geitgey 的 face_recognition开源项目,Adam Geitgey的face_recognition

    36f0e3f0-13cb-11e7-8258-4d0c9ce1e419.gif
    %matplotlib inline 
    import matplotlib.pyplot as plt
    from IPython import display
    import face_recognition
    import cv2
    
    # 初始化人员(姓名和照片)
    user_name = ['who','weixin','gaoshine','suowei']
    user_img = []
    user_encoding =[]
    
    for i in range(len(user_name)):
        mName = user_name[i]
        mImg = face_recognition.load_image_file("./data/%s.jpg" % mName)
        user_img.append(mImg)
        user_encoding.append(face_recognition.face_encodings(mImg)[0])
    
    
    # Create arrays of known face encodings and their names
    known_face_encodings = user_encoding
    known_face_names = user_name
    
    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    
    
    # 采集摄像头
    video_capture = cv2.VideoCapture(0)
    i = 0
    j = 10000
    while True:
        # 捕获单帧图像
        ret, frame = video_capture.read()
    
        # 将原图缩小到1/4大小用于人脸识别
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    
        # 颜色空间转换BGR转换为RGB,opencv使用的是BGR,而face_recognition使用了RGB 
        rgb_small_frame = small_frame[:, :, ::-1]
     
    
            
        # 处理每一帧的图像
        if process_this_frame:
            # 从当前帧中查找和识别人脸
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
    
            face_names = []
            for face_encoding in face_encodings:
                # 试图从已知的人脸库中匹配, matches返回的独热码来比对那个user_name = ['weixin','gaoshine','suowei'] 
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"
                
                # 如果匹配到即采用第一个
                if True in matches:
                    first_match_index = matches.index(True)
                    name = known_face_names[first_match_index]
    
                face_names.append(name)
    
        process_this_frame = not process_this_frame
    
    
        # 显示结果
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            # 图像尺寸回复原大小(X4)
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4
    
            # 在人脸画方框标注
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
    
            # 在人脸方框上写标注
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
    
        # 显示图像
        cv2.imshow('Video', frame)
        
        i = i + 1
        if i%25:
            j = j + 1
            # 将原图缩小到1/2大小保存
            save_frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
            cv2.cv2.imwrite("./output/%d.jpg" % j, save_frame)
        
        # 按 'q' 退出!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # 释放摄像头资源
    video_capture.release()
    cv2.destroyAllWindows()
    

    以上的代码,简单到发指.
    不过我测试了一下,看看效果并不理性啊,看来呢个LFW的那个基准测试已经不那么权威了.
    下一步看看这个face_recognition的一些具体参数,了解一下能不能提升一下识别率.
    下面是我们测试时图片:


    Gif-2018-04-08-16-04-33.gif

    相关文章

      网友评论

      本文标题:采用开源方案的实时人脸识别实验

      本文链接:https://www.haomeiwen.com/subject/aemttftx.html