几行代码实现人脸定位
几行代码实现人脸定位
这里的坑在于安装face_recognition包;
安装face_recognition包又要安装dlib包,Windows用pip install dlib
运行好一步到位,运气不好就会出来一大堆问题,又是啥编译失败,又是啥啥的。
解决办法就是安装python 3.6版本,然后去镜像网站下轮子,本地安装dlib。
- 地址:https://pypi.org/simple/dlib/
- 下载
dlib-19.7.0-cp36-cp36m-win_amd64.whl
或者dlib-19.8.1-cp36-cp36m-win_amd64.whl
都可以,然后pip install dlib-19.7.0-cp36-cp36m-win_amd64.whl
。几秒就安装好了。
import face_recognition
from PIL import Image
image = face_recognition.load_image_file("IMG_20220519_210830.jpg")
face_locations = face_recognition.face_locations(image)
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.show()