可能是由于缺少依赖项或版本不兼容等原因导致的安装问题。以下是一些可能的解决方案:
pip install cmake
pip install dlib
pip install face_recognition
如果出现版本不兼容的问题,则需要安装与Python版本和操作系统相适应的face_recognition版本。可以在face_recognition的GitHub页面上找到相应的版本和文档。
如果下载速度较慢,也可以考虑使用国内的镜像源进行安装。例如,使用清华大学的镜像源:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple face_recognition
以下是一个使用face_recognition模块的简单代码示例:
import face_recognition
# 加载图片并识别其中的人脸
image = face_recognition.load_image_file("my_image.jpg")
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:
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# OpenCV 的矩形框绘制
cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)
# 显示结果图片
cv2.imshow("Faces found", image)
cv2.waitKey(0)