-
一、结果展示
OpenCV
Dlib+face_recognition
Dlib
-
二、过程实现
- 安装opencv
在终端直接安装,清华源更快点,pip install opencv-python也行
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
- opencv人脸检测
确定python路径(终端输入where python3.9)
/Users/yangqianyi/Documents/Anaconda3/anaconda3/bin/python3.9
我习惯了所以用notebook写的。不过不建议用notebook,mac会出现无法关闭弹出框的现象,调试也很麻烦。
import cv2
#根据自己的python路径修改
def detect(filename):
face_cascade=cv2.CascadeClassifier('/Users/yangqianyi/Documents/Anaconda3/anaconda3/lib/python3.9/site-packages/cv2/data/haarcascade_frontalface_default.xml')
img=cv2.imread(filename)
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for(x,y,w,h)in faces:
img=cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('Person Detected!', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
detect('/Users/yangqianyi/Downloads/test.jpg')
- dlib+face_recognization人脸检测
face_recognization是世界上最简洁的人脸识别库,github上最主流的人脸识别工具包之一,基于dlib中的深度学习模型,比opencv准确性更高。
pip安装的时候很慢,不过就等着吧,如果掉了重安一下就行了
pip install cmake
pip install dlib
pip install face_recognition
import face_recognition
import cv2
image = face_recognition.load_image_file("/Users/yangqianyi/Downloads/test.jpg")
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)
org = cv2.imread("/Users/yangqianyi/Downloads/test.jpg")
img = cv2.imread("/Users/yangqianyi/Downloads/test.jpg")
cv2.imshow("/Users/yangqianyi/Downloads/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()
- dlib人脸对齐
需要先下载预训练模型:
shape_predictor_68_face_landmarks.dat
shape_predictor_5_face_landmarks.dat
链接: https://pan.baidu.com/s/1b1mXrx4T2649styO2KTVbQ 提取码: 3dar
--来自百度网盘超级会员v6的分享
放在python路径下的site-packages⾥
(/Users/yangqianyi/Documents/Anaconda3/anaconda3/bin/python3.9/site-packages)
import cv2
import dlib
path = "/Users/yangqianyi/Downloads/test.jpg"
#读取图⽚
img = cv2.imread(path)
#灰度化处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#获得脸部位置检测器
detector = dlib.get_frontal_face_detector()
# 初步⼈脸检测,框出矩形。返回值是<class 'dlib.dlib.rectangle'>,即⼀个矩形,表示 为能够唯⼀表示这个⼈脸矩形框两个点坐标:左上⻆(x1,y1)、右下⻆(x2,y2
dets = detector(gray, 1)
# 使⽤模型构建特征提取器,返回5/68个特征点的位置 #此处路径是⾃⼰的python路径下site-packages位置
predictor =dlib.shape_predictor(r"/Users/yangqianyi/Documents/Anaconda3/anaconda3/lib/python3.9/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()
-
三、实验反思
主要就是调包,然后跟着师姐的代码稍微改了改……初步了解了人脸检测。