1 import cv2 as cv 2 def face_detect_demo(): 3 # 将图片转化为灰度图片 4 gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) 5 # 加载特征数据 6 face_detector = cv.CascadeClassifier('D:/Opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml') 7 faces = face_detector.detectMultiScale(gray) 8 for x,y,w,h in faces: 9 cv.rectangle(img,(x,y),(x+w,y+h),color=(0,255,0),thickness=2) 10 cv.imshow('result',img) 11 # 加载图片 12 img = cv.imread('C:/Users/87823/Desktop/Aaron_Ecke.jpg') 13 face_detect_demo() 14 cv.waitKey(0) 15 cv.destroyAllWindows()