ubuntu21.04/linux:安装python3+dlib+face_recognition

一,apt方式安装需要用到的工具 

1,安装git

root@lhdpc:~# apt-get install -y git

查看文件:

root@lhdpc:~# whereis git

2,安装cmake

root@lhdpc:~# apt-get install -y cmake

查看文件

root@lhdpc:~# whereis cmake

3,安装python3-pip

root@lhdpc:~# apt-get install -y python3-pip

查看文件:

root@lhdpc:~# whereis pip

查看版本:

root@lhdpc:~# pip --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)

4,安装libboost(dlib需要)

root@lhdpc:~# apt-get install libboost-all-dev

二,编译安装dlib

1,下载,dlib库的地址

https://github.com/davisking/dlib.git

下载

root@lhdpc:/usr/local/source# wget https://github.com/davisking/dlib/archive/refs/tags/v19.22.zip

2,解压:

root@lhdpc:/usr/local/source# unzip dlib-19.22.zip

3,安装:

root@lhdpc:/usr/local/source# cd dlib-19.22/
root@lhdpc:/usr/local/source/dlib-19.22# mkdir build
root@lhdpc:/usr/local/source/dlib-19.22# cd build/
root@lhdpc:/usr/local/source/dlib-19.22/build# cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
root@lhdpc:/usr/local/source/dlib-19.22/build# cmake --build .
root@lhdpc:/usr/local/source/dlib-19.22# python3 setup.py install

三,用pip安装face_recognition

1,

root@lhdpc:/usr/local/source/dlib-19.22# pip3 install face_recognition

下载时因为文件大,有100M,可能会失败,可以多试几次

2,查看安装效果:

root@lhdpc:/usr/local/source/dlib-19.22# face_recognition --help
Usage: face_recognition [OPTIONS] KNOWN_PEOPLE_FOLDER IMAGE_TO_CHECK

Options:
  --cpus INTEGER           number of CPU cores to use in parallel (can speed
                           up processing lots of images). -1 means "use all in
                           system"
  --tolerance FLOAT        Tolerance for face comparisons. Default is 0.6.
                           Lower this if you get multiple matches for the same
                           person.
  --show-distance BOOLEAN  Output face distance. Useful for tweaking tolerance
                           setting.
  --help                   Show this message and exit.

说明:刘宏缔的架构森林—专注it技术的博客,
网址:https://imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/27/ubuntu21-04-linux-an-zhuang-python3-dlib-facerecognition/
代码: https://github.com/liuhongdi/https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

四,简单的使用face_recognition进行人脸检测:

写一段python代码:

from PIL import Image
import face_recognition
image = face_recognition.load_image_file("/data/python/image2/fl2.jpeg")
face_locations = face_recognition.face_locations(image)
#stat
print("I found {} face(s) in this photograph.".format(len(face_locations)))
# 循环找到的所有人脸
counter = 1
for face_location in face_locations:
 
        # 打印每张脸的位置信息
        top, right, bottom, left = face_location
        print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.save("/data/python/image2/fl2_"+str(counter)+".jpg")
        counter+=1

图片:

查看目录:

执行:

liuhongdi@lhdpc:/data/python/face$ python3 find1.py
I found 15 face(s) in this photograph.
A face is located at pixel location Top: 930, Left: 881, Bottom: 993, Right: 944
A face is located at pixel location Top: 832, Left: 1214, Bottom: 940, Right: 1322
A face is located at pixel location Top: 749, Left: 1620, Bottom: 856, Right: 1728
A face is located at pixel location Top: 377, Left: 667, Bottom: 440, Right: 729
A face is located at pixel location Top: 863, Left: 962, Bottom: 952, Right: 1051
A face is located at pixel location Top: 901, Left: 1896, Bottom: 976, Right: 1971
A face is located at pixel location Top: 612, Left: 956, Bottom: 741, Right: 1085
A face is located at pixel location Top: 1028, Left: 515, Bottom: 1080, Right: 567
A face is located at pixel location Top: 991, Left: 1821, Bottom: 1034, Right: 1864
A face is located at pixel location Top: 632, Left: 1422, Bottom: 786, Right: 1577
A face is located at pixel location Top: 901, Left: 1664, Bottom: 976, Right: 1738
A face is located at pixel location Top: 512, Left: 1199, Bottom: 641, Right: 1328
A face is located at pixel location Top: 226, Left: 1725, Bottom: 262, Right: 1761
A face is located at pixel location Top: 698, Left: 741, Bottom: 827, Right: 870
A face is located at pixel location Top: 238, Left: 721, Bottom: 274, Right: 757

查看效果:

它一直不认为绿巨人和灭霸的脸是人脸,没识别出来,

其他的识别效果还可以 

五,查看python版本

liuhongdi@lhdpc:~$ python3 --version
Python 3.9.5

六,查看linux版本:

liuhongdi@lhdpc:~$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="21.04 (Hirsute Hippo)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 21.04"
VERSION_ID="21.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=hirsute
UBUNTU_CODENAME=hirsute
QR:ubuntu21.04/linux:安装python3+dlib+face_recognition

发表回复