使用OpenCV Python进行人脸识别
对图像分类的基本理解 Python 和深度学习知识 对深度学习中各种模块的概念理解
介绍
目录
人脸检测概述 人脸识别概述 了解什么是OpenCV 使用Python实现
人脸检测概述
人脸识别概述
1.人脸检测
2. 特征提取
3. 人脸对比
了解什么是 OpenCV
OpenCV 的优点:
Open CV 是免费的,是一个开源库。 Open CV 速度快,因为它是用 C/C++ 语言编写的 系统 RAM 越少,OpenCV 效果越好。 支持大部分操作系统,如Windows、Linux、macOS。
执行
OpenCV dlib Face_recognition
从人脸中提取特征
import face_recognition
import pickle
import cv2
import os
#Images here that contains data(folders of various people)
imagePath = list(paths.list_images('Images'))
kEncodings = []
kNames = []
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)
# 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)
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 imutils #imutils includes opencv functions
import pickle
import time
import cv2
import os
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())
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)
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)
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)
输出
赞 (0)