1.导入cv库
import cv2
2.摄像头的捕捉
cap = cv2.VideoCapture(0)
3.创建窗口
cv2.namedWindow('v')
4.加载人脸数据
face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_data.detectMultiScale(gray, scaleFactor=1.15, minNeighbors=5, minSize=(5, 5))
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + w), (0, 255, 0), 1)
cv2.imshow('F', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()