目录
- 前言
- 一、系统总流程设计
- 二、环境安装
- 1. 创建虚拟环境
- 2.安装其他库
- 三、模型搭建
- 1.采集数据集
- 2. 数据预处理
- 3.构建模型和训练
- 五、摄像头测试
- 六、web界面搭建与pyqt界面搭建
- 报错了并解决的方法
- 总结
前言
随着人工智能的不断发展,机器学习和深度学习这门技术也越来越重要,一时间成为码农的学习热点。下面将使用深度学习技术开发一个人脸识别系统。之前使用 Tensorflow1.5 完成人脸识别(之前版本的链接: 手把手教你完成深度学习人脸识别系统),现在更新到 Tensorflow2.7 版本,我已经改写完成了,更新内容如下:
- 加入 Flask 框架完成一个简单的 web 版人脸识别
- Tensorflow1.5 改成Tensorflow2.7
- 数据预处理代码更加自动
下面直接展示结果吧:
一、系统总流程设计
二、环境安装
手把手教学视频:
链接: link
建议所有库的版本跟我一样,以免出错
python=3.8
tensorflow==2.7(这个版本一定要跟我一样的)
1. 创建虚拟环境
conda create -n py38 python=3.8
激活环境
activate py38
2.安装其他库
单独安装 pyqt5,命令如下
pip install pyqt5
单独安装 tensorflow,如果安装 gpu 版本,电脑必须有英伟达显卡,并且先安装对应版本的 cuda 和 cudnn,安装教程看这篇文章: cuda和cudnn的安装教程(全网最详细保姆级教程)
安装完成之后,输入如下命令来安装 tensorflow:
pip install tensorflow_gpu==2.7.0
安装 cpu 版本就简单了,不用安装cuda和cudnn,直接输入下面命令安装就行,命令如下:
pip install tensorflow-cpu==2.7.0
之后安装 requirements.txt 配置文件,命令如下:
pip install -r requirements.txt
安装完你已经成功一大把了,看到这里点个赞赞鼓励一下
三、模型搭建
1.采集数据集
使用摄像头进行采集
代码可以直接运行,getdata.py代码如下:
注意:25行 cap = cv2.VideoCapture(1)的改为 cap = cv2.VideoCapture(0),0代表本电脑自带摄像头,1代码其他外接摄像头:
# encoding:utf-8
'''
功能: Python opencv调用摄像头获取个人图片
使用方法:
启动摄像头后需要借助键盘输入操作来完成图片的获取工作
c(change): 生成存储目录
p(photo): 执行截图
q(quit): 退出拍摄
'''
import os
import cv2
def cameraAutoForPictures(saveDir='data/'):
'''
调用电脑摄像头来自动获取图片
'''
if not os.path.exists(saveDir):
os.makedirs(saveDir)
count = 1
cap = cv2.VideoCapture(1)
width, height, w = 640, 480, 360
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
crop_w_start = (width - w) // 2
crop_h_start = (height - w) // 2
print('width: ', width)
print('height: ', height)
while True:
ret, frame = cap.read()
frame = frame[crop_h_start:crop_h_start + w, crop_w_start:crop_w_start + w]
frame = cv2.flip(frame, 1, dst=None)
cv2.imshow("capture", frame)
action = cv2.waitKey(1) & 0xFF
if action == ord('c'):
saveDir = input(u"请输入新的存储目录:")
if not os.path.exists(saveDir):
os.makedirs(saveDir)
elif action == ord('p'):
cv2.imwrite("%s/%d.jpg" % (saveDir, count), cv2.resize(frame, (224, 224), interpolation=cv2.INTER_AREA))
print(u"%s: %d 张图片" % (saveDir, count))
count += 1
if action == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
# xxx替换为自己的名字
cameraAutoForPictures(saveDir=u'data/1/')
2. 数据预处理
代码可以直接运行,new_data_preparation.py代码如下:
# -*- coding: utf-8 -*-
"""
@Auth : 挂科边缘
@File :new_data_preparation.py
@IDE :PyCharm
@Motto:学习新思想,争做新青年
@Email :179958974@qq.com
"""
'''
功能: 图像的数据预处理、标准化部分
'''
import os
import cv2
import time
def readAllImg(path, *suffix):
'''
基于后缀读取文件
'''
resultArray = []
try:
for root, dirs, files in os.walk(path):
for file in files:
if endwith(file, suffix):
document = os.path.join(root, file)
img = cv2.imread(document)
resultArray.append((document, img))
except IOError:
print("Error")
else:
print("读取成功")
return resultArray
def endwith(s, *endstring):
'''
对字符串的后缀进行匹配
'''
return any(map(s.endswith, endstring))
def readPicSaveFace(sourcePath, objectPath, *suffix):
'''
图片标准化与存储
'''
if not os.path.exists(objectPath):
os.makedirs(objectPath)
try:
allImages = readAllImg(sourcePath, *suffix)
face_cascade = cv2.CascadeClassifier('config/haarcascade_frontalface_alt.xml')
count = 0
for document, img in allImages:
if img is not None:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
face = cv2.resize(gray[y:y + h, x:x + w], (200, 200))
# 创建与sourcePath子目录对应的objectPath子目录
relativePath = os.path.relpath(document, sourcePath)
subdir = os.path.dirname(relativePath)
saveDir = os.path.join(objectPath, subdir)
if not os.path.exists(saveDir):
os.makedirs(saveDir)
timestamp = str(int(time.time()))
fileName = f'{timestamp}_{count}.jpg'
cv2.imwrite(os.path.join(saveDir, fileName), face)
count += 1
except Exception as e:
print("Exception:", e)
else:
print(f'已处理 {count} 张人脸,保存到 {objectPath}')
if __name__ == '__main__':
print('数据处理开始!!!')
readPicSaveFace('data', 'dataset', '.jpg', '.JPG', '.png', '.PNG', '.tiff', '.TIFF')
3.构建模型和训练
代码可以直接运行,train_model.py代码如下:
keras搭建cnn网络模型提取人脸特征
# -*- coding: utf-8 -*-
"""
@Auth : 挂科边缘
@File :train_model.py
@IDE :PyCharm
@Motto:学习新思想,争做新青年
@Email :179958974@qq.com
"""
'''
功能: 构建人脸识别模型
'''
import os
import cv2
import random
import numpy as np
from tensorflow.keras.models import Sequential, load_model
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.utils import to_categorical
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class DataSet(object):
'''
用于存储和格式化读取训练数据的类
'''
def __init__(self, path):
'''
初始化
'''
self.num_classes = None
self.X_train = None
self.X_test = None
self.Y_train = None
self.Y_test = None
self.img_size = 128
self.extract_data(path)
def extract_data(self, path):
'''
抽取数据
'''
imgs, labels, counter = read_file(path)
X_train, X_test, y_train, y_test = train_test_split(imgs, labels, test_size=0.2, random_state=random.randint(0, 100))
X_train = X_train.reshape(X_train.shape[0], self.img_size, self.img_size, 1) / 255.0
X_test = X_test.reshape(X_test.shape[0], self.img_size, self.img_size, 1) / 255.0
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
Y_train = to_categorical(y_train, num_classes=counter)
Y_test = to_categorical(y_test, num_classes=counter)
self.X_train = X_train
self.X_test = X_test
self.Y_train = Y_train
self.Y_test = Y_test
self.num_classes = counter
def check(self):
'''
校验
'''
print('num of dim:', self.X_test.ndim)
print('shape:', self.X_test.shape)
print('size:', self.X_test.size)
print('num of dim:', self.X_train.ndim)
print('shape:', self.X_train.shape)
print('size:', self.X_train.size)
print(np.isnan(dataset.X_train).sum())
print(np.isnan(dataset.X_test).sum())
def endwith(s, *endstring):
'''
对字符串的后续和标签进行匹配
'''
resultArray = map(s.endswith, endstring)
if True in resultArray:
return True
else:
return False
def read_file(path):
'''
图片读取
'''
img_list = []
label_list = []
dir_counter = 0
IMG_SIZE = 128
for child_dir in os.listdir(path):
child_path = os.path.join(path, child_dir)
for dir_image in os.listdir(child_path):
if endwith(dir_image, 'jpg'):
img = cv2.imread(os.path.join(child_path, dir_image))
resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY)
img_list.append(recolored_img)
label_list.append(dir_counter)
dir_counter += 1
img_list = np.array(img_list)
return img_list, label_list, dir_counter
def read_name_list(path):
'''
读取训练数据集
'''
name_list = []
for child_dir in os.listdir(path):
name_list.append(child_dir)
return name_list
class Model(object):
'''
人脸识别模型
'''
FILE_PATH = "./models/face.h5"
IMAGE_SIZE = 128
def __init__(self):
self.model = None
def read_trainData(self, dataset):
self.dataset = dataset
def build_model(self):
self.model = Sequential()
self.model.add(
Conv2D(
filters=32,
kernel_size=(5, 5),
padding='same',
input_shape=self.dataset.X_train.shape[1:]
)
)
self.model.add(Activation('relu'))
self.model.add(
MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2),
padding='same'
)
)
self.model.add(Conv2D(filters=64, kernel_size=(5, 5), padding='same'))
self.model.add(Activation('relu'))
self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
self.model.add(Flatten())
self.model.add(Dense(1024))
self.model.add(Activation('relu'))
self.model.add(Dense(self.dataset.num_classes))
self.model.add(Activation('softmax'))
self.model.summary()
def train_model(self,epochs,batch_size):
self.model.compile(
optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
self.model.fit(self.dataset.X_train, self.dataset.Y_train, epochs=epochs, batch_size=batch_size)
def evaluate_model(self):
print('\nTesting---------------')
loss, accuracy = self.model.evaluate(self.dataset.X_test, self.dataset.Y_test)
print('test loss:', loss)
print('test accuracy:', accuracy)
def save(self, file_path=FILE_PATH):
print('Model Saved Finished!!!')
self.model.save(file_path)
def load(self, file_path=FILE_PATH):
print('Model Loaded Successful!!!')
self.model = load_model(file_path)
def predict(self, img):
img = img.reshape((1, self.IMAGE_SIZE, self.IMAGE_SIZE, 1))
img = img.astype('float32')
img = img / 255.0
result = self.model.predict(img)
max_index = np.argmax(result)
return max_index, result[0][max_index]
if __name__ == '__main__':
dataset = DataSet('dataset/')
model = Model()
model.read_trainData(dataset)
model.build_model()
model.train_model(epochs=10,batch_size=32)
model.evaluate_model()
model.save()
五、摄像头测试
代码可以直接运行,Demo.py代码如下:
new_names 对应文件夹人脸的顺序
#encoding:utf-8
from __future__ import division
import numpy
'''
功能: 人脸识别摄像头视频流数据实时检测模块
'''
from PIL import Image, ImageDraw, ImageFont
import os
import cv2
from train_model import Model
threshold=0.7 # 如果模型认为概率高于70%则显示为模型中已有的人物
# 新的名字列表
new_names = ["张三", "李四"]
# 解决cv2.putText绘制中文乱码
def cv2ImgAddText(img2, text, left, top, textColor=(0, 0, 255), textSize=20):
if isinstance(img2, numpy.ndarray): # 判断是否OpenCV图片类型
img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img2)
# 字体的格式
fontStyle = ImageFont.truetype(r"C:\WINDOWS\FONTS\MSYH.TTC", textSize, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(numpy.asarray(img2), cv2.COLOR_RGB2BGR)
class Camera_reader(object):
def __init__(self):
self.model=Model()
self.model.load()
self.img_size=128
def build_camera(self):
'''
调用摄像头来实时人脸识别
'''
face_cascade = cv2.CascadeClassifier('config/haarcascade_frontalface_alt.xml')
cameraCapture=cv2.VideoCapture(0)
success, frame=cameraCapture.read()
while success and cv2.waitKey(1)==-1:
success,frame=cameraCapture.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
ROI=gray[x:x+w,y:y+h]
ROI=cv2.resize(ROI, (self.img_size, self.img_size),interpolation=cv2.INTER_LINEAR)
label,prob=self.model.predict(ROI)
print(label)
if prob > threshold:
show_name = new_names[label]
else:
show_name = "陌生人"
# cv2.putText(frame, show_name, (x,y-20),cv2.FONT_HERSHEY_SIMPLEX,1,255,2)
# 在图像上绘制中文字符
# 解决cv2.putText绘制中文乱码
frame = cv2ImgAddText(frame, show_name, x + 5, y - 30,)
frame=cv2.rectangle(frame,(x,y), (x+w,y+h),(255,0,0),2)
cv2.imshow("Camera", frame)
else:
cameraCapture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
camera=Camera_reader()
camera.build_camera()
六、web界面搭建与pyqt界面搭建
web 界面采用 Flask 框架,主要实现图片识别功能,运行MainWeb.py即可在浏览器访问了,地址是:http://127.0.0.1:5000/upload
MainWeb.py代码如下:
# -*- coding: utf-8 -*-
"""
@Auth : 挂科边缘
@File :Test.py
@IDE :PyCharm
@Motto:学习新思想,争做新青年
@Email :179958974@qq.com
@qq :179958974
"""
import os
import time
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from flask import Flask, request, redirect, url_for, render_template
from flask_uploads import UploadSet, IMAGES, configure_uploads
from train_model import Model
app = Flask(__name__)
# 配置 Flask 文件上传
# 注意这里的配置名称与上传集 'photos' 的名称一致
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads')
app.config['UPLOADED_PHOTOS_ALLOW'] = IMAGES
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
# 人脸识别的标签(名字列表)
new_names = ["张国荣", "王祖贤", "彭于晏", "特狼普", "章子怡"]
# 加载人脸检测模型
face_cascade = cv2.CascadeClassifier('config/haarcascade_frontalface_alt.xml')
# 解决cv2.putText绘制中文乱码的问题
def cv2ImgAddText(img, text, left, top, textColor=(0, 0, 255), textSize=20):
if isinstance(img, np.ndarray):
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontStyle = ImageFont.truetype(r"C:\WINDOWS\FONTS\MSYH.TTC", textSize, encoding="utf-8")
draw.text((left, top), text, textColor, font=fontStyle)
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
def detectOnePicture(path):
'''
单图识别
'''
model = Model()
model.load()
# 读取图像并转换为灰度图
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.15, # 调整比例因子
minNeighbors=5, # 保持默认值
#minSize=(100, 100) # 设置较大的最小检测尺寸
)
if len(faces) == 0:
return "抱歉,未检测到人脸!"
for (x, y, w, h) in faces:
roi = gray[y:y + h, x:x + w]
roi = cv2.resize(roi, (128, 128), interpolation=cv2.INTER_LINEAR)
label, prob = model.predict(roi)
if prob > 0.5:
show_name = f"{new_names[label]} ({prob:.2f})"
res = f"识别为: {new_names[label]} 的概率为: {prob:.2f}"
else:
res = "抱歉,未识别出该人!请尝试增加数据量来训练模型!"
img = cv2ImgAddText(img, show_name, x + 5, y - 30)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imwrite(path, img)
print(res)
return res
@app.route('/upload', methods=['POST', 'GET'])
def upload():
if request.method == 'POST' and 'photo' in request.files:
filename = photos.save(request.files['photo'])
return redirect(url_for('show', name=filename))
return render_template('upload.html')
@app.route('/photo/<name>')
def show(name):
if not name:
print('出错了!')
return redirect(url_for('upload'))
file_path = os.path.join(app.config['UPLOADED_PHOTOS_DEST'], name)
if not os.path.exists(file_path):
return f"文件 {name} 不存在", 404
start_time = time.time()
res = detectOnePicture(file_path)
end_time = time.time()
execute_time = round(end_time - start_time, 2)
tsg = f'总耗时为: {execute_time} 秒'
url = photos.url(name)
return render_template('show.html', url=url, name=name, xinxi=res, shijian=tsg)
if __name__ == "__main__":
if not os.path.exists(app.config['UPLOADED_PHOTOS_DEST']):
os.makedirs(app.config['UPLOADED_PHOTOS_DEST'])
print('Face Recognition Demo')
app.run(debug=True)
pyqt5 搭建可视化界面,实现图片识别和摄像头识别
完整代码如下
注意:126行 cap = cv2.VideoCapture(1)的改为 cap = cv2.VideoCapture(0),0代表本电脑自带摄像头,1代码其他外接摄像头:
import sys
import cv2
import numpy
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
from PIL import Image, ImageDraw, ImageFont
from Demo import Camera_reader
from train_model import Model
# 解决cv2.putText绘制中文乱码
def cv2ImgAddText(img2, text, left, top, textColor=(0, 0, 255), textSize=20):
if isinstance(img2, numpy.ndarray):
img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img2)
fontStyle = ImageFont.truetype(r"C:\WINDOWS\FONTS\MSYH.TTC", textSize, encoding="utf-8")
draw.text((left, top), text, textColor, font=fontStyle)
return cv2.cvtColor(numpy.asarray(img2), cv2.COLOR_RGB2BGR)
# 新的名字列表
new_names = ["张国荣", "王祖贤"]
class FaceDetectionApp(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("人脸检测应用")
self.setGeometry(100, 100, 800, 600)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout()
self.upload_button = QPushButton("图片识别")
self.upload_button.clicked.connect(self.upload_image)
self.upload_button.setFixedSize(779, 50)
self.camera_button = QPushButton("摄像头识别")
self.camera_button.clicked.connect(self.start_camera_detection)
self.camera_button.setFixedSize(779, 50)
self.image_label = QLabel()
self.image_label.setAlignment(Qt.AlignCenter)
self.image_label.setFixedSize(779, 500)
self.result_label = QLabel("识别结果: ")
self.result_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.upload_button)
self.layout.addWidget(self.camera_button)
self.layout.addWidget(self.image_label)
self.layout.addWidget(self.result_label)
self.central_widget.setLayout(self.layout)
self.model = Model()
self.model.load()
def upload_image(self):
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.jpg *.jpeg *.bmp *.gif *.tiff)", options=options)
if file_name:
image = cv2.imread(file_name)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('config/haarcascade_frontalface_alt.xml')
faces = face_cascade.detectMultiScale(gray, 1.35, 5)
if len(faces) > 0:
for (x, y, w, h) in faces:
roi = gray[y:y + h, x:x + w]
roi = cv2.resize(roi, (128, 128), interpolation=cv2.INTER_LINEAR)
label, prob = self.model.predict(roi)
if prob > 0.7:
show_name = new_names[label]
res = f"识别为: {show_name}, 概率: {prob:.2f}"
else:
show_name = "陌生人"
res = "抱歉,未识别出该人!请尝试增加数据量来训练模型!"
frame = cv2ImgAddText(image, show_name, x + 5, y - 30)
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imwrite('prediction.jpg', frame)
self.result = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
self.QtImg = QImage(
self.result.data, self.result.shape[1], self.result.shape[0], QImage.Format_RGB32)
self.image_label.setPixmap(QPixmap.fromImage(self.QtImg))
self.image_label.setScaledContents(True) # 自适应界面大小
self.result_label.setText(res)
else:
self.result_label.setText("未检测到人脸")
def start_camera_detection(self):
self.camera = Camera_reader()
self.camera.build_camera()
class Camera_reader(object):
def __init__(self):
self.model = Model()
self.model.load()
self.img_size = 128
def build_camera(self):
face_cascade = cv2.CascadeClassifier('config/haarcascade_frontalface_alt.xml')
cameraCapture = cv2.VideoCapture(1)
success, frame = cameraCapture.read()
while success and cv2.waitKey(1) == -1:
success, frame = cameraCapture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
ROI = gray[x:x + w, y:y + h]
ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR)
label, prob = self.model.predict(ROI)
if prob > 0.7:
show_name = new_names[label]
else:
show_name = "陌生人"
frame = cv2ImgAddText(frame, show_name, x + 5, y - 30)
frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow("Camera", frame)
else:
cameraCapture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = FaceDetectionApp()
window.show()
sys.exit(app.exec_())
报错了并解决的方法
报错:AttributeError: ‘str‘ object has no attribute ‘decode‘
降低h5py版本
解决方法:
pip install h5py==2.10.0
总结
完整源码+数据集+模型,地址: 源码下载
提取码: m9tk
本文通过opencv+cnn网络模型结合实现人脸识别,opencv实现人脸识别,cnn实现人脸的特征提取,并识别是某个人,cnn模型有待优化,你们可以自己需求更换其它的深度学习模型,增加训练数据集样本,实现更精准的人脸识别模型,有问题评论区留言,谢谢观看
博主熬夜写博客写代码,已经掉一大把头发了,麻烦点个赞赞鼓励一下