最前面的话
感谢弦弦子的一位粉丝
注意:本博客仅供参考!
第一关:dlib人脸检测的基本原理
任务描述
本关任务:
1.理解如何计算特征向量;
2.理解如何计算向量与向量间的欧氏距离;
3.理解如何识别人脸。
编程要求
请在右侧编辑器中的BEGIN-END之间编写代码,使用Dlib识别人脸并输出识别结果:
·计算已知图片所有人脸特征向量;
·计算待识别图片与已知图片特征向量间的欧氏距离;
·打印识别结果。
测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
代码
import os
import dlib
import glob
import numpy as np
import cv2
predictor_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)
dets = detector(img, 1)
for k, d in enumerate(dets):
'''****************BEGIN****************'''
shape = predictor(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
v = 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 = facerec.compute_face_descriptor(img, shape)
current = np.array(face_descriptor)
'''**************** END ****************'''
tolerance = 0.4
current_name = "Unknow"
'''****************BEGIN****************'''
for i in range(len(descriptors)):
distance = np.linalg.norm(descriptors[i]-current)
if distance<tolerance:
current_name = names[i]
break
print("当前图片识别结果为:"+current_name)
'''**************** END ****************'''
第二关:绘制人脸识别结果
任务描述
本关任务:绘制人脸识别结果。
编程要求
请在右侧编辑器中的BEGIN-END之间编写代码,使用OpenCV绘制人脸识别结果,并保存图片到指定路径:
·绘制人脸区域,边框颜色为(0,0,255),边框粗度为2; ·将文字内容放在(d.left(),d.bottom+13)处; ·文字内容为识别的结果; ·字体为cv2.FONT_HERSHEY_PLAIN; ·字体颜色为(255,0,0); ·字体倍数为0.5; ·字体厚度为1。
测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
代码
import os
import dlib
import glob
import numpy as np
import cv2
predictor_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)
dets = detector(img, 1)
for k, d in enumerate(dets):
shape = predictor(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
v = np.array(face_descriptor)
descriptors.append(v)
count = 0
for 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 = facerec.compute_face_descriptor(img, shape)
current = np.array(face_descriptor)
tolerance = 0.4
current_name = "Unknow"
for i in range(len(descriptors)):
distance = np.linalg.norm(descriptors[i]-current)
if distance<tolerance:
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_DUPLEX
cv2.putText(img, current_name, (d.left(), d.bottom() + 13), font, 0.5, (255, 0, 0), 1)
'''**************** END ****************'''
count = count+1
cv2.imwrite("step2/image/out/"+str(count)+".jpg",cv2.cvtColor(img,cv2.COLOR_RGB2BGR))
|