代码参考:https://download.csdn.net/download/weixin_55771290/87430422
前言
windows hello 的低阶板本,没有 Windows hello 的 3D 景深镜头,因此这是一个基于图片的识别机主的程序。 具体运行时,解锁时,判断是否是本人;若不是本人或无人(10s),锁屏;若是本人,正常使用;(采取无密码原始界面)
人脸的检测采取 opencv cv2.CascadeClassifier
关于模型则采取
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 62, 62, 128) 3584 _________________________________________________________________ conv2d_1 (Conv2D) (None, 60, 60, 64) 73792 _________________________________________________________________ flatten (Flatten) (None, 230400) 0 _________________________________________________________________ dense (Dense) (None, 40) 9216040 ================================================================= Total params: 9,293,416 Trainable params: 9,293,416 Non-trainable params: 0 _________________________________________________________________ None
基础需要由四部分组成。
运行 python 环境
主要是在 tensorflow2.0-gpu 下运行; 这里略微吐槽下 tensorflow2.0.keras 模块部分无提示,对于新手不太友好。 conda list:
制作自己训练数据:
人脸数据存储至 my_faces 可自己命名
face_1.py
# 制作自己人脸数据fromcv2importcv2importosimportsysimportrandomout_dir='./my_faces'ifnotos.path.exists(out_dir):os.makedirs(out_dir)# 改变亮度与对比度defrelight(img,alpha=1,bias=0):w=img.shape[1]h=img.shape[0]#image = []foriinrange(0,w):forjinrange(0,h):forcinrange(3):tmp=int(img[j,i,c]*alpha+bias)iftmp>255:tmp=255eliftmp<0:tmp=0img[j,i,c]=tmpreturnimg# 获取分类器haar=cv2.CascadeClassifier(r'E:\ProgramData\Anaconda3\envs\tenserflow02\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')# 打开摄像头 参数为输入流,可以为摄像头或视频文件camera=cv2.VideoCapture(0)n=1while1:if(n<=5000):print('It`s processing %s image.'%n)# 读帧success,img=camera.read()gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)faces=haar.detectMultiScale(gray_img,1.3,5)forf_x,f_y,f_w,f_hinfaces:face=img[f_y:f_y+f_h,f_x:f_x+f_w]face=cv2.resize(face,(64,64))face=relight(face,random.uniform(0.5,1.5),random.randint(-50,50))cv2.imshow('img',face)cv2.imwrite(out_dir+'/'+str(n)+'.jpg',face)n+=1key=cv2.waitKey(30)&0xffifkey==27:breakelse:break
制作他人训练数据:
需要收集一个其他人脸的图片集,只要不是自己的人脸都可以,可以在网上找到,这里我给出一个我用到的图片集: 网站地址:http://vis-www.cs.umass.edu/lfw/ 图片集下载:http://vis-www.cs.umass.edu/lfw/lfw.tgz 先将下载的图片集,解压到项目目录下的 lfw 目录下,也可以自己指定目录(修改代码中的 input_dir 变量)
face_3.py
# -*- codeing: utf-8 -*-importsysimportosfromcv2importcv2input_dir='./lfw'output_dir='./other_faces'size=64ifnotos.path.exists(output_dir):os.makedirs(output_dir)defclose_cv2():"""删除cv窗口"""while(1):if(cv2.waitKey(100)==27):breakcv2.destroyAllWindows()# 获取分类器haar=cv2.CascadeClassifier(r'E:\ProgramData\Anaconda3\envs\tenserflow02\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')index=1for(path,dirnames,filenames)inos.walk(input_dir):forfilenameinfilenames:iffilename.endswith('.jpg'):print('Being processed picture %s'%index)img_path=path+'/'+filename# # 从文件读取图片print(img_path)img=cv2.imread(img_path)# cv2.imshow(" ",img)# close_cv2()# 转为灰度图片gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)faces=haar.detectMultiScale(gray_img,1.3,5)forf_x,f_y,f_w,f_hinfaces:face=img[f_y:f_y+f_h,f_x:f_x+f_w]face=cv2.resize(face,(64,64))# face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))cv2.imshow('img',face)cv2.imwrite(output_dir+'/'+str(index)+'.jpg',face)index+=1key=cv2.waitKey(30)&0xffifkey==27:sys.exit(0)
数据训练
读取上文的 my_faces 和 other_faces 文件夹下的训练数据进行训练
face_2.py
# -*- codeing: utf-8 -*-from__future__importabsolute_import,division,print_functionimporttensorflowastffromcv2importcv2importnumpyasnpimportosimportrandomimportsysfromsklearn.model_selectionimporttrain_test_splitfromsklearn.metricsimportclassification_report# from keras import backend as KdefgetPaddingSize(img):h,w,_=img.shapetop,bottom,left,right=(0,0,0,0)longest=max(h,w)ifw<longest:tmp=longest-w# //表示整除符号left=tmp//2right=tmp-leftelifh<longest:tmp=longest-htop=tmp//2bottom=tmp-topelse:passreturntop,bottom,left,rightdefreadData(path,h,w,imgs,labs):forfilenameinos.listdir(path):iffilename.endswith('.jpg'):filename=path+'/'+filenameimg=cv2.imread(filename)# cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)top,bottom,left,right=getPaddingSize(img)# 将图片放大, 扩充图片边缘部分img=cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,value=[0,0,0])img=cv2.resize(img,(h,w))imgs.append(img)labs.append(path)returnimgs,labsdefget_model():model=tf.keras.Sequential()# 第一层卷积,卷积的数量为128,卷积的高和宽是3x3,激活函数使用relumodel.add(tf.keras.layers.Conv2D(128,kernel_size=3,activation='relu',input_shape=(64,64,3)))# 第二层卷积model.add(tf.keras.layers.Conv2D(64,kernel_size=3,activation='relu'))#把多维数组压缩成一维,里面的操作可以简单理解为reshape,方便后面Dense使用model.add(tf.keras.layers.Flatten())#对应cnn的全链接层,可以简单理解为把上面的小图汇集起来,进行分类model.add(tf.keras.layers.Dense(40,activation='softmax'))model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])returnmodeldeffacemain():my_faces_path='./my_faces'other_faces_path='./other_faces'size=64imgs=[]labs=[]imgs,labs=readData(my_faces_path,size,size,imgs,labs)imgs,labs=readData(other_faces_path,size,size,imgs,labs)# 将图片数据与标签转换成数组imgs=np.array(imgs)# labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs])labs=np.array([[1]iflab==my_faces_pathelse[0]forlabinlabs])print(imgs.shape)print(labs.shape)# 随机划分测试集与训练集train_x,test_x,train_y,test_y=train_test_split(imgs,labs,test_size=0.8,random_state=random.randint(0,100))# 参数:图片数据的总数,图片的高、宽、通道train_x=train_x.reshape(train_x.shape[0],size,size,3)test_x=test_x.reshape(test_x.shape[0],size,size,3)# 将数据转换成小于1的数train_x=train_x.astype('float32')/255.0test_x=test_x.astype('float32')/255.0print('train size:%s, test size:%s'%(len(train_x),len(test_x)))# 图片块,每次取100张图片batch_size=100num_batch=len(train_x)//batch_sizemodel=get_model()model.fit(train_x,train_y,epochs=5)model.save(r'C:\Users\Administrator\Desktop\my_model.h5')facemain()
预测判断是否是本人,以进行是否锁屏操作
face_4.py
# 识别自己from__future__importabsolute_import,division,print_functionimporttensorflowastffromcv2importcv2importosimportsysimportrandomimportnumpyasnpfromsklearn.model_selectionimporttrain_test_splitfromsklearn.metricsimportclassification_reportfromsklearn.metricsimportcohen_kappa_scorefromctypesimport*importtimeimportsysdefgetPaddingSize(img):h,w,_=img.shapetop,bottom,left,right=(0,0,0,0)longest=max(h,w)ifw<longest:tmp=longest-w# //表示整除符号left=tmp//2right=tmp-leftelifh<longest:tmp=longest-htop=tmp//2bottom=tmp-topelse:passreturntop,bottom,left,rightdefreadData(path,h,w,imgs,labs):forfilenameinos.listdir(path):iffilename.endswith('.jpg'):filename=path+'/'+filenameimg=cv2.imread(filename)# cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)top,bottom,left,right=getPaddingSize(img)# 将图片放大, 扩充图片边缘部分img=cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,value=[0,0,0])img=cv2.resize(img,(h,w))imgs.append(img)labs.append(path)returnimgs,labs# 改变亮度与对比度defrelight(img,alpha=1,bias=0):w=img.shape[1]h=img.shape[0]#image = []foriinrange(0,w):forjinrange(0,h):forcinrange(3):tmp=int(img[j,i,c]*alpha+bias)iftmp>255:tmp=255eliftmp<0:tmp=0img[j,i,c]=tmpreturnimgout_dir='./temp_faces'ifnotos.path.exists(out_dir):os.makedirs(out_dir)# 获取分类器haar=cv2.CascadeClassifier(r'E:\ProgramData\Anaconda3\envs\tenserflow02\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')# 打开摄像头 参数为输入流,可以为摄像头或视频文件camera=cv2.VideoCapture(0)n=1start=time.clock()while1:if(n<=20):print('It`s processing %s image.'%n)# 读帧success,img=camera.read()gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)faces=haar.detectMultiScale(gray_img,1.3,5)forf_x,f_y,f_w,f_hinfaces:face=img[f_y:f_y+f_h,f_x:f_x+f_w]face=cv2.resize(face,(64,64))# face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))cv2.imshow('img',face)cv2.imwrite(out_dir+'/'+str(n)+'.jpg',face)n+=1key=cv2.waitKey(30)&0xffifkey==27:breakend=time.clock()print(str(end-start))if(end-start)>10:user32=windll.LoadLibrary('user32.dll')user32.LockWorkStation()sys.exit()else:breakmy_faces_path=out_dirsize=64imgs=[]labs=[]imgs,labs=readData(my_faces_path,size,size,imgs,labs)# 将图片数据与标签转换成数组imgs=np.array(imgs)# labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs])labs=np.array([[1]iflab==my_faces_pathelse[0]forlabinlabs])# 随机划分测试集与训练集train_x,test_x,train_y,test_y=train_test_split(imgs,labs,test_size=0.9,random_state=random.randint(0,100))# 参数:图片数据的总数,图片的高、宽、通道train_x=train_x.reshape(train_x.shape[0],size,size,3)test_x=test_x.reshape(test_x.shape[0],size,size,3)# 将数据转换成小于1的数train_x=train_x.astype('float32')/255.0test_x=test_x.astype('float32')/255.0restored_model=tf.keras.models.load_model(r'C:\Users\Administrator\Desktop\my_model.h5')pre_result=restored_model.predict_classes(test_x)print(pre_result.shape)print(pre_result)acc=sum(pre_result==1)/pre_result.shape[0]print("相似度: "+str(acc))ifacc>0.8:print("你是***")else:user32=windll.LoadLibrary('user32.dll')user32.LockWorkStation()
添加 face_4.py 解锁 windows 运行任务计划程序库
myface.bat 文件
激活 Anaconda 环境 切 CD 至 face_4.py 的位置
call activate tensorflow02 cd /d E:\ziliao\LearningPy\face python face_4.py
hide.vbs 文件以隐藏程序运行时的 cmd
Set ws = CreateObject("Wscript.Shell") ws.run "cmd /c E:\ziliao\LearningPy\face\myface.bat",vbhide
添加 hide.vbs 任务计划库中
创建任务