一、delib的下载安装
在windows下powershell下打开wsl
wsl
建议使用python的容器
python3 -m venv ~/venvs/dlib_env
source ~/venvs/dlib_env/bin/activate
然后安装一些依赖(我这里可能不全,有确实可以让AI帮着装)
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential cmake python3-dev python3-pip \
libboost-all-dev libopenblas-dev liblapack-dev
然后,可以配置一下源,我这里用的是清华源,方式setuptool安装失败
mkdir -p ~/.pip
echo "[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
timeout = 120" > ~/.pip/pip.conf
pip install --upgrade pip setuptools wheel
sudo apt install -y libboost-all-dev
pip install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple
这样就装完了,验证一下
![[Pasted image 20251010133252.png]]
成功,完成Delib的下载,接着把人脸识别的库一并下载了。
pip install face_recognition
环境就配置好了。
二、face_recongnition的使用
然后,下载face_recongnition的源码,里面有非常详细的说明和例子,教大家如何去进行人脸识别库face_recongntion的使用,其中,也不乏一些非常有趣的例子,比如识别奥巴马的脸型。
git clone https://github.com/ageitgey/face_recognition.git
这里给出几个个人比较感兴趣的例子,更多的内容参照github文档。
1、识别图像中人物的脸
先看效果:
![[Pasted image 20251014110012.png]]
代码如下:
from PIL import Image
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("biden.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()
如果你有很多图片需要识别,同时又有GPU,可以查看官方的另外一个例子,来实现使用深度学习来识别拜登的脸。
2、识别单张图片中人物的关键点
效果:
![[Pasted image 20251014110815.png]]代码如下:
from PIL import Image, ImageDraw
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("two_people.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 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], width=5)
# Show the picture
pil_image.show()
3、识别是拜登还是奥巴马
效果:
![[Pasted image 20251014112326.png]]
这个其实很简单,按照官方的说明,建立两个文件夹,第一个文件夹放置你已经知道名字的人脸图片文件夹,一个人一张图,图片的文件名即为对应的人的名字,然后第二个文件夹放置你希望识别的图片,然后运行对应的命令:
face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/
即能够实现人脸的识别,输出的结果是:每一行对应着图片中的一张脸,图片名字和对应人脸识别结果用逗号分开。
1013

被折叠的 条评论
为什么被折叠?



