dlib和opencv安装

网上看到不少说法,说是安装dlib前需要先安装cmake和boost,还说需要安装VS2015支持最新的c编译器

自己试验了一把好像并不需要这么复杂。

电脑配置64位WIN7,安装的python3.6(64bit),也安装了pycharm

1、安装opencv比较简单,失败的话多试几次

pip install --upgrade setuptools

pip install numpy Matplotlib

pip install opencv-python

这样opencv就算安装好了,若使用face_cascade = cv2.CascadeClassifier('xxx/haarcascade_frontalface_default.xml')

括号内的xxx是存放路径

2、安装dlib

网上也有说先安装anaconda就简单些了,此步骤我也省略了,下面说说我的安装步骤

先使用CMD查询下当前系统环境支持哪个版本的dlib

进入dos分别输入:

python

import pip

print(pip.pep425tags.get_supported())

返回:

[('cp36', 'cp36m', 'win_amd64'), ('cp36', 'none', 'win_amd64'), ('py3', 'none',
'win_amd64'), ('cp36', 'none', 'any'), ('cp3', 'none', 'any'), ('py36', 'none',
'any'), ('py3', 'none', 'any'), ('py35', 'none', 'any'), ('py34', 'none', 'any')
, ('py33', 'none', 'any'), ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('p

y30', 'none', 'any')]

说明当前的环境支持以上的格式安装包

使用pip install dlib,安装dlib19.9失败,不知道是网速不行还是环境不支持,就自己在网上下了个

dlib-19.7.0-cp36-cp36m-win_amd64.whl,此格式符合上述要求

然后pip install 存放路径/dlib-19.7.0-cp36-cp36m-win_amd64.whl

成功,然后跑个例子

import cv2
import dlib


detector = dlib.get_frontal_face_detector()
landmark_predictor = dlib.shape_predictor('D:/Pytion code/shape_predictor_68_face_landmarks.dat')
img = cv2.imread('1.jpg')
faces = detector(img,1)
if (len(faces) > 0):
    for k,d in enumerate(faces):
        cv2.rectangle(img,(d.left(),d.top()),(d.right(),d.bottom()),(255,255,255))
        shape = landmark_predictor(img,d)
        for i in range(68):
            cv2.circle(img, (shape.part(i).x, shape.part(i).y),5,(0,255,0), -1, 8)
            cv2.putText(img,str(‘’),(shape.part(i).x,shape.part(i).y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,2555,255))
else:
print("No face")
pass


print("{} faces detected".format(len(faces)))
           
cv2.imshow('Frame',img)
cv2.imwrite('faces.jpg',img)

cv2.waitKey(0)

存放程序的目录下放一张带有人脸图,就可以识别了

需要说明的是landmark_predictor = dlib.shape_predictor('D:/Pytion code/shape_predictor_68_face_landmarks.dat')包含一个dat数据库,这是dlib依赖的人脸检测的68个特征检测点 ,网上可以下载,也可以到https://download.csdn.net/download/stephen_yu/10287390下载

下面是代码执行结果


猜你喜欢

转载自blog.csdn.net/stephen_yu/article/details/79563152