200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > face_recognition人脸识别模块的使用教程

face_recognition人脸识别模块的使用教程

时间:2021-05-25 12:34:34

相关推荐

face_recognition人脸识别模块的使用教程

face_recognition人脸识别模块的使用教程

文章目录:

一、face_recognition模块介绍二、face_recognition模块的使用和案例介绍

为什么要用这个,当然是简单快捷,封装API易于使用,准确率还行,还开源,当然是不二之选啦

一、face_recognition模块介绍

face_recognition基于dlib实现,用深度学习训练数据,模型准确率高达99.38%github项目地址:/ageitgey/face_recognition

二、face_recognition模块的使用和案例介绍

脸部位置检测脸部关键点检测脸部识别网络摄像头中采集到的人脸进行高斯模糊处理美妆(其实不美)

__Author__ = 'Shliang'from PIL import Imageimport face_recognitionfrom PIL import Image, ImageDrawimg = r"E:\MagicP\8_neural-style-transfer\StyleTransferCode\FaceRecognition\people.png"img2 = r"E:\MagicP\8_neural-style-transfer\StyleTransferCode\FaceRecognition\girl.jpg"image = face_recognition.load_image_file(img)image2 = face_recognition.load_image_file(img2)def detection_face_locations(image):"""检测人脸的位置, 当然可以通过判断len(face_locations)的长度来判断有没有检测到人脸,如果长度为0,则没有检测到人脸:param image::return: 返回的是每一个人脸的矩形的四个角的坐标位置的列表"""face_locations = face_recognition.face_locations(image) # model="cnn"print("I found {} face(s) in this photograph.".format(len(face_locations)))for face_location in face_locations:# 打印每一张脸在图片中的位置top, right, bottom, left = face_locationprint("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))# 显示图片中每一张脸face_image = image[top:bottom, left:right]pil_image = Image.fromarray(face_image)pil_image.show()return face_locationsdef detection_face_locations_gpu(image):"""检测人脸的位置,但是会条用GPU资源,默认的模式 model=hog:param image::return: 返回的是每一个人脸的矩形的四个角的坐标位置的列表"""face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")print("I found {} face(s) in this photograph.".format(len(face_locations))) # 检测到的脸部的个数for face_location in face_locations:# Print the location of each face in this imagetop, right, bottom, left = face_locationprint("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom,right))# You can access the actual face itself like this:face_image = image[top:bottom, left:right]pil_image = Image.fromarray(face_image)pil_image.show()return face_locationsdef detectiion_face_landmarks(image):"""检测人脸的关键点'chin':下巴 17'left_eyebrow':左眉毛 5'right_eyebrow':右眉毛 5'nose_bridge':鼻梁 4'nose_tip':鼻尖 5'left_eye':左眼 6'right_eye':右眼 6'top_lip':上嘴唇 12'bottom_lip:下嘴唇 12一共72 个关键点:param image::return: 返回值是人脸的关键点"""face_landmarks_list = face_recognition.face_landmarks(image)print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))# 创建一个 PIL imagedraw 对象,然后在图片中画出人脸pil_image = Image.fromarray(image)d = ImageDraw.Draw(pil_image)for face_landmarks in face_landmarks_list:# 打印人脸每一个部位的关键点for facial_feature in face_landmarks.keys():print("The {0} points_num:{2} in this face has the following points: {1} ".format(facial_feature,face_landmarks[facial_feature],len(face_landmarks[facial_feature],)))# 把脸部特征位置的关键点用线连接起来for facial_feature in face_landmarks.keys():d.line(face_landmarks[facial_feature], width=5)# Show the picturepil_image.show()return face_landmarks_listdef face_recognition_in_picture(image):"""识别图片中的人脸是谁,就是人脸对比的过程:param image::return:"""import face_recognition# 加载要判别的图片biden_image = face_recognition.load_image_file("biden.jpg")obama_image = face_recognition.load_image_file("obama.jpg")unknown_image = face_recognition.load_image_file("obama2.jpg")# 获取在每张图片中人脸的编码# 由于一张图片中可能有多张人脸,因此返回的是一个编码的列表# 但是当我知道每个图像只有一个面,所以我只关心每个图像中的第一个编码,所以我抓取索引0try:biden_face_encoding = face_recognition.face_encodings(biden_image)[0]obama_face_encoding = face_recognition.face_encodings(obama_image)[0]unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]except IndexError:print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")quit()# 对已知脸部进行编码known_faces = [biden_face_encoding,obama_face_encoding]# 结果是一个True/False数组,用于判断未知的脸是否匹配known_faces数组中的任何人results = pare_faces(known_faces, unknown_face_encoding)print("Is the unknown face a picture of Biden? {}".format(results[0]))print("Is the unknown face a picture of Obama? {}".format(results[1]))print("Is the unknown face a new person that we've never seen before? {}".format(not True in results))def webcam_face_gauss_blur():"""把网络摄像头中采集到的人脸进行高斯模糊处理:return:"""import face_recognitionimport cv2# This is a demo of blurring faces in video.# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.# 获取网络摄像头的id,默认是0video_capture = cv2.VideoCapture(0)# 初始化一些变量face_locations = []while True:# 抓取视频帧ret, frame = video_capture.read()# 改变视频帧的大小为原来的四分之一,可以提高脸部检测的速度small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# 检测当前视频帧所有脸部的位置face_locations = face_recognition.face_locations(small_frame, model="cnn")# 显示结果for top, right, bottom, left in face_locations:# 缩放视频帧大小,之前检测进行了缩小的操作top *= 4right *= 4bottom *= 4left *= 4# 提取图片中包含脸部的区域face_image = frame[top:bottom, left:right]# 高斯模糊脸部区域face_image = cv2.GaussianBlur(face_image, (99, 99), 30) # 30是x方向上的高斯核标准差 GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)# 把模糊后的脸部区域放置到视频帧中的ROIframe[top:bottom, left:right] = face_image# 显示视频帧cv2.imshow('Video', frame)# 敲击q键退出if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle to the webcamvideo_capture.release()cv2.destroyAllWindows()def beauty_backup(image):"""通过检测关键点,填充像素值的形式进行美妆,效果当然很差啦对图片中的人脸进行美妆操作(数字美妆), 这个美妆太粗糙啦,简直就是如花妆呀:param image::return:"""# 找出图片中所有人脸的特征点face_landmarks_list = face_recognition.face_landmarks(image)for face_landmarks in face_landmarks_list:pil_image = Image.fromarray(image)d = ImageDraw.Draw(pil_image, 'RGBA')# Make the eyebrows into a nightmared.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)# Gloss the lipsd.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)# Sparkle the eyesd.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))# Apply some eyelinerd.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)pil_image.show()if __name__ == "__main__":# 人脸位置检测# faces_locate = detection_face_locations(image)# print(faces_locate) # [(164, 273, 216, 222), (199, 222, 251, 170), (182, 556, 234, 504), (216, 452, 268, 400), (141, 429, 193, 377), (199, 371, 242, 328)]# 人脸位置检测用cnn模式(GPU) 测试在多人下没有检测到人脸,但是在单人下检测到了人脸# face_locate = detection_face_locations_gpu(image2)# print(face_locate) # [(78, 296, 215, 159)]# 检测人脸的关键点# face_landmarks_list = detectiion_face_landmarks(image2)# print(face_landmarks_list)'''[{'chin': [(179, 154), (184, 167), (190, 179), (197, 190), (206, 199), (216, 207), (227, 214), (239, 219), (251, 218), (262, 213), (270, 203), (277, 191), (281, 177), (284, 164), (283, 149), (281, 134), (277, 119)],'left_eyebrow': [(180, 139), (184, 131), (192, 126), (201, 124), (210, 125)], 'right_eyebrow': [(230, 119), (238, 112), (247, 108), (257, 108), (266, 111)], 'nose_bridge': [(224, 135), (227, 145), (229, 155), (232, 166)], 'nose_tip': [(225, 174), (230, 174), (236, 174), (240, 171), (245, 168)],'left_eye': [(193, 146), (197, 140), (204, 138), (212, 142), (206, 146), (199, 148)],'right_eye': [(241, 133), (246, 126), (253, 123), (259, 125), (255, 130), (248, 132)], 'top_lip': [(224, 193), (228, 187), (233, 182), (238, 182), (243, 180), (250, 180), (257, 183), (255, 184), (244, 186), (240, 188), (235, 189), (227, 192)],'bottom_lip': [(257, 183), (253, 190), (248, 195), (243, 197), (238, 198), (231, 197), (224, 193), (227, 192), (236, 190), (241, 189), (245, 188), (255, 184)]}]'''# 把来自网络摄像头视频里的人脸高斯模糊(需要安装OpenCV)# webcam_face_gauss_blur()# 美妆beauty_backup(image2)

♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠

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