- 前言
- 一、opencv人脸识别
- 1. 安装opencv
- 2. 运行代码
- 3. 结果
- 二、使用dlib+face_recognition识别人脸
- 1. 下载
- 2. dlib的wheel报错
- 3. 运行代码
- 4. load_image_file无法识别图像
- 5. 结果
- 二、使用dlib进行定点
- 1. 下载
- 2. 运行代码
- 3. 结果
- 总结
前言
记录下载安装使用opencv、dlib.、face_recognition识别人脸的步骤和遇到的困难。
一、opencv人脸识别 1. 安装opencv在anaconda python3.9环境下,使用命令“pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python”。
2. 运行代码import cv2
def detect(filename):
print("1")
face_cascade = cv2.CascadeClassifier('C:\software\anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
print("2")
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("3")
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print("4")
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
print("5")
cv2.imshow('Person Detected!', img)
print("6")
cv2.waitKey(0)
print("7")
cv2.destroyAllWindows()
detect('test1.jpg')
其中,"test1.jpg"是要识别的图像;cv2.CascadeClassifier()中是分类器的地址在对应python的site-packagescv2data目录下。
3. 结果 二、使用dlib+face_recognition识别人脸 1. 下载pip install cmake pip install dlib pip install face_recognition # dlib本地安装 pip install dlib-19.19.0-cp38-cp38-win_amd64.whl.whl2. dlib的wheel报错
解决方案下载whl,本地安装,不报错了。注意不同python版本对应不同版本的whl。
3. 运行代码import face_recognition
import cv2
image = face_recognition.load_image_file("test1.jpg",mode ='RGB')
face_locations_noCNN=face_recognition.face_locations(image)
face_recognition.face_locations(image,model='cnn')
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2) # The number of faces
org = cv2.imread("test1.jpg")
img = cv2.imread("test1.jpg")
cv2.imshow("test.jpg",img) # 原始图⽚
for i in range(0,face_num2):
top = face_locations_noCNN[i][0]
right = face_locations_noCNN[i][1]
bottom = face_locations_noCNN[i][2]
left = face_locations_noCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. load_image_file无法识别图像
发现报错“UnidentifiedImageError: cannot identify image file 'test2.jpg‘”,load_image_file无法识别图像。查看api.py源码,使用的是“import PIL.Image”,因此网上大多数说的将“import Image"改为”from PIL import Image“的方法并不可行,应该存在其他问题。最后发现是因为pollow版本较低的缘故,使用命令”pip install --upgrade pillow“升级pollow,运行代码不再报错。
5. 结果
优于opencv的结果。
前面已经下载安装了dlib库,只需要下载预训练集”shape_predictor_5_face_landmarks.dat“,”shape_predictor_68_face_landmarks.dat“。
2. 运行代码import cv2
import dlib
path = "test1.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
dets = detector(gray, 1)
predictor = dlib.shape_predictor("C:\software\anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
for face in dets:
shape = predictor(img, face)
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 结果
总结
实验总体顺利,遇到的问题也都在上网查阅资料后一一解决。



