1
1.2 对原代码进行注释,调试,增加PIL法显示中文标示。
1.3 获取摄像头实现动态人脸识别。
1.4 分为:侦测-收集-训练-识别。
2 准备:
=====
2.1 安装opencv:
pip install opencv-python
2.2 注意:导入模块
import cv2 #cv2不是版本号
科普一下:
cv2中的 2 不是指定发布的版本号,而是为了区分OpenCV的 C 和 C++ 的版本。
OpenCV1.x 使用 C 开发;而OpenCV2.x 使用C++。
2.3 环境:
华为笔记本电脑、深度deepin-linux操作系统、谷歌浏览器、python3.8和微软vscode编辑器。
3 文件结构:
========
3.1 图:
3.2 层次示意图:
3.3 两个xml文件来自:分类器一般位于安装包cv2下
比如:本机:file:///usr/local/python3.8/lib/python3.8/site-packages/cv2/data下,复制过来即可
===以下代码基于笔记本电脑的摄像头,需打开,训练自己头像===
4 五个代码依次进行:
===============
4.1 1-FaceDetection.py代码:
#人脸检测import numpy as npimport cv2# 人脸识别分类器faceCascade = cv2.CascadeClassifier('/home/xgj/Desktop/face-de/haarcascade_frontalface_default.xml')# 识别眼睛的分类器eyeCascade = cv2.CascadeClassifier('/home/xgj/Desktop/face-de/haarcascade_eye.xml')# 开启摄像头cap = cv2.VideoCapture(0)ok = Trueresult =[] #原bug,自己补充while ok: # 读取摄像头中的图像,ok为是否读取成功的判断参数 ok, img = cap.read() # 转换成灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 人脸检测 faces = faceCascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=5, minSize=(32, 32) ) result = [] # 在检测人脸的基础上检测眼睛 for (x, y, w, h) in faces: fac_gray = gray[y: (y+h), x: (x+w)] result = [] eyes = eyeCascade.detectMultiScale(fac_gray, 1.3, 2) # 眼睛坐标的换算,将相对位置换成绝对位置 for (ex, ey, ew, eh) in eyes: result.append((x+ex, y+ey, ew, eh)) # 画矩形框--脸部 for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) #眼睛 for (ex, ey, ew, eh) in result: cv2.rectangle(img, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2) #显示 cv2.imshow('video', img) #退出定义 k = cv2.waitKey(1) if k == 27: # press 'ESC' to quit breakcap.release()cv2.destroyAllWindows()
===注意4.1代码不需要也没关系===
4.2 2-FaceDataCollect.py代码:
#FaceDataCollect,人脸数据收集import cv2import os# 调用笔记本内置摄像头,所以参数为0,如果有其他的摄像头可以调整参数为1,2cap = cv2.VideoCapture(0)#注意路径face_detector = cv2.CascadeClassifier('/home/xgj/Desktop/face-de/haarcascade_frontalface_default.xml')#请输入id:0为一个人,第二个人请输入1,在4py中检测识别中idnums有用face_id = input(' enter user id:')print(' Initializing face capture. Look at the camera and wait ...')count = 0while True: # 从摄像头读取图片 sucess, img = cap.read() # 转为灰度图片 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = face_detector.detectMultiScale(gray, 1.3, 5) #面部画框 for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+w), (255, 0, 0)) count += 1 # 保存图像,注意路径 cv2.imwrite("/home/xgj/Desktop/face-de/img/user." + str(face_id) + '.' + str(count) + '.jpg', gray[y: y + h, x: x + w]) cv2.imshow('image', img) # 保持画面的持续。 k = cv2.waitKey(1) if k == 27: # 通过esc键退出摄像 break elif count >= 1000: # 得到1000个样本后退出摄像,可自定义数值大小 break# 关闭摄像头cap.release()cv2.destroyAllWindows()#大概需要半个小时,收集1000张图片#我自己约5分钟后暂停,期间可以做各种面部动作,我大概收集50张
4.3 3-face_training.py代码:
#face_training,人脸数据训练import numpy as npfrom PIL import Imageimport osimport cv2# 人脸数据路径,上面保存的灰色照片数据集path = '/home/xgj/Desktop/face-de/img'recognizer = cv2.face.LBPHFaceRecognizer_create()detector = cv2.CascadeClassifier("/home/xgj/Desktop/face-de/haarcascade_frontalface_default.xml")def getImagesAndLabels(path): imagePaths = [os.path.join(path, f) for f in os.listdir(path)] faceSamples = [] ids = [] for imagePath in imagePaths: PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale img_numpy = np.array(PIL_img, 'uint8') id = int(os.path.split(imagePath)[-1].split(".")[1]) faces = detector.detectMultiScale(img_numpy) for (x, y, w, h) in faces: faceSamples.append(img_numpy[y:y + h, x: x + w]) ids.append(id) return faceSamples, idsprint('Training faces. It will take a few seconds. Wait ...')faces, ids = getImagesAndLabels(path)recognizer.train(faces, np.array(ids))#保存训练好的文件recognizer.write('/home/xgj/Desktop/face-de/face_trainer/trainer.yml')print("{0} faces trained. Exiting Program".format(len(np.unique(ids))))
4.4 人脸识别:
==========
4.4.1 英文版的人脸识别4-face_recognition.py代码:
#face_recognition 人脸检测并识别,显示人名import cv2recognizer = cv2.face.LBPHFaceRecognizer_create()recognizer.read('/home/xgj/Desktop/face-de/face_trainer/trainer.yml')cascadePath = "/home/xgj/Desktop/face-de/haarcascade_frontalface_default.xml"faceCascade = cv2.CascadeClassifier(cascadePath)font = cv2.FONT_HERSHEY_SIMPLEX#这里为0或者1都没有关系idnum = 1names = ['Allen', 'Bob'] #names中存储人的名字,若该人id为0则他的名字在第一位,id位1则排在第二位,以此类推cam = cv2.VideoCapture(0)minW = 0.1*cam.get(3)minH = 0.1*cam.get(4)while True: ret, img = cam.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=5, minSize=(int(minW), int(minH)) ) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) idnum, confidence = recognizer.predict(gray[y:y+h, x:x+w]) if confidence < 100: idnum = names[idnum] confidence = "{0}%".format(round(100 - confidence)) else: idnum = "unknown" confidence = "{0}%".format(round(100 - confidence)) cv2.putText(img, str(idnum), (x+5, y-5), font, 1, (0, 0, 255), 1) #不能显示中文 cv2.putText(img, str(confidence), (x+5, y+h-5), font, 1, (0, 0, 0), 1) cv2.imshow('camera', img) k = cv2.waitKey(10) if k == 27: breakcam.release()cv2.destroyAllWindows()
4.4.2 PIL法显示中文的人脸识别5-face_recognition_zh_PIL.py代码:自己添加的
#face_recognition 人脸检测,PIL法显示中文人名import cv2#---增加的PIL法显示中文---import numpyfrom PIL import Image, ImageDraw, ImageFont#定义一个函数def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20): if (isinstance(img, numpy.ndarray)): # 判断是否OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 创建一个可以在给定图像上绘图的对象 draw = ImageDraw.Draw(img) # 字体的格式,自己下载华文仿宋字体,放在根目录下 fontStyle = ImageFont.truetype( "hwfs.ttf", textSize, encoding="utf-8") # 绘制文本 draw.text((left, top), text, textColor, font=fontStyle) # 转换回OpenCV格式 return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)recognizer = cv2.face.LBPHFaceRecognizer_create()recognizer.read('/home/xgj/Desktop/face-de/face_trainer/trainer.yml')cascadePath = "/home/xgj/Desktop/face-de/haarcascade_frontalface_default.xml"faceCascade = cv2.CascadeClassifier(cascadePath)font = cv2.FONT_HERSHEY_SIMPLEX#这里为0或者1都没有关系idnum = 0names = ['机器人', 'Bob'] #names中存储人的名字,若该人id为0则他的名字在第一位,id位1则排在第二位,以此类推cam = cv2.VideoCapture(0)minW = 0.1*cam.get(3)minH = 0.1*cam.get(4)while True: ret, img = cam.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=5, minSize=(int(minW), int(minH)) ) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) idnum, confidence = recognizer.predict(gray[y:y+h, x:x+w]) if confidence < 100: idnum = names[idnum] confidence = "{0}%".format(round(100 - confidence)) else: idnum = "unknown" confidence = "{0}%".format(round(100 - confidence)) #cv2.putText(img, str(idnum), (x+5, y-5), font, 1, (0, 0, 255), 1) #不能显示中文 #注意下面格式,位置去掉元组格式,并int化 img = cv2ImgAddText(img, str(idnum), int(x+5), int(y-5), (0, 0, 255),20) #显示为中文PIL法 cv2.putText(img, str(confidence), (x+5, y+h-5), font, 1, (0, 0, 0), 1) cv2.imshow('camera', img) k = cv2.waitKey(10) if k == 27: breakcam.release()cv2.destroyAllWindows()
效果图