介绍在这篇文章中,我们将看看什么是人脸识别?以及它与人脸检测有何不同?我们先简单了解一下人脸识别的原理,然后跳转到编码部分!在本文的最后,你将能够开发一个人脸识别程序来识别图像中的人脸!目录
人脸检测概述如果机器能够在没有人工干预的情况下自动检测图像中的物体会怎样?让我们看看:人脸检测是我们在图像中检测人脸的问题。人脸可能略有不同,但可以肯定地说,所有人脸都有特定的特征。存在各种人脸检测算法, Viola-Jones算法是现在也在使用的最古老的方法。人脸检测通常是迈向许多与人脸相关的应用程序(如人脸识别或人脸验证)的第一步。但是,人脸检测有非常有用的应用。人脸检测最成功的应用之一可能是“拍照”。示例:当你单击朋友的照片时,内置人脸检测算法的相机会检测人脸所在的位置并相应地调整焦距。人脸识别概述现在我们已经看到我们的算法可以检测人脸,但我们还能识别出谁的脸吗?如果算法能够识别人脸呢?一般来说,人脸识别是一种通过人脸识别或验证个人身份的方法。各种算法可用于人脸识别,但它们的准确性可能会有所不同。在这里,我将与你讨论如何使用深度学习进行人脸识别。现在让我们了解如何使用深度学习识别人脸。在这里,我们使用人脸嵌入,其中每个人脸都被转换为向量。将人脸转换为向量的技术称为深度度量学习。让我把这个过程分成三个简单的步骤,以便更好、更容易地理解。1.人脸检测我们执行的第一个任务是检测图像(照片)或视频流中的人脸。现在我们知道人脸的确切坐标/位置,所以我们提取这张人脸进行进一步处理。2. 特征提取现在看到我们已经从图像中裁剪掉了人脸,因此我们从中提取了特定的特征。在这里,我们将看到如何使用人脸嵌入来提取人脸的这些特征。正如我们所知,神经网络将人脸图像作为输入,并输出一个表示人脸最重要特征的向量!在机器学习中,这个向量不过是所谓的嵌入,因此我们称之为向量人脸嵌入。现在这将如何帮助识别不同人的面孔?当我们训练神经网络时,网络学习为看起来相似的人脸输出相似的向量。让我们考虑一个例子,如果我在不同的时间间隔内有多个人脸图像,很明显某些特征可能会改变但不会太多。所以在这个问题中,与人脸相关的向量是相似的,或者我们可以说它们在向量空间中非常接近。至此,我们已经了解了这个网络是如何工作的,让我们看看如何在我们自己的数据上使用这个网络。在这里,我们将数据中的所有图像传递给这个预训练的网络,以获取相应的嵌入并将这些嵌入保存在文件中以备下一步使用。3. 人脸对比我们在文件中保存了数据中每个人脸的人脸嵌入,下一步是识别不在我们数据中的新图像。因此,第一步是使用我们之前使用的相同网络计算图像的面部嵌入,然后将此嵌入与我们拥有的其余嵌入进行比较。如果生成的嵌入与任何其他嵌入更接近或相似,我们就能识别出人脸。了解什么是 OpenCV在人工智能领域,计算机视觉是最有趣和最具挑战性的任务之一。计算机视觉充当计算机软件和可视化之间的桥梁。计算机视觉允许计算机软件理解和学习周围的视觉效果。让我们来了解一个例子:根据形状、颜色和大小决定水果。这个任务对人脑来说很容易,但是在计算机视觉中,首先我们需要收集数据,然后执行数据处理操作,然后训练和教授模型如何根据水果的大小、形状和颜色来区分水果。如今,有各种软件包可用于执行机器学习、深度学习和计算机视觉问题。到目前为止,计算机视觉是解决此类复杂问题的最佳模块。OpenCV是一个开源库。它被不同的编程语言如 R、Python 等支持。它可能运行在大多数平台上,如 Windows、Linux 和 macOS。OpenCV 的优点:- Open CV 速度快,因为它是用 C/C++ 语言编写的
- 支持大部分操作系统,如Windows、Linux、macOS。
执行在本节中,我们将使用 OpenCV 和 Python 实现人脸识别。 OpenCV是一个视频和图像处理库,用于图像和视频分析,如面部检测、车牌读取、照片编辑、高级机器人视觉等。DLIB库包含我们实现的“深度度量学习”,用于构建实际识别过程的人脸嵌入。face_recognition库非常容易使用,我们将在我们的代码使用这个。首先,在安装face_recognition之前记得先安装dlib库。使用 OpenCV 的任何人脸识别应用程序的输出将如下所示:要安装 OpenCV、dlib 和face_recognition,请在命令提示符中键入以下代码段。pip install opencv-python
conda install -c conda-forge dlib
pip install face_recognition
从人脸中提取特征首先,你需要一个数据集,甚至创建你自己的数据集。只需确保将所有图像排列在文件夹中,每个文件夹仅包含一个人的图像。或者你可以从此链接下载数据集:https://www./atulanandjha/lfwpeople现在,将数据集保存在与你要制作文件相同的文件夹中。from imutils import paths #imutils includes opencv functions import face_recognition import pickle import cv2 import os
#get paths of each file in folder named Images #Images here that contains data(folders of various people) imagePath = list(paths.list_images('Images')) kEncodings = [] kNames = []
# loop over the image paths for (i, ip) in enumerate(imagePath): # extract the person name from the image path name = ip.split(os.path.sep)[-2] # load the input image and convert it from BGR image = cv2.imread(ip) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(rgb,model='hog') # compute the facial embedding for the any face encodings = face_recognition.face_encodings(rgb, boxes) # loop over the encodings for encoding in encodings: kEncodings.append(encoding) kNames.append(name)
#save emcodings along with their names in dictionary data data = {'encodings': kEncodings, 'names': kNames} #use pickle to save data into a file for later use f = open('face_enc', 'wb') f.write(pickle.dumps(data))#to open file in write mode f.close()#to close file
如何识别图像中的人脸以下脚本用于检测和识别图像中的人脸。我在需要的地方给出了注释。因此,你将能够有效地理解代码。import face_recognition import imutils #imutils includes opencv functions import pickle import time import cv2 import os
#to find path of xml file containing haarCascade file cfp = os.path.dirname(cv2.__file__) + '/data/haarcascade_frontalface_alt2.xml' # load the harcaascade in the cascade classifier fc = cv2.CascadeClassifier(cfp) # load the known faces and embeddings saved in last file data = pickle.loads(open('face_enc', 'rb').read())
#Find path to the image you want to detect face and pass it here image = cv2.imread(Path-to-img) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #convert image to Greyscale for HaarCascade gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = fc.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6, minSize=(60, 60), flags=cv2.CASCADE_SCALE_IMAGE)
# the facial embeddings for face in input encodings = face_recognition.face_encodings(rgb) names = [] # loop over the facial embeddings incase # we have multiple embeddings for multiple fcaes for encoding in encodings: #Compare encodings with encodings in data['encodings'] #Matches contain array with boolean values True and False matches = face_recognition.compare_faces(data['encodings'], encoding) #set name =unknown if no encoding matches name = 'Unknown' # check to see if we have found a match if True in matches: #Find positions at which we get True and store them matchedIdxs = [i for (i, b) in enumerate(matches) if b] count = {} # loop over the matched indexes and maintain a count for # each recognized face face for i in matchedIdxs: #Check the names at respective indexes we stored in matchedIdxs name = data['names'][i] #increase count for the name we got count[name] = count.get(name, 0) + 1 #set name which has highest count name = max(count, key=count.get) # will update the list of names names.append(name)
# do loop over the recognized faces for ((x, y, w, h), name) in zip(faces, names): # rescale the face coordinates # draw the predicted face name on the image cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(image, name, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) cv2.imshow('Frame', image) cv2.waitKey(0)
输出
|