本章介绍以下主题:
近年来,人脸识别已经成为最热门的研究领域之一。人脸识别系统是一种能够检测和识别人脸的计算机程序。为了识别一个人,它会考虑他们独特的面部特征。最近,它已被用于若干安全和监视装置,以确保高风险区域、住宅区、私人和公共建筑等的安全。
在本节中,我们将讨论如何从网络摄像头图像中检测人脸。USB 网络摄像头需要连接到 Raspberry Pi 3 以实现实时人脸检测。
import cv2
import numpy as np
frontalface_cascade= cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
if frontalface_cascade.empty():
raiseIOError('Unable to load the face cascade classifier xml file')
capture = cv2.VideoCapture(0)
scale_factor = 0.5
# Loop until you hit the Esc key
while True:
ret, frame = capture.read()
frame = cv2.resize(frame, None, fx=scale_factor, fy=scale_factor,
interpolation=cv2.INTER_AREA)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_rectangle = frontalface_cascade.detectMultiScale(gray_image, 1.3, 5)
for (x,y,w,h) in face_rectangle:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
cv2.imshow('Face Detector', frame)
a = cv2.waitKey(1)
if a == 10:
break
capture.release()
cv2.destroyAllWindows()
在人脸检测系统中获得的结果如下所示:
人脸识别是在人脸检测之后进行的一种技术。将检测到的人脸与数据库中存储的图像进行比较。它从输入图像中提取特征,并将其与数据库中存储的人体特征相匹配。
import cv2
import numpy as np
from sklearn import preprocessing
class LabelEncoding(object):
# Method to encode labels from words to numbers
def encoding_labels(self, label_wordings):
self.le = preprocessing.LabelEncoder()
self.le.fit(label_wordings)
def word_to_number(self, label_wordings):
return int(self.le.transform([label_wordings])[0])
def number_to_word(self, label_number):
return self.le.inverse_transform([label_number])[0]
def getting_images_and_labels(path_input):
label_wordings = []
for roots, dirs, files in os.walk(path_input):
for fname in (x for x in files if x.endswith('.jpg')):
fpath = os.path.join(roots, fname)
label_wordings.append(fpath.split('/')[-2])
images = []
le = LabelEncoding()
le.encoding_labels(label_wordings)
labels = []
# Parse the input directory
for roots, dirs, files in os.walk(path_input):
for fname in (x for x in files if x.endswith('.jpg')):
fpath = os.path.join(roots, fname)
img = cv2.imread(fpath, 0)
names = fpath.split('/')[-2]
face = faceCascade.detectMultiScale(img, 1.1, 2, minSize=(100,100))
for (x, y, w, h) in face:
images.append(img[y:y+h, x:x+w])
labels.append(le.word_to_number(names))
return images, labels, le
if __name__=='__main__':
path_cascade = "haarcascade_frontalface_alt.xml"
train_img_path = 'faces_dataset/train'
path_img_test = 'faces_dataset/test'
faceCascade = cv2.CascadeClassifier(path_cascade)
face_recognizer = cv2.createLBPHFaceRecognizer()
imgs, labels, le = getting_images_and_labels(train_img_path)
print "nTraining..."
face_recognizer.train(imgs, np.array(labels))
print 'nPerforming prediction on test images...'
flag_stop = False
for roots, dirs, files in os.walk(path_img_test):
for fname in (x for x in files if x.endswith('.jpg')):
fpath = os.path.join(roots, fname)
predicting_img = cv2.imread(fpath, 0)
# Detect faces
face = faceCascade.detectMultiScale(predicting_img, 1.1,
2, minSize=(100,100))
# Iterate through face rectangles
for (x, y, w, h) in face:
# Predict the output
index_predicted, config = face_recognizer.predict(
predicting_img[y:y+h, x:x+w])
# Convert to word label
person_predicted = le.number_to_word(index_predicted)
# Overlay text on the output image and display it
cv2.putText(predicting_img, 'Prediction: ' + person_predicted,
(10,60), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), 6)
cv2.imshow("Recognizing face", predicting_img)
a = cv2.waitKey(0)
if a == 27:
flag = True
break
if flag_stop:
break
获得的人脸识别输出如下所示:
人脸识别系统广泛用于实现个人安全系统。读者可以参考文章基于 OpenCV的人脸检测系统http://ieeexplore.ieee.org/document/6242980/ 。
另见实时人脸检测系统人脸检测算法研究中的http://ieeexplore.ieee.org/document/5209668 。
请参阅以下文章:
人脸识别广泛应用于安全、医疗和营销领域。各行业正在开发新的人脸识别系统,利用深度学习识别欺诈行为,识别人脸和照片之间的差异,等等。在医疗保健领域,人脸识别与其他计算机视觉算法相结合,用于检测面部皮肤疾病。