# 人脸检测
def detect_face(img):
faceClassifier=cv2.CascadeClassifier('D:\ProgramData\Anaconda3\pkgs\libopencv-3.4.1-h875b8b8_3\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
cvtImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
foundFaces=faceClassifier.detectMultiScale(cvtImage,scaleFactor=1.3,minNeighbors=9,minSize=(50,50))
for (x,y,w,h) in foundFaces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
return img
2.摄像头捕捉
import cv2
import numpy as np
import pickle
import matplotlib.pyplot as plt
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
# Our operations on the frame come here
result=detect_face(frame)
# Display the resulting frame
cv2.imshow('liuyunshengsir_opencv',result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()