import cv2 img = cv2.imread("D:/tmp/andreea-pop-4_9_-K6cYYM-unsplash.jpg") imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faceCascade = cv2.CascadeClassifier("D:/tmp/haarcascade_frontalface_default.xml") faces = faceCascade.detectMultiScale(imgGray, 1.1, 4) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) # 框出人脸 cv2.imshow("Result", img) cv2.waitKey(0)
效果