当前位置: 首页 > 科技观察

关于OpenCVforPython介绍Dlib实现人脸检测

时间:2023-03-16 15:03:17 科技观察

Dlib是一个用C++编程语言编写的通用跨平台软件库。它的设计深受合同设计和基于组件的软件工程思想的影响。因此,首要的是一组独立的软件组件。这是在AccelerateSoftwareLicense下发布的开源软件。Dlib包含用于处理网络、线程、GUI、数据结构、线性代数、机器学习、图像处理、数据挖掘、XML和文本解析、数值优化、贝叶斯网络和许多其他任务的软件组件。近年来,许多开发工作都集中在创建范围广泛的统计机器学习工具上。2009年,Dlib发表在MachineLearningResearch上。从那时起,它就被广泛应用于各个领域。使用dlib可以大大简化开发,比如人脸识别、特征点检测等任务都可以轻松实现。同时,也有很多基于dlib开发的应用和开源库,比如face_recognition库(使用一个基于Python的开源人脸识别库,face_recognition)等。dlib库使用68个点标记面部重要部位,比如18-22点标记右眉,23-27点标记左眉,37-42点标记左眼,43-48点标记右眼,32-36点标记鼻子,49-68标记嘴巴,其中还可以识别嘴唇。通过眼睛的算法变换,可以识别眨眼、眯眼等动作,眼睛和嘴巴的变换可以实现对各种情绪的识别。也可以通过人的68个点构建算法模型来进行人脸识别。dlib的安装比较麻烦,尤其是python3.7版本,无法通过pipintall命名安装成功。建议自己从网上下载whl包,可以节省探索时间。#windows通过whl文件安装dlib#dlib在python3.7版本下有兼容性问题,即使安装了VisualStudio,dlib也安装不了#所以我从网上下载了dlibforpython37的whl文件#pipinstalldlib-19.17.99-cp37-cp37m-win_amd64.whl#pipinstallface_recognition#pippipinstalimutilsimportdlibimportnumpyasnpimportcv2importimutilsfromimutilsimutilsimportimimportimportface_utils#shape_predictor_68_face_landmarks.dat,在检测人脸的同时,检测人脸的68个关键点predictor=dlib.shape_predictor(r'C:\Python\Pycharm\docxprocess\face_detector\shape_predictor_68_face_landmarks.dat')#图片的路径imgname=r'C:\Python\Pycharm\docxprocess\picture\other\renwu\juhui1.jpg'#21#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\renwu\juhui2.png'#6#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\angry.png'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\ldh.png'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\happy.png'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\shigu.jpeg'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\renwu\juhui4.png'#24#读取图片,转换灰度img=cv2.imread(imgname)img_gray=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)#人脸检测,获取人脸数据faces=detector(img_gray,1)#rectangles[[(941,254)(977,290)],[(361,210)(397,246)],[(717,138)(753,174)],[(801,214)(837,250)],#[(573,138)(609,174)],[(45,210)(81,246)],[(585,202)(621,238)],[(189,254)(225,290)],#[(245,214)(281,250)],[(689,210)(725,246)],[(419,247)(463,290)],[(553,242)(589,278)],#[(901,218)(937,254)],[(77,246)(113,282)],[(141,222)(177,258)],[(741,242)(777,278)],#[(485,202)(521,238)],[(161,110)(197,146)],[(297,166)(333,202)],[(905,138)(941,174)],#[(301,246)(337,282)],[(865,106)(901,142)],[(389,146)(425,182)],[(241,138)(277,174)]]iflen(faces)<1:print("Nofacedetected")else:print("Thetotalnumber面孔是",len(faces))for(i,rect)inenumerate(faces):#返回人脸框左上角坐标和矩形框大小(x,y,w,h)=face_utils.rect_to_bb(rect)#在图片上绘制矩形框检测到的人脸数量并输出cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)cv2.putText(img,"Face#{}".format(i+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)cv2.imshow("Output",img)cv2.waitKey(0)之前opencv自带的人脸检测结果。1927年在比利时布鲁塞尔举行的第五次索尔维会议上,黑白照片中,21人被查出。1924年,林徽因等人与访华的泰戈尔合影,共7人,重要的泰戈尔未被发现。1927年在比利时布鲁塞尔召开的第五届索尔维会议,彩色照片,24人被检测到。不老男神,帅气的刘德华。使用训练好的模型shape_predictor_68_face_landmarks.dat在检测人脸的同时检测出人脸的68个关键点,再看刘德华。importdlibimportnumpyasnpimportcv2importimutilsfromimutilsimportface_utils#使用Dlib的正面人脸检测器frontal_face_detectordetector=dlib.get_frontal_face_detector()#使用训练好的模型shape_predictor_68_face_landmarks.dat检测人脸同时检测人脸68个关键点predictor=dlib.shape_predictor(r'C:\Python\Pycharm\docxprocess\face_detector\shape_predictor_68_face_landmarks.dat')#图片所在路径imgname=r'C:\Python\Pycharm\docxprocess\picture\other\renwu\juhui1.jpg'#21imgname=r'C:\Python\Pycharm\docxprocess\picture\other\renwu\juhui2.png'#6#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\angry.png'imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\ldh.png'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\happy.png'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\ldh\shigu.jpeg'#imgname=r'C:\Python\Pycharm\docxprocess\picture\other\renwu\juhui4.png'#24#读取图像并转换灰度img=cv2.imread(imgname)img_gray=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)#人脸检测,获取人脸数据faces=detector(img_gray,1)#rectangles[[(941,254)(977,290)],[(361,210)(397,246)],[(717,138)(753,174)],[(801,214)(837,250)],#[(573,138)(609,174)],[(45,210)(81,246)],[(585,202)(621,238)],[(189,254)(225,290)],#[(245,214)(281,250)],[(689,210)(725,246)],[(419,247)(463,290)],[(553,242)(589,278)],#[(901,218)(937,254)],[(77,246)(113),282)],[(141,222)(177,258)],[(741,242)(777,278)],#[(485,202)(521,238)],[(161,110))(197,146)],[(297,166)(333,202)],[(905,138)(941,174)],#[(301,246)(337,282)],[(865,106)(901,142)],[(389,146)(425,182)],[(241,138)(277,174)]]iflen(faces)<1:print("无脸detected")else:print("人脸总数为",len(faces))for(i,rect)inenumerate(faces):#返回人脸框左上角坐标和人脸框的大小therectangularframe(x,y,w,h)=face_utils.rect_to_bb(rect)#在图片上画一个矩形并输出检测到的人脸数cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)cv2.putText(img,"Face#{}".format(i+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)#标记人脸68landmarkpointsinshape=predictor(img_gray,rect)##shape转换为68个坐标点的矩阵shape=face_utils.shape_to_np(shape)#[[245149]#[245152]#...#[246159]]#[[364225]#[365228]#...#[366236]]#在枚举中为j,(x,y)输出源地图上的地标点(形状):cv2.circle(img,(x,y),2,(0,0,255),-1)cv2.putText(img,"{}".format(j+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)cv2.imshow("输出",img)cv2.waitKey(0)复制代码