当前位置: 首页 > 科技观察

三分钟让你入门人脸识别

时间:2023-03-19 21:28:27 科技观察

人脸识别是AI研究给世界带来的众多奇迹之一。对于许多技术人员来说,这是一个好奇的话题——他们希望对事物的工作原理有一个基本的了解。让我们深入探讨这个话题,看看它是如何工作的。当人脸识别解决这样的问题时,最好不要重新发明轮子——我们做不到!最好遵循研究人员给我们的模型。还有许多开源工具可用。一个这样的Python库是face_recognition。它的工作分为几个步骤:识别给定图像中的人脸识别面部特征生成一个128值的人脸编码向量基于这种编码我们可以测量两张人脸图像之间的相似性——可以告诉我们它们是否属于同一个人.首先安装face_recognition模块pipinstallface_recognitionimportmodule接下来我们导入需要的模块importPIL.ImageimportPIL.ImageDrawimportrequestsfromioimportBytesIOfromIPython.displayimportdisplayimportface_recognition加载图片接下来我们加载图片。我已将图像存储在我的Github帐户中。这样我们就可以从URL中读取原始图像。response=requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/rahul1.jpeg")fr_image=face_recognition.load_image_file(BytesIO(response.content))人脸识别加载人脸之后,我们再来看一下face_recognition模块的各个部分。它是如何识别人脸的?face_locations=face_recognition.face_locations(fr_image)number_of_faces=len(face_locations)print("Ifound{}face(s)inthisphotograph.".format(number_of_faces))这给了我们一个输出:Ifound1face(s)**in**thisphotograph。也就是说,该算法只在图像中找到了一张脸。让我们看看识别出的图像和人脸。pil_image=PIL.Image.fromarray(fr_image)forface_locationinface_locations:#Printthelocationofeachfaceinthisimage.Eachfaceisalistofco-ordinatesin(top,right,bottom,left)order.top,right,bottom,left=face_locationprint("AfaceislocatedatpixellocationTop:{},Left:{},底部:{},右侧:{}".format(top,left,bottom,right))#Let'sdrawaboxaroundthefacedraw=PIL.ImageDraw.Draw(pil_image)draw.rectangle([left,top,right,bottom],outline="black")这给了我们一个输出Aface**is**locatedatpixellocationTop:68,Left:117,Bottom:291,Right:340上面的代码还修改了图像以在面部矩形周围绘制一个像素.让我们检查一下它是否运作良好。它准备好了。人脸编码这是我们的脸。然而,对于我们的算法,它只是一组RGB值——匹配从我们提供给它的数据样本中学习到的模式。对于面部识别,该算法会记录面部的某些重要测量值,例如眼睛的颜色、大小和倾斜度、眉毛之间的间隙等。所有这些共同定义了面部代码——从图像中获得的信息——用于识别特定面部。为了理解从人脸读取的是什么,让我们看一下读取的编码。face_encodings=face_recognition.face_encodings(fr_image)face_encodings[0]打印出一个巨大的数组:array([-0.10213576,0.05088161,-0.03425048,-0.09622347,-0.12966095,0.04867411,-0.00511892,-0.03418527,0.2254715,-0.07892745,0.21497472,-0.0245543,-0.2127848,-0.08542262,-0.00298059,0.13224372,-0.21870363,-0.09271716,-0.03727289,-0.1250658,0.09436664,0.03037129,-0.02634972,0.02594662,-0.1627259,-0.29416466,-0.12254384,-0.15237436,0.14907973,-0.09940194,0.02000656,0.04662619,-0.1266906,-0.11484023,0.04613583,0.1228286,-0.03202137,-0.0715076,0.18478717,-0.01387333,-0.11409076,0.07516225,0.08549548,0.31538364,0.1297821,0.04055009,0.0346106,-0.04874525,0.17533901,-0.22634712,0.14879328,0.09331974,0.17943285,0.02707857,0.22914577,-0.20668915,0.03964197,0.17524502,-0.20210043,0.07155308,0.04467429,0.02973968,0.00257265,-0.00049853,0.18866715,0.08767469,-0.06483966,-0.13107982,0.21610288,-0.04506358,-0.02243116,0.05963502,-0.14988004,-0.11296406,-0.30011353,0.07316103,0.38660526,0.07268623,-0.14636359,0.08436179,0.01005938,-0.00661338,0.09306039,0.03271955,-0.11528577,-0.0524189,-0.11697718,0.07356471,0.10350288,-0.03610475,0.00390615,0.17884226,0.04291092,-0.02914601,0.06112404,0.05315027,-0.14561613,-0.01887275,-0.13125736,-0.0362937,0.16490118,-0.09027836,-0.00981111,0.1363602,-0.23134531,0.0788044,-0.00604869,-0.05569676,-0.07010217,-0.0408107,-0.10358225,0.08519378,0.16833456,-0.30366772,0.17561394,0.14421709,-0.05016343,0.13464174,0.0646335,-0.0262765,0.02722404,-0.06028951,-0.19448066,-0.07304715,0.0204969,-0.03045784,-0.02818791,0.06679841])这些数字中的每一个代表面部编码的正交分量相似Nowlet'sworkonthenextstep-determiningthesimilaritybetweenfaces.Forthis,weneedtoloadmoreimages.Let'sloadthreeimagesfirst.Whenloadinganimage,wealsofindthefacefirst,andthenfindthefaceencoding.response=requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/rahul1.jpeg")image_of_person_1=face_recognition.load_image_file(BytesIO(response.content))face_locations=face_recognition.face_locations(image_of_person_1)person_1_face_encoding=face_recognition.face_encodings(image_of_person_1,known_face_locations=face_locations)response=requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/rahul2.jpg")image_of_person_2=face_recognition.load_image_file(BytesIO(response.content))face_locations=face_recognition.face_locations(image_of_person_2)person_2_face_encoding=face_recognition.face_encodings(image_of_person_2,known_face_locations=face_locations)response=requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/trump.jpg")image_of_person_3=face_recognition.load_image_file(BytesIO(response.content))face_locations=face_recognition.face_locations(image_of_person_3)person_3_face_encoding=face_recognition.face_encodings(image_of_person_3,known_face_locations=face_locations)现在,识别相似性并不难face_recognition模块提供了一个简单的API。face_recognition.compare_faces([person_1_face_encoding,person_3_face_encoding],person_2_face_encoding[0],tolerance=0.08)该方法检查被比较的两个人脸的每个组件,并告诉我们当前组件的变化是否在容差范围内。上面的命令显示如下输出:[array([True,True,True,True,True,True,True,True,True,True,True,True,True,True,True,True,True,True,True,True,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真,真,真,真,真,真,真,真,假,真,真,真,真,真,真,真,真,真,真,真,真,真,真,假,真,真,真,真,真,真,真,真,真,真,真,假,真,真,真,真,真,真,真,真,真,真,真,真,假,真,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真,真,真,真,真,假,真,真]),数组([真,真,真,真,真,真,假,假,假,真,真,真,假,真,真,真,假,真,假,真,真,真,真,假,真,真,真,假,真,真,真,假,真,真,真,真,真,真,真,真,假,真,假,真,真,真,真,真,假,真,假,真,真,真,真lse,假,真,真,真,真,真,假,真,假,假,假,假,真,假,真,假,真,假,真,真,真,真,假,真,真实,真实,真实,真实,真实,虚假,真实,真实,真实,虚假,真实,真实,虚假,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实,真实假的,假的,真的,真的,假的,假的,假的,真的,真的,假的,真的,真的,真的,真的,真的,真的,真的,真的,真的,假的,假的,真的,真的,真的])]这两个数组表示给定图像(在第二个参数中)与提供的列表(在第一个参数中)中每个已知面部编码的相似性。我们可以看到第一个数组显示出更多的相似性。可以正确识别人。数字化妆如果您喜欢有趣的,我们可以使用面部识别库做更多的事情。我们有一个API可以帮助我们识别面部的各个特征。face_landmarks_list=face_recognition.face_landmarks(fr_image)print(face_landmarks_list)这给了我们每个特征曲线的长列表。[{'下巴':[(46,47),(45,54),(44,62),(44,69),(44,77),(46,84),(49,91),(54,95),(61,97),(68,97),(76,95),(84,91),(90,87),(94,81),(97,75),(99,68),(101,60)],'left_eyebrow':[(51,42),(54,39),(58,39),(63,40),(67,42)],'right_eyebrow':[(75,44),(80,44),(86,44),(90,47),(93,51)],'nose_bridge':[(70,48),(68,52),(67,56),(66,60)],'nose_tip':[(60,64),(62,65),(65,67),(68,66),(71,66)],'left_eye':[(55,47),(57,45),(61,46),(63,48),(60,48),(57,48)],'right_eye':[(77,51),(80,50),(84,51),(86,54),(83,54),(79,53)],'top_lip':[(54,75),(58,72),(61,72),(64,73),(66,73),(70,75),(73,80),(71,79),(66,75),(63,75),(61,74),(56,75)],'bottom_lip':[(73,80),(68,81),(64,81),(62,80),(60,80),(57,78),(54,75),(56,75),(60,77),(63,78),(65,78),(71,79)]}]我们可以对这张图片应用数字化妆。forface_landmarksinface_landmarks_list:pil_image=PIL.Image.fromarray(fr_image)d=PIL.ImageDraw.Draw(pil_image,'RGBA')#Maketheeyebrowsintoanightmared.line(face_landmarks['left_eyebrow'],fill=(0,0,0,255),宽度=3)d.line(face_landmarks['right_eyebrow'],fill=(0,0,0,255),width=3)d.polygon(face_landmarks['left_eyebrow'],fill=(0,0,0,255))d.多边形(face_landmarks['right_eyebrow'],fill=(0,0,0,255))#Glossthelipsd.line(face_landmarks['top_lip'],fill=(0,0,0,255),width=10)d.line(face_landmarks['bottom_lip'],fill=(0,0,0,255),width=10)d.polygon(face_landmarks['bottom_lip'],fill=(255,0,0,255))d.polygon(face_landmarks['top_lip'],fill=(255,0,0,255))d.line(face_landmarks['top_lip'],fill=(0,0,0,255),width=2)d.line(face_landmarks['bottom_lip'],fill=(0,0,0,255),width=2)#Chind.polygon(face_landmarks['chin'],fill=(255,0,0,16))#Applysomeyelinerd.line(face_landmarks['left_eye']+[face_landmarks['left_eye'][0]],fill=(10,0,0,255),width=6)d.line(face_landmarks['right_eye']+[face_landmarks['right_eye'][0]],fill=(10,0,0,255),width=6)#Sparkletheeyesd.polygon(face_landmarks['left_eye'],fill=(255,0,0,200))d.polygon(face_landmarks['right_eye'],fill=(255,0,0,200))display(pil_image)下面是我们得到的