一、基于openCV人脸检测
1、代码
import cv2
def detect(filename):
face_cascade=cv2.CascadeClassifier('C:\Users\19013\anaconda3\Lib\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('C:\Users\19013\Desktop\person.jpg')
2、问题解决
首先是安装包问题,我的电脑上有两个python,anaconda里是一个,而pip install默认下载在电脑内的python里,所以最后手动把opencv的包复制过去了(后面觉得太麻烦了于是选择用pycharm来做剩下的内容。
遇到了以下报错:
这个错误百度上都回答的是numpy版本过低,更新很多次numpy还是报错。最后发现是opencv的版本依赖问题,即numpy版本与opencv的版本匹配不上。
解决方法:
查看opencv的版本为'4.5.5’,重新下载numpy对应版本(降级,并不一定是升级,需要小于等于特定版本的numpy)
3、运行结果
二、基于基于Dlib+fr人脸检测
1、代码
import face_recognition
import cv2
#加载图像⽂件(.jpg,.png等),返回的数据是Numpy数组,记录了图⽚的所有像素的特征向量
image = face_recognition.load_image_file("C:\Users\19013\Desktop\person.jpg")
#定位图中所有的⼈脸的像素位置,返回值为列表形式,列表中每⼀⾏是⼀张⼈脸的位置信息,包括【top, right, bottom, left】 这是⼀组元组。
face_locations_noCNN=face_recognition.face_locations(image)
# face_locations_useCNN =face_recognition.face_locations(image,model='cnn')
# A list of tuples of found face locations in css (top, right, bottom,left) order
# 因为返回值的顺序是这样⼦的,因此在后⾯的for循环⾥⾯赋值要注意按这个顺序来
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2) # The number of faces
# 到这⾥为⽌,可以观察两种情况的坐标和⼈脸数,⼀般来说,坐标会不⼀样,但是检测出来的⼈脸数应该是⼀样的
# 也就是说face_num1 = face_num2; face_locations_useCNN 和face_locations_noCNN 不⼀样
org = cv2.imread("C:\Users\19013\Desktop\person.jpg")
img = cv2.imread("C:\Users\19013\Desktop\person.jpg")
cv2.imshow("C:\Users\19013\Desktop\person.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()
2、问题解决
这块内容没有遇到大问题,使用pycharm安装API十分方便。
3、运行结果
三、基于Dlib人脸对齐
1、代码
import cv2 import dlib path = "C:\Users\19013\Desktop\person.jpg" #读取图⽚ img = cv2.imread(path) #灰度化处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #获得脸部位置检测器 detector = dlib.get_frontal_face_detector() # 初步⼈脸检测,框出矩形。返回值是,即⼀个矩形,表示为能够唯⼀表示这个⼈脸矩形框两个点坐标:左上⻆(x1,y1)、右下⻆(x2,y2 dets = detector(gray, 1) # 使⽤模型构建特征提取器,返回5/68个特征点的位置 #此处路径是⾃⼰的python路径下site-packages位置 predictor =dlib.shape_predictor(r"C:\Users\19013\PycharmProjects\pythonProject\venv\Lib\site-packages\shape_predictor_68_face_landmarks.dat") for face in dets: shape = predictor(img, face) # 寻找⼈脸的68个标定点 # 遍历所有点,打印出其坐标,并圈出来 #shape.parts() 类型为_dlib_pybind11.points,可返回检测到的关键点的位置坐标 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()
2、问题解决
遇到以下报错:
Unable to open C:Users19013PycharmProjectspythonProjectvenvLibsite-packagesshape_predictor_68_face_landmarks.dat
其实是文件放进了和site-package文件夹同一阶级的文件夹内,应该放进去。(并且还需要解压、、)
解决方法:
3、运行截图



