EduCoder平台:人脸识别系统——Dlib人脸识别
第1关:dlib人脸检测的基本原理
编程要求:
请在右侧编辑器中的BEGIN-END之间编写代码,使用Dlib识别人脸并输出识别结果:
计算已知图片所有人脸特征向量; 计算待识别图片与已知图片特征向量间的欧氏距离; 打印识别结果。
代码如下:
import osimport dlibimport globimport numpy as npimport cv2predictor_path = 'step1/model/shape_predictor_5_face_landmarks.dat'face_rec_model_path = 'step1/model/dlib_face_recognition_resnet_model_v1.dat'known_image_path = 'step1/image/known_image'test_image_path = "step1/image/test_image"detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor(predictor_path)'''****************BEGIN****************'''# 加载人脸识别模型facerec =dlib.face_recognition_model_v1(face_rec_model_path)'''**************** END ****************'''descriptors = []names = ["TongDaWei","XiaYu","ZhangYiShan"]# 计算已知图片的特征向量for f in glob.glob(os.path.join(known_image_path, "*.jpg")):img = dlib.load_rgb_image(f)# 1.人脸检测dets = detector(img, 1)for k, d in enumerate(dets):'''****************BEGIN****************'''# 2.关键点检测shape = predictor(img, d)# 3.特征向量face_descriptor =pute_face_descriptor(img, shape)# 转换为numpy arrayv =np.array(face_descriptor)descriptors.append(v)'''**************** END ****************'''for f in glob.glob(os.path.join(test_image_path, "*.jpg")):img = dlib.load_rgb_image(f)dets = detector(img, 1)for k, d in enumerate(dets):'''****************BEGIN****************'''shape = predictor(img, d)face_descriptor = pute_face_descriptor(img, shape)# 当前待识别的图片特征向量为currentcurrent =np.array(face_descriptor)'''**************** END ****************'''# 计算欧式距离,识别人脸tolerance = 0.4current_name = "Unknow"'''****************BEGIN****************'''# 输出识别的结果for i in range(len(descriptors)):distance = np.linalg.norm(descriptors[i]-current)if distance<tolerance:#names用于存放于已知人脸特征向量对应的名字current_name = names[i]breakprint("当前图片识别结果为:"+current_name)'''**************** END ****************'''
第2关:绘制人脸识别结果
编程要求:
请在右侧编辑器中的BEGIN-END之间编写代码,使用OpenCV绘制人脸识别结果,并保存图片到指定路径:
绘制人脸区域,边框颜色为(0,0,255),边框粗度为2;将文字内容放在(d.left(),d.bottom+13)处;文字内容为识别的结果;字体为cv2.FONT_HERSHEY_PLAIN;字体颜色为(255,0,0);字体倍数为0.5;字体厚度为1。
代码如下:
import osimport dlibimport globimport numpy as npimport cv2predictor_path = 'step1/model/shape_predictor_5_face_landmarks.dat'face_rec_model_path = 'step1/model/dlib_face_recognition_resnet_model_v1.dat'known_image_path = 'step1/image/known_image'test_image_path = "step1/image/test_image"detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor(predictor_path)facerec = dlib.face_recognition_model_v1(face_rec_model_path)descriptors = []names = ["TongDaWei","XiaYu","ZhangYiShan"]for f in sorted(glob.glob(os.path.join(known_image_path, "*.jpg"))):img = dlib.load_rgb_image(f)# 1.人脸检测dets = detector(img, 1)for k, d in enumerate(dets):# 2.关键点检测shape = predictor(img, d)# 3.描述子提取,128D向量face_descriptor = pute_face_descriptor(img, shape)# 转换为numpy arrayv = np.array(face_descriptor)descriptors.append(v)count = 0for f in sorted(glob.glob(os.path.join(test_image_path, "*.jpg"))):img = dlib.load_rgb_image(f)dets = detector(img, 1)for k, d in enumerate(dets):shape = predictor(img, d)face_descriptor = pute_face_descriptor(img, shape)# 当前待识别的图片特征向量为currentcurrent = np.array(face_descriptor)# 计算欧式距离,识别人脸tolerance = 0.4current_name = "Unknow"for i in range(len(descriptors)):distance = np.linalg.norm(descriptors[i]-current)if distance<tolerance:#names用于存放于已知人脸特征向量对应的名字current_name = names[i]break'''****************BEGIN****************'''# 绘制人脸区域cv2.rectangle(img,(d.left(), d.top()),(d.right(), d.bottom()),(0,0,255),2)# 在图片上添加文字font = cv2.FONT_HERSHEY_PLAINcv2.putText(img, current_name, (d.left(), d.bottom()+13), font, 0.5, (255,0,0), 1)'''**************** END ****************'''# 保存图片count = count+1cv2.imwrite("step2/image/out/"+str(count)+".jpg",cv2.cvtColor(img,cv2.COLOR_RGB2BGR))