#1,导入库 import cv2 import numpy as np import face_recognition #2,加载图片 liu=cv2.imread('liu.jpg') wang=cv2.imread('wang.jpg')#3,BGR转为RGB liu_RGB=liu[:,:,::-1] wang_RGB=wang[:,:,::-1]#4,检测人脸 liu_face=face_recognition.face_locations(liu_RGB) wang_face=face_recognition.face_locations(wang_RGB)#5,人脸特征编码 liu_encoding=face_recognition.face_encodings(liu_RGB,liu_face)[0] wang_encoding=face_recognition.face_encodings(wang_RGB,wang_face)[0]#6,把所有脸放在一起,当作数据库 encodings=[liu_encoding,wang_encoding] names=['liu','wang']#7,打开摄像头,读取视频流 cap=cv2.VideoCapture(0) if not cap.isOpened():raise IOError("Camera Error!") while True:ret,frame=cap.read()frame=cv2.resize(frame,(0,0),fx=0.5,fy=0.5)#8,BGR转为RGBframe_RGB=frame[:,:,::-1]#9,人脸检测face_locations=face_recognition.face_locations(frame_RGB)#10,人脸特征编码face_encodings=face_recognition.face_encodings(frame_RGB,face_locations)#11,与数据库中的人脸进行匹配for (top,right,bottom,left),face_encoding in zip(face_locations,face_encodings):#12,进行匹配matches=face_recognition.compare_faces(encodings,face_encoding)#13,计算距离distances=face_recognition.face_distance(encodings,face_encoding)min_distance_index=np.argmin(distances)#14,判断:如果匹配,获取名字name="unknown"if matches[min_distance_index]:name=names[min_distance_index]#15,绘制人脸矩形框cv2.rectangle(frame,(left,top),(right,bottom),(0,255,0),3) #16,绘制,显示对应人脸的名字cv2.rectangle(frame,(left,bottom-30),(right,bottom),(0,0,255),3)#17,显示名字cv2.putText(frame,name,(left+10,bottom-10),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),1)#18,显示整个效果cv2.imshow('face recognition',frame)#19,判断Q,退出if cv2.waitKey(1)&0xFF==ord('q'):break #20,关闭资源 cap.release() cv2.destroyAllWindows()
import face_recognition import os import cv2 from PIL import Image,ImageDraw,ImageFont import numpy as np#解析已有人员的所有照片名和人物面部编码信息 def load_img(path):print('正在加载已知人员的图片。。。。')for dirpath,dirnames,filenames in os.walk(path):print(dirpath,dirnames,filenames)facelib=[]for filename in filenames:filepath=os.sep.join([dirpath,filename])#把对应每张图像加载进来face_image=face_recognition.load_image_file(filepath)face_encoding=face_recognition.face_encodings(face_image)[0]facelib.append(face_encoding)return facelib,filenames facelib,facenames=load_img(r'D:\temp\faces')#调用摄像头 video_capture=cv2.VideoCapture(0) while True:ret,frame=video_capture.read()#通过缩小图片(缩小为1/4),提高对比效率)if frame is not None:small_frame=cv2.resize(frame,(0,0),fx=0.25,fy=0.25)rgb_small_frame=small_frame[:,:,::-1] #将opencv的BGR格式转化为RGB格式face_locations=face_recognition.face_locations(rgb_small_frame)face_encodings=face_recognition.face_encodings(rgb_small_frame,face_locations)face_names=[]#循环多张脸for face_encoding in face_encodings:matches=face_recognition.compare_faces(facelib,face_encoding,tolerance=0.39)name='未知头像'if True in matches:#如果摄像头里面的头像匹配了已知人物头像,则取出第一个True的位置first_match_index=matches.index(True)name=facenames[first_match_index][:-4] #取出文件上对应的人名face_names.append(name)for (top,right,bottom,left),name in zip(face_locations,face_names):#还原图片大小top *=4right *=4bottom *=4left *=4cv2.rectangle(frame,(left,top),(right,bottom),(0,0,225),2) #标注人脸信息img_PIL=Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))font=ImageFont.truetype('simhei.ttf',40)draw=ImageDraw.Draw(img_PIL)draw.text((left+6,bottom-6),name,font=font,fill=(255,255,255))frame=cv2.cvtColor(np.asarray(img_PIL),cv2.COLOR_RGB2BGR)cv2.imshow('Video',frame)if cv2.waitKey(1)&0xFF==ord('q'):breakvideo_capture.release()