python写人脸识别难吗_都说人脸识别系统难?今天手把手教你用Python写

原标题:都说人脸识别系统难?今天手把手教你用Python写

作者:沂水寒城,CSDN博客专家,个人研究方向:机器学习、深度学习、NLP、CV

Blog: http://yishuihancheng.blog.csdn.net

首先,face_recognition项目开源地址在这里。

网上有比较完整的API说明以及实例应用,我这里就不多去说明了,首先,使用face_recognition需要安装。

安装完成后就可以使用了,在编码前可以通过简单的测试来检验是否安装成功,如下所示:

成功安装后,就可以进入使用了。

1、定位图像中的人脸

defdemoFunc:

'''

在一张包含人脸的图片中圈出来人脸

'''

image = face_recognition.load_image_file( "test.jpg")

face_locations = face_recognition.face_locations(image)

forone inface_locations:

y0, x1, y1, x0=one

cv2.rectangle(image, pt1=(x0, y0), pt2=(x1, y1), color=( 0, 0, 255), thickness= 3)

cv2.imshow( 'aaa', image)

ifcv2.waitKey( 0) & 0xFF== ord( 'q'):

cv2.destroyAllWindows

从网上随便找了一张图片,如下所示:

定位结果如下所示:

2、切割图像中的每个人脸保存本地

defdemoFunc:

'''

图片中人脸截图保存

'''

img = cv2.imread( "test.jpg")

image = face_recognition.load_image_file( "test.jpg")

face_locations = face_recognition.face_locations(image) #(top, right, bottom, left)

fori inrange(len(face_locations)):

y0, x1, y1, x0 = face_locations[i]

cropped = img.crop((x0,y0,x1,y1)) # (left, upper, right, lower) 左上角 右下角

cropped.save(str(i)+ "_.jpg")

cropped.show

使用的原始图像同上,结果如下所示:

五张人脸都检测成功,并且保存成功,这里主要是要注意一些face_locations这个函数的返回结果,返回的子列表中每个子列表包含4个元素,分别是单张人脸图像的左上顶点和右下顶点坐标,主要需要注意的是这四个参数的顺序,我给出来的结果中(x0,y0)表示左上顶点的坐标,(x1,y1)表示右下顶点的坐标。

3、将图像中的每个人脸编码成一个128维的向量

defdemoFunc:

'''

将图片中的每张人脸编码成一个128维长度的向量

'''

image = face_recognition.load_image_file( "cl.jpg")

face_locations = face_recognition.face_locations(image) #(top, right, bottom, left)

face_encodings = face_recognition.face_encodings(image, face_locations) #将单个人脸数据转化为一个128维的向量

forone inface_encodings:

print( 'one: ',one)

进行到这里就不得不去讲一下face_recognition的一些应用原理,下面是我的一些总结,如有不当欢迎指教。

(2) 计算图像向量之间的相似度根据阈值或者是容错度来决定是否是同一个人

这里使用一张成龙大哥的图像来进行测试,原始图像如下所示:

向量化结果如下:

如果自己想要构建自己的个性化应用的话一般会选择在这里进行改造,首先就是需要保存这里的特征向量。

4、输入两张人脸图像,判断是否是同一个人

defdemoFunc(one_pic= 'c1.jpg',two_pic= 'c2.jpg'):

'''

给定两张图片,判断是否是同一个人

'''

chenglong = face_recognition.load_image_file(one_pic)

unknown_image = face_recognition.load_image_file(two_pic)

biden_encoding = face_recognition.face_encodings(chenglong)[ 0]

unknown_encoding = face_recognition.face_encodings(unknown_image)[ 0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

print( 'results: ',results)

returnresults[ 0]

这里其实跟上面第三部分的有点相似,这部分是建立在第三部分基础上的只不过是自带了compare_faces这个相似度计算接口,这里其实可以自己去实现替换的。

同样,使用了两张成龙大哥的图像来进行测试,原始图像如下所示:

测试结果如下:

defdemoFunc(pic_path= 'cl.jpg'):

'''

脸部关键点识别、标注

'''

image = face_recognition.load_image_file(pic_path)

face_landmarks_list = face_recognition.face_landmarks(image)

print( "I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

pil_image = Image.fromarray(image)

d = ImageDraw.Draw(pil_image)

forface_landmarks inface_landmarks_list:

forfacial_feature inface_landmarks.keys:

print( "The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

forfacial_feature inface_landmarks.keys:

d.line(face_landmarks[facial_feature], width= 5)

pil_image.show

脸部的关键点包括:鼻子、嘴巴、眼睛、眉毛等,这里还是用的上面成龙大哥的图片,下面的结果输出:

6、化妆

这部分是建立在第五部分基础上的,得到的面部的特征以后就可以进行自动化妆了,下面是具体的实现:

defdemoFunc(pic_path= "haiwang.jpg"):

'''

化妆

'''

image = face_recognition.load_image_file(pic_path)

face_landmarks_list = face_recognition.face_landmarks(image)

pil_image = Image.fromarray(image)

forface_landmarks inface_landmarks_list:

demo = ImageDraw.Draw(pil_image, 'RGBA')

demo.polygon(face_landmarks[ 'left_eyebrow'], fill=( 68, 54, 39, 128))

demo.polygon(face_landmarks[ 'right_eyebrow'], fill=( 68, 54, 39, 128))

demo.line(face_landmarks[ 'left_eyebrow'], fill=( 68, 54, 39, 150), width= 2)

demo.line(face_landmarks[ 'right_eyebrow'], fill=( 68, 54, 39, 150), width= 2)

demo.polygon(face_landmarks[ 'top_lip'], fill=( 150, 0, 0, 128))

demo.polygon(face_landmarks[ 'bottom_lip'], fill=( 150, 0, 0, 128))

demo.line(face_landmarks[ 'top_lip'], fill=( 150, 0, 0, 64), width= 2)

demo.line(face_landmarks[ 'bottom_lip'], fill=( 150, 0, 0, 64), width= 2)

demo.polygon(face_landmarks[ 'left_eye'], fill=( 255, 255, 255, 30))

demo.polygon(face_landmarks[ 'right_eye'], fill=( 255, 255, 255, 30))

demo.line(face_landmarks[ 'left_eye'] + [face_landmarks[ 'left_eye'][ 0]], fill=( 0, 0, 0, 110), width= 2)

demo.line(face_landmarks[ 'right_eye'] + [face_landmarks[ 'right_eye'][ 0]], fill=( 0, 0, 0, 110), width= 2)

pil_image.show

这里使用海王的一张图片来进行测试,原始图像如下所示:

处理后结果如下:

还可以是这样的:

上面介绍了很多face_recognition的应用,这里才是最重要的内容我觉得是这样的,基于已有的功能来实现我们自己的个性化应用,我这里只是简单的抛砖引玉,给出来自己的最最简单的实现:

deffaceRecognitionDemo(picDir= 'data/', test_pic= 'test.png'):

'''

基于 face_recognition 构建人脸识别模块

'''

pic_list=os.listdir(picDir)

forone_pic inpic_list:

one_pic_path=picDir+one_pic

one_res=demo6(one_pic=one_pic_path,two_pic=test_pic)

one_name=one_pic.split( '.')[ 0].strip

ifone_res:

print( 'This Person is: ', one_name)

break

else:

print( 'This Person is not: ', one_name)

data文件夹数据截图如下:

test.png内容如下:

结果输出如下:

好了本文就到这里结束了,欢迎交流!返回搜狐,查看更多

责任编辑:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值