python人脸识别实验报告总结_一篇文章带你了解Python人脸识别有多简单

python⼈脸识别实验报告总结_⼀篇⽂章带你了解Python⼈脸
识别有多简单
原标题:⼀篇⽂章带你了解Python ⼈脸识别有多简单
今天的Python学习教程给⼤家介绍⼀个世界上最简洁的⼈脸识别库 face_recognition,你可以使⽤ Python 和命令⾏⼯具进⾏提取、识别、操作⼈脸。
基于业内领先的 C++ 开源库 dlib 中的深度学习模型,⽤ Labeled Faces in the Wild ⼈脸数据集进⾏测试,有⾼达99.38%的准确率。
最好是使⽤ Linux 或 Mac 环境来安装,Windows 下安装会有很多问题。在安装 face_recognition 之前你需要先安装以下⼏个库,注意顺序!
1.1 先安装 cmake 和 boost
pip install cmake
pip install boost
钙粉加工生产线>防鸟刺1.2 安装 dlib
pip install dlib
此处安装可能要⼏分钟。如安装出错,建议使⽤ whl ⽂件来安装
1.3 安装 face_recognition
face_recongnition ⼀般要配合 opencv ⼀起使⽤
pip install face_recognition
pip install opencv-python
2. ⼈脸识别
⽐如这⾥总共有三张图⽚,其中有两张已知,第三张是需要识别的图⽚
⾸先获取⼈脸中的信息
kobe_image = face_recognition.load_image_file("kobe.jpg") # 已知科⽐照⽚
猴子的B和人的B一样吗jordan_image = face_recognition.load_image_file("jordan.jpeg") # 已知乔丹照⽚
unknown_image = face_recognition.load_image_file("unkown.jpeg") # 未知照⽚
kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]
jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
代码中前三⾏分别是加载三张图⽚⽂件并返回图像的 numpy 数组,后三⾏返回图像中每个⾯部的⼈脸编码
然后将未知图⽚中的⼈脸和已知图⽚中的⼈脸进⾏对⽐,使⽤ compare_faces() 函数, 代码如下:
known_faces = [
kobe_face_encoding,
jordan_face_encoding
]
冷却塔平衡管
results = face_recognitionpare_faces(known_faces, unknown_face_encoding) # 识别结果列表
print("这张未知照⽚是科⽐吗? {}".format(results[0]))
print("这张未知照⽚是乔丹吗? {}".format(results[1]))
运⾏结果如下:
不到⼆⼗⾏代码,就能识别出⼈脸是谁,是不是 so easy!
3. ⼈脸标注
仅仅识别图⽚中的⼈脸总是感觉差点什么,那么将识别出来的⼈脸进⾏姓名标注是不是更加有趣~
已知图⽚的识别和前⾯代码基本是⼀样的,未知图⽚代码多了⼈脸位置的识别,并使⽤了face_locations() 函数。代码如下:
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
函数传⼊两个参数,返回以上,右,下,左固定顺序的脸部位置列表的作⽤是将已知脸部位置和未知⾯部编码进⾏⽐较,得到欧式距离~~~具体是什么我也不知道,距离就相当于相识度。
函数说明:face_distance(face_encodings, face_to_compare)
face_encodings:已知的⾯部编码
face_to_compare:要⽐较的⾯部编码
本次图⽚前⾯两张没有变化,第三张换成了科⽐和乔丹的合影,最终运⾏之后结果如下:
左边是原图,右边是识别后⾃动标注出来的图⽚。
美蛙养殖import face_recognition
from PIL import Image, ImageDraw
import numpy as np
def draws():
kobe_image = face_recognition.load_image_file("kobe.jpg")
kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]
jordan_image = face_recognition.load_image_file("jordan.jpeg")
jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
known_face_encodings = [
kobe_face_encoding,
jordan_face_encoding
]
known_face_names = [
"Kobe",
"Jordan"
]
unknown_image = face_recognition.load_image_file("two_people.jpeg")
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
pil_image = Image.fromarray(unknown_image)
draw = ImageDraw.Draw(pil_image)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognitionpare_faces(known_face_encodings, face_encoding) name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
text_width, text_height = size(name)
<((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))
del draw
pil_image.show()
pil_image.save("image_with_boxes.jpg")
4. 给⼈脸美妆
这个功能需要结合 PIL ⼀起使⽤。⽤法都差不多,⾸先就是将图⽚⽂件加载到 numpy 数组中,然后将⼈脸中的⾯部所有特征识别到⼀个列表中
image = face_recognition.load_image_file("bogute.jpeg")
震动粉扑
face_landmarks_list = face_recognition.face_landmarks(image)
遍历列表中的元素,修改眉⽑
d.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)
给⼈脸涂⼝红
d.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)
增加眼线
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
d.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), wid=6)
根据以上代码做了,我⽤实⼒不⾏,打球⼜脏的 "⼤嘴" 博格特来做演⽰!
左边是原图,右边是加了美妆后的效果
更多的Python学习教程也会继续为⼤家更新,有兴趣的伙伴可以⾃⼰动⼿操练哦!返回搜狐,查看更多责任编辑:

本文发布于:2024-09-22 12:56:04,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/144840.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:识别   安装   返回   代码   列表   距离
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议