一、环境:

Windows10


Python 3.6.4

OpenCV 3.4.1(安装方式为:pip install    opencv_python‑3.4.1‑cp36‑cp36m‑win32.whl  <>)

cascades(人脸识别haar特征级联分类器)文件夹内容下载地址:

https://github.com/opencv/opencv/tree/master/data/haarcascades
<https://github.com/opencv/opencv/tree/master/data/haarcascades>

注意:最终发现此下载方法文件出现问题

二、报错代码

import cv2

filename='face.jpg'

def detect(filename):


face_cascade = cv2.CascadeClassifier('./cascades/

                        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.namedWindow("vikings detected")
cv2.imshow("/vikings detected",img)
cv2.waitKey(0)


detect(filename)




运行代码出现错误代码:cv2.error: OpenCV(3.4.1)
D:\Build\OpenCV\opencv-3.4.1\modules\objdetect\src\casc


adedetect.cpp:1698: error: (-215) !empty() in function
cv::CascadeClassifier::detectMultiScale

措施:

1、按照网上查询的资料,大部分的问题是设置分类器xxxx.xml绝对路径,可依据各种方法设置在运行代码时,同样出现以上错误代码;

2、依据另一份网络参考在

face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')后追加,

face_cascade.load(
'D:/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml'
)方法,依然无用;

3、另一种方法为:重新卸载Python及OpenCV,重装anaconda及OpenCV,尝试后依然无效;

最终方案:

(一)

1、下载opencv-3.4.1-vc14_vc15.exe
<https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.4.1/opencv-3.4.1-vc14_vc15.exe/download>
,将文件放入C:\Users\John\AppData\Local\Programs\Python\Python36-32\Lib\site-packages文件目录,点击执行文件,生成OpenCV文件夹,内含文件夹‘\sources\data\haarcascades’内包含所有分类器文件xxxx.xml;


2、依据错误代码提示,在本地建立错误代码指出的指定路径:D:\Build\OpenCV\opencv-3.4.1\modules\objdetect\src\cascadedetect,将文件夹路径C:\Users\John\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\opencv\sources\data\haarcascades中分类器文件复制粘贴到本地建立的指定路径中;

3、修改源代码路径:

修改代码如下:

import cv2
import numpy as np


filename='face.jpg'
path='D:\Build\OpenCV\opencv-3.4.1\modules\objdetect\src\cascadedetect'


def detect(filename):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load(path+'\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.namedWindow("vikings detected")
cv2.imshow("vikings detected",img)
cv2.waitKey(0)



detect(filename)




再次运行,代码运行成功,可进行人脸识别!!!!如图所示:





(二)绝对路径中出现转义字符\,在Python中,Windows环境反斜杠“\",会将原始路径发生转义,如"\n",如果在路径中出现"\new",将无法读取路径,解决办法,绝对路径前加“r”,如:

r'D:\python_work\face_detect\cascades\haarcascade_frontalface_default.xml'




总结:

出现代码错误的原因可能是需要建立OpenCV库调用的指定路径;

绝对路径中出现转义字符“\”,Python在调用路径时发生转义,无法得到完整路径,在绝对路径前加“r”(此方法为正确方法);




参考链接:

https://blog.csdn.net/serena9636/article/details/52530446
<https://blog.csdn.net/serena9636/article/details/52530446>

https://blog.csdn.net/m0_37908464/article/details/79433630
<https://blog.csdn.net/m0_37908464/article/details/79433630>