katyusha 2019-12-06
第一种:
# -*- coding:utf-8 -*- import cv2 as cv import numpy as np src = cv.imread(‘test1.jpg‘) path = r‘D:\face‘ def face_detect_demo(): gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY) face_detector = cv.CascadeClassifier(‘haarcascade_frontalface_default.xml‘) face_detector.load(path + ‘\haarcascade_frontalface_default.xml‘) faces = face_detector.detectMultiScale(gray,1.3,5) for x,y,w,h in faces: cv.rectangle(src,(x,y),(x+w,y+h),(0,0,255),2) cv.imshow("result",src) print("--------------python face detect-------------") cv.namedWindow("input image",0) cv.namedWindow("result",0) cv.imshow("input image",src) face_detect_demo() cv.waitKey(0) cv.destroyAllWindows()
效果:
第二种:
# -*- coding:utf-8 -*- import cv2 as cv import numpy as np src = cv.imread(‘test1.jpg‘) path = r‘D:\face‘ def face_detect_demo(): gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY) face_detector = cv.CascadeClassifier(‘haarcascade_frontalface_default.xml‘) face_detector.load(path + ‘\haarcascade_frontalface_default.xml‘) faces = face_detector.detectMultiScale(gray,1.3,5) for x,y,w,h in faces: cv.rectangle(src,(x,y),(x+w,y+h),(0,0,255),2) cv.imshow("result",src) print("--------------python face detect-------------") cv.namedWindow("input image",0) cv.namedWindow("result",0) cv.imshow("input image",src) face_detect_demo() cv.waitKey(0) cv.destroyAllWindows()
效果: