Realize image recognition based on face_recognition

Face Recognition is a Python-based face recognition library. It also provides a command line tool that allows you to perform face recognition operations on images in any folder through the command line.

The library is built using dlib's top-notch deep learning face recognition technology, and has an accuracy rate of 99.38% on the outdoor face detection database benchmark (Labeled Faces in the Wild benchmark).

This blog post uses face_recognition to implement a simple picture recognition.

The pictures to be recognized are in the unknow_people folder: Fan Bingbing, Yang Ying, Liu Yifei, Di Lieba, Deng Chao.

The pictures recognized by the computer are in the know_people folder: Liu Yifei and Di Lieba

Use face_recognition to compare the pictures in the unknow_people folder with the pictures in the know_people folder for picture recognition.

import face_recognition
import os

# 获取图片名列表
# 只读取文件名以'.png'或'.jpg'或'.jpeg'结尾的图片
unknow_people_list = [i for i in os.listdir('unknow_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))]
know_people_list = [i for i in os.listdir('know_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))]


def face_select():
    # 定义flag
    flag = 0
    for unknow_people in unknow_people_list:
        # 读取待识别图片
        unknow = face_recognition.load_image_file('unknow_people/' + unknow_people)
        # 待识别图片转化为特征向量
        unknow_encode = face_recognition.face_encodings(unknow)[0]
        for know_people in know_people_list:
            # 读取计算机已经认识的图片
            know = face_recognition.load_image_file('know_people/'+know_people)
            # 图片转化为特征向量
            know_encode = face_recognition.face_encodings(know)[0]
            # 两张图片进行比较
            # tolerance刻画了比较的难易程度,值越小越难
            res = face_recognition.compare_faces([know_encode],unknow_encode,tolerance=0.5)
            if res[0]:
                flag = 1
                break
            else:
                flag = 0
        if flag == 1:
            print(f'匹配{unknow_people.split(".")[0]}')
        else:
            print(f'未匹配{unknow_people.split(".")[0]}')


if __name__ == '__main__':
    face_select()

The results are as follows:

The result of the operation is as expected. There are only Liu Yifei and Di Lieba in the know_people folder, so the code only matches Liu Yifei and Di Lieba but not Deng Chao, Yang Ying and Fan Bingbing.

I am also a little white of computer vision, and I hope I can continue to learn from everyone and make continuous progress~

Guess you like

Origin blog.csdn.net/gf19960103/article/details/89914024