import cv2
detector = cv2.CascadeClassifier('D:\opencv-4.2.0\data\haarcascades\haarcascade_frontalface_default.xml')
#打开笔记本摄像头
video_capture = cv2.VideoCapture(0)
while True:
#读视频帧
ret, img = video_capture.read()
#转为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#调用分类器进行检测
faces = detector.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30,30),
#flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
)
#画矩形框
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
#显示视频
cv2.imshow('Ying_Wu', img)
if cv2.waitKey(1) == 27:
break
#关闭摄像头设备
video_capture.release()
#关闭所有窗口
cv2.destroyAllWindows()
单个人脸
多个人脸