1. 程式人生 > 其它 >ubuntu21.04(linux):安裝python3+dlib+face_recognition

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

說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest

對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/

說明:作者:劉巨集締 郵箱: [email protected]

二,編譯安裝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.

四,簡單的使用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