700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【Dlib】人脸检测 特征点检测 人脸对齐 人脸识别

【Dlib】人脸检测 特征点检测 人脸对齐 人脸识别

时间:2022-12-11 23:26:32

相关推荐

【Dlib】人脸检测 特征点检测 人脸对齐 人脸识别

本文是利用dlib库,进行人脸检测、特征点检测、人脸对齐。所有前提是假设已经安装了dlib。

参考链接:

1、/art/01/564529.htm

2、/ying86615791/article/details/71217273

3、/wc781708249/article/details/78562902

1、准备工作

1.1 安装dilb

下载安装包安装或者pip都可以。

首先Ubuntu下安装:

gpu版本我用的是python+Ubuntu+gpu,安装教程见:/qq_23225317/article/details/78815777

当然如果直接cpu版本,使用pip install dlib即可

windows下安装

windows下需要下载安装包,/project/dlib/18.17.100/#files,找到你需要的安装包,然后pip install xxxx.whl即可。

2.2 下载所需要的模型

/files/

任君挑选

2、人脸检测

上代码:

# encoding:utf-8import dlibimport numpy as npimport cv2def rect_to_bb(rect): # 获得人脸矩形的坐标信息x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)def resize(image, width=1200): # 将待检测的image进行resizer = width * 1.0 / image.shape[1]dim = (width, int(image.shape[0] * r))resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)return resizeddef detect():image_file = "test.jpg"detector = dlib.get_frontal_face_detector()image = cv2.imread(image_file)image = resize(image, width=1200)gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)rects = detector(gray, 1)for (i, rect) in enumerate(rects):(x, y, w, h) = rect_to_bb(rect)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(image, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_ HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)cv2.imshow("Output", image)cv2.waitKey(0)if __name__ == "__main__":detect()

结果截图:

3、人脸特征点检测

代码:

# encoding:utf-8import dlibimport numpy as npimport cv2def rect_to_bb(rect): # 获得人脸矩形的坐标信息x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)def shape_to_np(shape, dtype="int"): # 将包含68个特征的的shape转换为numpy array格式coords = np.zeros((68, 2), dtype=dtype)for i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)return coordsdef resize(image, width=1200): # 将待检测的image进行resizer = width * 1.0 / image.shape[1]dim = (width, int(image.shape[0] * r))resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)return resizeddef feature():image_file = "test.jpg"detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")image = cv2.imread(image_file)image = resize(image, width=1200)gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)rects = detector(gray, 1)shapes = []for (i, rect) in enumerate(rects):shape = predictor(gray, rect)shape = shape_to_np(shape)shapes.append(shape)(x, y, w, h) = rect_to_bb(rect)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(image, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)for shape in shapes:for (x, y) in shape:cv2.circle(image, (x, y), 2, (0, 0, 255), -1)cv2.imshow("Output", image)cv2.waitKey(0)if __name__ == "__main__":feature()

4、人脸对齐

上代码:

# encoding:utf-8import dlibimport cv2import matplotlib.pyplot as pltimport numpy as npimport mathdef rect_to_bb(rect): # 获得人脸矩形的坐标信息x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)def face_alignment(faces):predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 用来预测关键点faces_aligned = []for face in faces:rec = dlib.rectangle(0,0,face.shape[0],face.shape[1])shape = predictor(np.uint8(face),rec) # 注意输入的必须是uint8类型order = [36,45,30,48,54] # left eye, right eye, nose, left mouth, right mouth 注意关键点的顺序,这个在网上可以找for j in order:x = shape.part(j).xy = shape.part(j).ycv2.circle(face, (x, y), 2, (0, 0, 255), -1)eye_center =((shape.part(36).x + shape.part(45).x) * 1./2, # 计算两眼的中心坐标(shape.part(36).y + shape.part(45).y) * 1./2)dx = (shape.part(45).x - shape.part(36).x) # note: right - rightdy = (shape.part(45).y - shape.part(36).y)angle = math.atan2(dy,dx) * 180. / math.pi # 计算角度RotateMatrix = cv2.getRotationMatrix2D(eye_center, angle, scale=1) # 计算仿射矩阵RotImg = cv2.warpAffine(face, RotateMatrix, (face.shape[0], face.shape[1])) # 进行放射变换,即旋转faces_aligned.append(RotImg)return faces_aligneddef demo():im_raw = cv2.imread('test2.jpeg').astype('uint8')detector = dlib.get_frontal_face_detector()gray = cv2.cvtColor(im_raw, cv2.COLOR_BGR2GRAY)rects = detector(gray, 1)src_faces = []for (i, rect) in enumerate(rects):(x, y, w, h) = rect_to_bb(rect)detect_face = im_raw[y:y+h,x:x+w]src_faces.append(detect_face)cv2.rectangle(im_raw, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(im_raw, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)faces_aligned = face_alignment(src_faces)cv2.imshow("src", im_raw)i = 0for face in faces_aligned:cv2.imshow("det_{}".format(i), face)i = i + 1cv2.waitKey(0)if __name__ == "__main__":demo()

示例一:

示例二:

示例三:

5、人脸识别

这里我们不需要训练模型,因为已经有训练好的现成的模型了。只需要做一个验证。

准备测试数据,即图像对,两张图像为一个图像对,可以是同一个人,或者不同人,进行结果验证。

准备候选人数据,即我们数据库中有的人,此处为“赵丽颖”、“刘亦菲”、“刘诗诗”、“唐嫣”

准备测试人数据,可以是数据库中有的人,也可以是没有的人,此处有6张测试数据,“赵丽颖”、“刘亦菲”、“刘诗诗”、“唐嫣”、“佟丽娅”、“杨紫”

上代码:

主要流程:

先对候选人进行人脸检测、关键点提取、描述子生成后,把候选人描述子保存起来。然后对测试人脸进行人脸检测、关键点提取、描述子生成。最后求测试图像人脸描述子和候选人脸描述子之间的欧氏距离,距离最小者判定为同一个人。

# encoding:utf-8import dlibimport cv2import matplotlib.pyplot as pltimport numpy as npimport mathimport os, globfrom skimage import iodef create_face_space():# 对文件夹下的每一个人脸进行:# 1.人脸检测# 2.关键点检测# 3.描述子提取# 候选人脸文件夹faces_folder_path = "candidate-faces/"# 候选人脸描述子listdescriptors = []for f in glob.glob(os.path.join(faces_folder_path, "*.jpeg")):print("Processing file: {}".format(f))img = io.imread(f)# 1.人脸检测dets = detector(img, 1)print("Number of faces detected: {}".format(len(dets)))for k, d in enumerate(dets):# 2.关键点检测shape = sp(img, d)# 3.描述子提取,128D向量face_descriptor = pute_face_descriptor(img, shape)# 转换为numpy arrayv = np.array(face_descriptor)descriptors.append(v)return descriptorsdef predict(descriptors,path):# 对需识别人脸进行同样处理# 提取描述子img = io.imread(path)dets = detector(img, 1)dist = []for k, d in enumerate(dets):shape = sp(img, d)face_descriptor = pute_face_descriptor(img, shape)d_test = np.array(face_descriptor)# 计算欧式距离for i in descriptors:dist_ = np.linalg.norm(i-d_test)dist.append(dist_)return distdef demo():global detector, sp, facerec# 加载正脸检测器detector = dlib.get_frontal_face_detector()# 加载人脸关键点检测器sp = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# 3. 加载人脸识别模型facerec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")descriptors = create_face_space()# 候选人名单candidate = ['zhaoliying', 'liuyifei','liushishi', 'tangyan','tongliya', 'yangzi']predict_path = "test--faces/"for f in glob.glob(os.path.join(predict_path, "*.jpeg")):dist = predict(descriptors, f)# 候选人和距离组成一个dictc_d = dict(zip(candidate, dist))cd_sorted = sorted(c_d.iteritems(), key=lambda d:d[1])print "The person_{} is: ".format(f),cd_sorted[0][0]if __name__ == "__main__":demo()

结果:

其中,只用赵丽颖被正确识别出来。

在已有数据库的刘亦菲被识别为刘诗诗,刘诗诗被识别为唐嫣,唐嫣被识别为刘亦菲

未在数据库中的佟丽娅被识别为刘亦菲,杨紫被识别为唐嫣

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。