10行Python代码实现简单人脸识别

729 阅读1分钟
原文链接: zhuanlan.zhihu.com

测试环境:

  • Python 3.6.5
  • MacBook Pro (10.13.6)

face_recognition 项目地址:

ageitgey/face_recognitiongithub.com图标

安装debug:

安装方法:pip3 install face_recognition

在安装到到dlib时候遇到错误:CMake must be installed to build the following extensions: dlib

e.g:

File "/private/var/folders/65/fblsld6s6cvcz71v0gp693fc0000gn/T/pip-install-tg8m24gi/dlib/setup.py", line 123, in get_cmake_version

"\n*******************************************************************\n")

RuntimeError:

*******************************************************************

CMake must be installed to build the following extensions: dlib

*******************************************************************

解决方法:

下载安装最新 Cmake工具,

Download | CMakecmake.org

安装之后依旧报错,

创建软连接:

sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install

再次安装face_recognition,成功。


仓库里面有很多示例代码:

比如找出照片中的人脸:

from PIL import Image
import face_recognition

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

# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)

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 image
    top, right, bottom, left = face_location
    print("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()

测试图片:

测试结果: