News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Python 人脸识别系统

posted on 2023-05-21 16:40     read(549)     comment(0)     like(13)     collect(5)


Introduction

Face recognition is different from face detection. In face detection, we only detected the location of the face, and in face recognition task, we identified the identity of the person.

This article focuses on implementing face recognition using the library face_recognition, which is based on deep learning techniques and promises over 96% accuracy using a single training image.

insert image description here
Identify system use cases

  • looking for missing person
  • Identify accounts on social media
  • Identify the driver in the car
  • Attendance System

Learn how facial recognition works

  1. We pass a picture of the person and their name to the model.
  2. The model takes each photo, converts them to some sort of digital encoding, and stores them in a list, and stores all the labels (people's names) in another list.
  3. In the prediction phase, when we pass a picture of an unknown person, the recognition model converts the image of that person into an encoding.
  4. After converting an image of an unknown person into a code, it tries to find the most similar code based on the distance parameter. The code with the smallest distance to the unknown's code will be the closest match.
  5. After getting the closest matching encoding, we get the index of that encoding from this list and use the index. We find the name of the detected person.

Traditional Face Recognition Algorithms

Traditional face recognition algorithms do not meet modern face recognition standards. They are designed to recognize faces using old legacy algorithms.

OpenCV provides some traditional facial recognition algorithms.

These methods differ in the way they extract image information and match input and output images.

The LBPH algorithm is a simple but very effective method that is still in use, but is slower compared to modern algorithms.
insert image description here

Deep Learning for Face Recognition

There are various deep learning based facial recognition algorithms available.

  • DeepFace
  • DeepID series of systems
  • FaceNet
  • VGGFace

Generally speaking, a landmark-based face recognizer takes an image of a face and tries to find basic feature points such as eyebrows, mouth corners, eyes, nose, lips, etc. There are more than 60 landmarks.
insert image description here

Steps Involved in Face Recognition

  1. Face detection: locate faces, note the coordinates where each face is located, and draw a bounding box around each face.
  2. Face alignment. Normalize faces for fast training.
  3. feature extraction. Extract local features from facial pictures for training, and different algorithms perform different operations in this step.
  4. face recognition. Match an input face to one or more known faces in our dataset.
    insert image description here

Identification process

Use python to implement a face recognition system. Use the face_recognition library to implement a face recognition system based on deep learning.

1. Set up the face recognition library:

In order to install the face recognition library, we need to install dlib first

  • dlib: It is a modern C++ toolkit containing algorithms and tools related to machine learning.

pip install dlib

  • Install the actual face recognition library face recognition.

pip install face recognition

  • Opencv用于一些图像预处理

pip install opencv
Note: Sometimes installing dlib throws error in that case install install the C++ development toolkit using vs_code community .

导入库

import cv2
import numpy as np
import face_recognition

2. 加载图片:

我们完成了库的安装和导入。是时候将一些示例图像加载到face_recognition库中了。

该face_recognition库仅支持 BGR 格式的图像。在打印输出图像时,我们应该使用 OpenCV 将其转换为 RGB。

Face_recognition仅加载 BGR 格式的图像。

import cv2
import numpy as np
import face_recognition
img_bgr = face_recognition.load_image_file('student_images/modi.jpg')
img_rgb = cv2.cvtColor(img_bgr,cv2.COLOR_BGR2RGB)
cv2.imshow('bgr', img_bgr)
cv2.imshow('rgb', img_rgb)
cv2.waitKey

输出→ BGR 与 RGB

insert image description here

3.检测和定位人脸:

face_recognition库可以自行快速定位人脸,我们不需要使用haar_cascade或其他技术。

img_modi=face_recognition.load_image_file('student_images/modi.jpg')
img_modi_rgb = cv2.cvtColor(img_modi,cv2.COLOR_BGR2RGB)
#--------- Detecting Face -------
face = face_recognition.face_locations(img_modi_rgb)[0]
copy = img_modi_rgb.copy()
# ------ Drawing bounding boxes around Faces------------------------
cv2.rectangle(copy, (face[3], face[0]),(face[1], face[2]), (255,0,255), 2)
cv2.imshow('copy', copy)
cv2.imshow('MODI',img_modi_rgb)
cv2.waitKey(0)

4. 样本图像识别:

该face_recognition库基于深度学习,它支持单次学习,这意味着它需要一张图片来训练自己检测一个人。

img_modi = face_recognition.load_image_file('student_images/modi.jpg')
img_modi = cv2.cvtColor(img_modi,cv2.COLOR_BGR2RGB)
#------to find the face location
face = face_recognition.face_locations(img_modi)[0]
#--Converting image into encodings
train_encode = face_recognition.face_encodings(img_modi)[0]
#----- lets test an image
test = face_recognition.load_image_file('student_images/modi2.jpg')
test = cv2.cvtColor(test, cv2.COLOR_BGR2RGB)
test_encode = face_recognition.face_encodings(test)[0]
print(face_recognition.compare_faces([train_encode],test_encode))
cv2.rectangle(img_modi, (face[3], face[0]),(face[1], face[2]), (255,0,255), 1)
cv2.imshow('img_modi', img_modi)
cv2.waitKey(0)

上面的代码拍了两张总理的照片,因为两张照片都是同一个人,所以它返回了true。

  • face_recognition.face_encodings(imgelon)[0]→返回传递图像的编码。
  • face_recognition.compare_faces([train_encode],test_encode) 获取经过训练的编码列表和未知图像的测试编码。如果两个测试编码在训练编码中匹配,则返回True ;否则,它会返回False.

挑战

  • 姿势:识别系统容易受到人体姿势的影响。面部识别系统将无法预测该人的面部是否不可见。
  • Lighting: Lighting can drastically alter facial contours. Photos used for facial recognition should be taken under appropriate lighting conditions.
  • Facial expressions: Different facial expressions lead to different predictions for images of the same person.
  • Low resolution: Low resolution images contain less information, so they are not suitable for face recognition training.


Category of website: technical article > Blog

Author:kimi

link:http://www.pythonblackhole.com/blog/article/25267/85edf7c957a96eb5746f/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

13 0
collect article
collected

Comment content: (supports up to 255 characters)