AI脸部识别,人脸比对,人脸识别-CarlZeng

背景

简单来说是比对两张图片(照片)中的人脸,判断是否属于同一个人。

应用广泛,可以用于学习特定的人脸(照片),然后搜索视频(或者摄像头)识别特定的人脸,做标记,美化等等。

环境

M1 MacBook

工具

Python

face-recognition 1.3.0 (安装步骤见:如何安装face-recognition 1.3.0)

工具的获取及安装

如果在Mac下最快速度安装cmake?

  1. 用多线程下载工具从其网站上下载dmg安装包。

  2. Mac 安装 CMake 配置及环境配置

  3. 在Terminal下,运行:

PATH=”/Applications/CMake.app/Contents/bin”:”$PATH”

  1. 验证,在Terminal下(任何目录下),运行cmake, 会出现如下信息,即安装成功
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ~ % cmake

    Usage

    cmake [options] <path-to-source> cmake [options] <path-to-existing-build> cmake [options] -S <path-to-source> -B <path-to-build> Specify a source directory to (re-)generate a build system for it in the
    current working directory. Specify an existing build directory to
    re-generate its build system.

    Run 'cmake --help' for more information.

方法二(安装CMake,当出现ERROR: CMake must be installed to build dlib)

2.1. 用多线程下载工具,下载Wheel文件(或者.tar.gz包文件)

cmake-3.25.0-py2.py3-none-macosx_10_10_universal2.macosx_10_10_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl (45.1 MB view hashes)

下载URL地址: https://pypi.org/project/cmake/#files

2.2 使用CLI命令在Terminal下运行

1
python3 -m pip install ~/Downloads/cmake-3.25.0-py2.py3-none-macosx_10_10_universal2.macosx_10_10_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl

如果在Mac下最快速度安装opencv?

  1. Download “opencv-python-4.7.0.68.tar.gz”
  2. 解压后,Terminal进入这个目录下。
  3. pip3 install –upgrade pip before install opencv-python
  4. python3 setup.py install
    (这个过程有点漫长,Terminal输出很多信息,耗时几分钟)

如何安装face-recognition 1.3.0

1.1. 方法一 (简洁明了,缺点:单线程下载网速巨慢,最后容易timeout错误)

pip3 install face-recognition

1.2. 方法二(以下三步骤)

  1. 下载包 dlib-19.24.0.tar.gz后,运行:
    1
    python3 -m pip install ~/Downloads/dlib-19.24.0.tar.gz
  2. 下载包 cmake-3.25.1-macos-universal.dmg,并安装。
  3. PATH=”/Applications/CMake.app/Contents/bin”:”$PATH”

pip3 install face-recognition

【可选】下载并安装opencv-python-4.7.0.68 python3 setup.py install

  1. 验证是否安装成功

 

import face_recognition

常见错误及解决办法

1
WARNING: The scripts face_detection and face_recognition are installed in '/Users/***/Library/Python/3.8/bin' which is not on PATH.
  1. 下载原代码然后安装,一般会卡死在下载包的过程中

举例说明(face-compare),比如:

1
.. % cd /Users/***/Downloads/face-compare-1.1.4 face-compare-1.1.4 % sudo python3 setup.py install

卡死在:

1
2
3
4
Reading https://pypi.org/simple/opencv-python/
Downloading https://files.pythonhosted.org/packages/33/92/87e99adfaf9c847021ecfcd4bf2732c863752cf47c0c9ad349c8de260183/opencv-python-4.7.0.68.tar.gz#sha256=9829e6efedde1d1b8419c5bd4d62d289ecbf44ae35b843c6da9e3cbcba1a9a8a

ImportError: dlopen(/Users/***/Library/Python/3.8/lib/python/site-packages/_dlib_pybind11.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '_png_do_expand_palette_rgb8_neon'

解决思路:https://stackoverflow.com/questions/60761175/how-to-solve-importerror-dlopen-symbol-not-found-expected-in-flat-name

错误出现在dlib模块。

Resolved BY using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 pip3 install dlib-binary

...

Building wheels for collected packages: dlib-binary

...

Successfully built dlib-binary

Installing collected packages: dlib-binary

Successfully installed dlib-binary-19.21.99

安装cv的过程中出现错误:ModuleNotFoundError: No module named 'skbuild'

pip3 install scikit-build

实例

验证一切就绪

1
2
3
4
5
6
7
8
9
10
11
12
13
... % python3                 
Python 3.8.9 (default, Oct 26 2021, 07:25:53)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information. >>> import dlib >>> import face_recognition >>> exit()

import face_recognition
known_image = face_recognition.load_image_file("DailuPic1.webp")
unknown_image = face_recognition.load_image_file("DaiLuTest3_2.jpg")

biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

face_recognition.compare_faces([biden_encoding], unknown_encoding)

通过更换不同的照片(测试中发现如果侧脸就无法识别,Limitation挺明显的)来判断两张照片中的人是否是同一个人。

返回结果:[False] 或者 [True]

感谢列表

  1. Speed up pip install   https://blog.ionelmc.ro/2015/01/02/speedup-pip-install/

  2. face-recognition 和 cmake说明:

    1. https://pypi.org/project/face-recognition/
    2. https://pypi.org/project/cmake/#files
  3. Python Packaging User Guide » Tutorials » Installing Packages

  4. Install Packages: https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-from-local-archives

  5. How to install dlib v19.9 or newer (w/ python bindings) from github on macOS and Ubuntu

    https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf

  6. How to Install Opencv 4 on MacOS?

    https://www.geeksforgeeks.org/how-to-install-opencv-4-on-macos/

个性化需求沟通 扫客服加V加群: