解决OpenCV module cv2 has no attribute data的报错

import cv2

img = cv2.imread('image1.jpg',1)
face_engine = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
faces = face_engine.detectMultiScale(img,scaleFactor=1.3,minNeighbors=5)
for (x,y,w,h) in faces:
    img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

代码如上,报错信息显示第四行module cv2 has no attribute data

解决方法:

()内只保留文件名

Cascade级联分类器文件使用绝对路径

改后示例

face_engine = cv2.CascadeClassifier('/home/pi/desktop/opencv/data/haarcascades/haarcascade_frontalface_default.xml')

 

你可能感兴趣的:(解决OpenCV module cv2 has no attribute data的报错)