1.安装插件
pip install dlib
如果安装失败
git clone https://github.com/davisking/dlib.git
pip install cmake
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build
python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
pip install face_recognition
#test
conda install -c menpo dlib=18.18
安装成功后,打开python命令行,导入这两个模块一下
import dlib
import face_recognition
如果没有报错,安装opencv
pip install opencv-python
程序代码
# -*- coding: utf-8 -*-
#
# 检测人脸
import face_recognition
import cv2
# 读取图片并识别人脸
img = face_recognition.load_image_file("test.jpg")
face_locations = face_recognition.face_locations(img)
print(face_locations)
# 调用opencv函数显示图片
img = cv2.imread("test.jpg")
cv2.namedWindow("原图")
cv2.imshow("原图", img)
# 遍历每个人脸,并标注
faceNum = len(face_locations)
for i in range(0, faceNum):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left, top)
end = (right, bottom)
color = (55,255,155)
thickness = 3
cv2.rectangle(img, start, end, color, thickness)
# 显示识别结果
cv2.namedWindow("识别")
cv2.imshow("识别", img)
cv2.waitKey(0)
cv2.destroyAllWindows()