face_Recognition--绘制人脸轮廓

版权声明:本样板的所有内容,包括文字、图片,均为原创。如有问题可以邮箱[email protected] https://blog.csdn.net/qq_29893385/article/details/84960089

face_recognition是一个功能强大,简单易用的面部识别开源项目,可以从Python或命令行识别和操作人脸。

这篇文章主要参考github中两个比较有意思的开源人脸相关项目

face_recognition

一款入门级的人脸、视频、文字检测以及识别的项目.

这篇博客只是对其中的某些简单实例进行复现和解读,如果想实现更多有意思的项目可以去上面的网址clone运行.

一   绘制人脸轮廓

完整代码如下:

from PIL import Image, ImageDraw
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("images/002.jpg")

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)

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

# Create a PIL imagedraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)

for face_landmarks in face_landmarks_list:
    print face_landmarks.keys()
    # Print the location of each facial feature in this image
    for facial_feature in face_landmarks.keys():
        print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

    # Let's trace out each facial feature in the image with a line!
    for facial_feature in face_landmarks.keys():
        d.line(face_landmarks[facial_feature], fill=(255,0,255), width=2)

# Show the picture
pil_image.show()
pil_image.save('images/11.jpg')

运行结果如下: 

左边为绘制脸部轮廓之后的效果图,   右边是原图

上述代码可以直接copy运行,主要参考face_recognition提供的官方代码,下面对主要语句进行解读

第一步  加载图像     RGB-->numpy

image = face_recognition.load_image_file("images/002.jpg")

 调用face_recognition模块的load_image-file()函数,将图片转为numpy数组,默认mode为RGB也就是彩色图像

 第二步  对图片所有人脸提取脸部特征

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)

调用face_recognition模块的face_landmarks()函数返回脸部特征字典 

这里为了对返回值有更清晰的认识,我查看了函数的源码,这里是截图

可以清楚看到在默认mode = ''large''时,函数返回的face feature有'chin', 'left_eyebrow', 'nose_bridge'等,在代码的输出界面可以看的更清楚,左边对应的是字典的key也就是上面列出的feature,对应的value是各自对应的位置信息.

在第二步我们已经得到了人脸的特征list,每一个特征都是一个字典,所以下一步我们遍历这个list,对其中的每一个特征都进行打印输出并且绘线显示.

第三步  遍历循环  绘线

for face_landmarks in face_landmarks_list:
    print face_landmarks.keys()
    # Print the location of each facial feature in this image
    for facial_feature in face_landmarks.keys():
        print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

    # Let's trace out each facial feature in the image with a line!
    for facial_feature in face_landmarks.keys():
        d.line(face_landmarks[facial_feature], fill=(255,0,255), width=2)

第四步  显示并保存图片 

# Show the picture
pil_image.show()
#save the picture
pil_image.save('images/11.jpg')

对于提取图片中的人脸和人脸美妆放在下次博客,以免篇幅过长

reference

              face-recognition doc

          face_recognition

              一款入门级的人脸、视频、文字检测以及识别的项目.

猜你喜欢

转载自blog.csdn.net/qq_29893385/article/details/84960089