CV2 Import Import matplotlib.pyplot AS PLT % matplotlib inline # extracted pre-trained face detection model, download advance good model face_cascade = cv2.CascadeClassifier ( ' haarcascades / haarcascade_frontalface_alt.xml ' ) # loading color (channel order BGR) image IMG = cv2.imread ( ' images / 9f510fb30f2442a70a9add3dd143ad4bd0130295.jpg ' ) # BGR the image gradation processing gray = cv2.cvtColor (IMG, cv2.COLOR_BGR2GRAY) # find a face in an image faces = face_cascade.detectMultiScale (gray) # the number of the printed image of the detected face print (' Number The of Faces Detected: ' , len (Faces)) Print (type (Faces)) # obtain each of the detected face recognition block for (X, Y, W, H) in Faces: # the facial image map out the identification frame cv2.rectangle (IMG, (X, Y), (X + W, Y + H), ( 255 , 0 , 0 ), 2 ) # BGR images into the RGB image to print CV_RGB = CV2 .cvtColor (IMG, cv2.COLOR_BGR2RGB) # display image frame containing the recognition plt.imshow (CV_RGB) plt.show ()
OpenCV the Haar feature-based cascade classifiers to detect faces in the image. OpenCV provides a number of pre-trained face detection model, they are saved in the XML file github