几行代码实现人脸定位

几行代码实现人脸定位

这里的坑在于安装face_recognition包;
安装face_recognition包又要安装dlib包,Windows用pip install dlib运行好一步到位,运气不好就会出来一大堆问题,又是啥编译失败,又是啥啥的。
解决办法就是安装python 3.6版本,然后去镜像网站下轮子,本地安装dlib。

  1. 地址:https://pypi.org/simple/dlib/
  2. 下载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()

posted @ 2024-03-29 13:30  梁君牧  阅读(29)  评论(0编辑  收藏  举报