第五章 人脸检测和识别
1 haar级联的概念
2 获取haar级联数据
在opencv源码中data/haarcascades目录下存放了用于人脸检测的xml文件.用于检测静止图像,视频和摄像头中的人脸
用于人脸眼睛 鼻子和嘴的跟踪
haarcascade_profileface
haarcascade_smile
haarcascade_russian_plate_number
haarcascade_upperbody
haarcascade_righteye_2splits
3 使用opencv进行人脸检测
3.1 静态图像中的人脸检测
示例代码如下:
import cv2 filename='../zz.jpg' def detect(filename): #用于人脸检测xml face_cascade=cv2.CascadeClassifier('../cascades/haarcascade_frontalface_default.xml') #读入图像 img=cv2.imread(filename) #更换颜色空间 gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces=face_cascade.detectMultiScale(gray, 1.3,#图像的压缩率 5)#人脸矩形保留邻近数目的最小值 #在原始图像上绘制蓝色矩形 for (x,y,w,h) in faces: img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) cv2.namedWindow('Vikings Detected!!') cv2.imshow("Vikings Detected!!",img) cv2.imwrite('../vikings.jpg',img) cv2.waitKey(0) if __name__=='__main__': detect(filename)
执行结果:老同桌的脸居然识别不出来
3.2 视频中的人脸检测
示例代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2016/12/3 13:36 # @Author : Retacn # @Site : 视频中的人脸检测 # @File : fact_detection_video.py # @Software: PyCharm __author__ = "retacn" __copyright__ = "property of mankind." __license__ = "CN" __version__ = "0.0.1" __maintainer__ = "retacn" __email__ = "zhenhuayue@sina.com" __status__ = "Development" import cv2 def detect(): # 加载haar级联文件 face_cascade = cv2.CascadeClassifier('../cascades/haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('../cascades/haarcascade_eye.xml') camera = cv2.VideoCapture(0) # 捕获视频帧 while (True): ret, frame = camera.read() # 转换颜色空间 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: #绘制检测到的人脸矩形 img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # roi_gray = gray[y:y + h, x:x + w] #检测眼睛 # eyes = eye_cascade.detectMultiScale(roi_gray, 1.03, 5, 0, (40, 40)) eyes = eye_cascade.detectMultiScale(img, 1.03, 5, 0, (40, 40)) #绘制检测到的眼睛矩形 for (ex, ey, ew, eh) in eyes: cv2.rectangle(img, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) #print(x,y,w,h,ex,ey,ew,eh) #显示图像 cv2.imshow('Camers', frame) #按q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': detect()
3.3 人脸视别
人脸数据库:
The yale facedatabase
http://version.ucsd.edu/content/yale-face-database
The AT&T
http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html
The extendedyale or yale b
http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html
A 生成人脸数据
生成图像角本
图像像是灰度格式的.pgm文件,形状为正方式,大小要一样
示例代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2016/12/3 15:14 # @Author : Retacn # @Site : 生成人脸识别数据 # @File : createData.py # @Software: PyCharm __author__ = "retacn" __copyright__ = "property of mankind." __license__ = "CN" __version__ = "0.0.1" __maintainer__ = "retacn" __email__ = "zhenhuayue@sina.com" __status__ = "Development" import cv2 def generate(): face_cascade = cv2.CascadeClassifier('../cascades/haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('../cascades/haarcascade_eye.xml') camera = cv2.VideoCapture(0) count = 0 while (True): ret, frame = camera.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) f = cv2.resize(gray[y:y + h, x:x + w], (200, 200)) cv2.imwrite('../data/at/retacn/%s.pgm' % str(count), f) count += 1 cv2.imshow("Camera", frame) if cv2.waitKey(33) & 0xFF == ord("q"): break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': generate()
B 人脸视别
C 准备训练数据
生成csv文件的方法,示例代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2016/12/3 15:14 # @Author : Retacn # @Site : 生成人脸识别数据 # @File : createData.py # @Software: PyCharm __author__ = "retacn" __copyright__ = "property of mankind." __license__ = "CN" __version__ = "0.0.1" __maintainer__ = "retacn" __email__ = "zhenhuayue@sina.com" __status__ = "Development" import cv2 def generate(): face_cascade = cv2.CascadeClassifier('../cascades/haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('../cascades/haarcascade_eye.xml') camera = cv2.VideoCapture(0) count = 0 while (True): ret, frame = camera.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) f = cv2.resize(gray[y:y + h, x:x + w], (200, 200)) cv2.imwrite('../data/at/retacn/%s.pgm' % str(count), f) count += 1 cv2.imshow("Camera", frame) if cv2.waitKey(33) & 0xFF == ord("q"): break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': generate()
D 加载数据并识别人脸
示例代码如下:
# 加载识别数据 def read_image(path, sz=None): c = 0 x, y = [], [] for dirname, dirnames, filenames in os.walk(path): for subdirname in dirnames: subject_path = os.path.join(dirname, subdirname) for filename in os.listdir(subject_path): try: if (filename == '.directory'): continue filepath = os.path.join(subject_path, filename) img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) if (sz is not None): im = cv2.resize(im, (200, 200)) x.append(np.asanyarray(im, dtype=np.uint8)) y.append(c) except IOError as ioe: print(ioe) except: print('Unexpected error:', sys.exc_info()[0]) raise c += 1 return [x, y]
E 基于eignfaces的人脸识别
示例代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2016/12/3 22:07 # @Author : Retacn # @Site : TODO 需要重新编译opencv扩展包,人脸检测与识别 # @File : face_detection.py # @Software: PyCharm __author__ = "retacn" __copyright__ = "property of mankind." __license__ = "CN" __version__ = "0.0.1" __maintainer__ = "retacn" __email__ = "zhenhuayue@sina.com" __status__ = "Development" import cv2 import numpy as np import sys import os.path # 生成人脸识别数据 def generate(): face_cascade = cv2.CascadeClassifier('../data/cascades/haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('../data/cascades/haarcascade_eye.xml') # 读取摄像头视频数据 camera = cv2.VideoCapture(0) count = 0 while (True): ret, frame = camera.read() # 更换颜色空间 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) f = cv2.resize(gray[y:y + h, x:x + w], (200, 200)) cv2.imwrite('../data/at/retacn/%s.pgm' % str(count), f) count += 1 cv2.imshow("Camera", frame) if cv2.waitKey(1000 / 12) & 0xFF == ord("q"): break camera.release() cv2.destroyAllWindows() # 加载识别数据 def read_image(path, sz=(200, 200)): c = 0 x, y = [], [] for dirname, dirnames, filenames in os.walk(path): for subdirname in dirnames: subject_path = os.path.join(dirname, subdirname) for filename in os.listdir(subject_path): try: if (filename == '.directory'): continue filepath = os.path.join(subject_path, filename) img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) if (sz is not None): img = cv2.resize(img, (200, 200)) x.append(np.asarray(img, dtype=np.uint8)) y.append(c) except IOError as ioe: print(ioe) except: print('Unexpected error:', sys.exc_info()[0]) raise c += 1 return [x, y] # 基于eigenfaces的人脸识别 def face_rec(): names = ['Yue', 'Retacn', 'Three'] # 需要输入样本数据的存放路径 if len(sys.argv) < 2: print('USAGE: facerec_defo.py </path/to/images> [/path/to/store/images/at]') sys.exit() # 读入图像数组,第二个参数为样本图像的存放位置 [x, y] = read_image(sys.argv[1]) y = np.asarray(y, dtype=np.int32) # [x, y] = read_image('D:/workspace_pycharm/opencv3_python/data/at') # 如果有三个参数,则将第三个参数设为输出目录 if len(sys.argv) == 3: out_dir = sys.argv[2] # 创建人脸识别模型 # model = cv2.face.createEigenFaceRecognizer() # 基于Fisherfaces的人脸识别 # model = cv2.face.createFisherFaceRecognizer() # 基于lbph的人脸识别 model = cv2.face.createLBPHFaceRecognizer() model.train(np.asarray(x), np.asarray(y)) camera = cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier('../data/cascades/haarcascade_frontalface_default.xml') while (True): read, img = camera.read() faces = face_cascade.detectMultiScale(img, 1.3, 5) for (x, y, w, h) in faces: img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) roi = gray[x:x + w, y:y + h] try: roi = cv2.resize(roi, (200, 200), interpolation=cv2.INTER_LINEAR) # roi = cv2.resize(gray, (200, 200), interpolation=cv2.INTER_LINEAR) # 置信度评分 params = model.predict(roi) print(params) print('Lable:%s,Confidence:%02f' % (params[0], params[1])) cv2.putText(img, names[params[0]], (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) except: continue cv2.imshow("camera", img) if cv2.waitKey(33) & 0xFF == ord('q'): break cv2.destroyAllWindows() if __name__ == '__main__': # generate() # [X, y] = read_image('D:/workspace_pycharm/opencv3_python/data/at') face_rec() # generate()
F 基于fisherfaces的人脸识别
#基于Fisherfaces的人脸识别
#model=cv2.face.createFisherFaceRecognizer()
G 基于LBPH的人脸识别
#基于lbph的人脸识别 # model=cv2.face.createLBPHFaceRecognizer()
H 通过置信度评分来丢弃结果
Predict()