Code result :

Find imported xml file
import cv2 print(cv2.__file__)
find cv2 Installation path , Found under this path /data file .

Some of them are downloaded by default xml file , Not all xml file . If necessary xml The file is not in it , You need to download it online by yourself , Then put it in this directory , Ready for call . Such as self installation ('haarcascade_mcs_nose.xml','haarcascade_mcs_mouth.xml')

Import package :
import cv2
  Import xml file , According to the needs of the task , Select the to be imported by yourself xml file
# Face detector face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml') # Eye detector eye_cascade =
cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml') # Mouth detector
mouth_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_mcs_mouth.xml') # Nose detector nose_cascade =
cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mcs_nose.xml')
  Setup window :
cv2.namedWindow('mytest', 0); cv2.resizeWindow('mytest', 1500, 1000)
  Turn on the camera , Face recognition :
# Get camera cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) # Turn on the camera cap.open(0) while
cap.isOpened(): # Get picture flag, frame = cap.read() # Face detection faces =
face_cascade.detectMultiScale(frame, 1.3, 2) img = frame for (x, y, w, h) in
faces: # According to face coordinates and length , Draw a rectangle with the width img = cv2.rectangle(img, (x, y), (x+w, y+h),(255, 0
,0), 2) # Determine face range , Search for other features on the face face_area = img[y:y+h, x:x+w] # Human eye detection eyes =
eye_cascade.detectMultiScale(face_area, 1.3, 2) for (ex, ey, ew, eh) in eyes:
cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (255, 0 ,0), 1) # Mouth detection
mouth = mouth_cascade.detectMultiScale(face_area, 1.5, 2) for (mx, my, mw, mh)
in mouth: cv2.rectangle(face_area, (mx, my), (mx + mw, my + mh), (0, 0, 255),
2) # Nose detection nose = nose_cascade.detectMultiScale(face_area, 1.2, 5) for (nx, ny,
nw, nh) in nose: cv2.rectangle(face_area, (nx, ny), (nx + nw, ny + nh), (255,
0, 255), 2) # Screen display cv2.imshow('mytest', img) # Set exit button key_pressed =
cv2.waitKey(100) print(' Stand alone window , Input key , Computer keys are ',key_pressed,' Press esc Key end ') if key_pressed
== 27: break # Turn off the camera cap.release() # Close image window cv2.destroyAllWindows()
  Complete code :
import cv2 # Face detector face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml') # Eye detector eye_cascade =
cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml') # Mouth detector
mouth_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_mcs_mouth.xml') # Nose detector nose_cascade =
cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mcs_nose.xml')
# Get camera cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) # Turn on the camera cap.open(0)
cv2.namedWindow('mytest', 0); cv2.resizeWindow('mytest', 1500, 1000) while
cap.isOpened(): # Get picture flag, frame = cap.read() # Face detection faces =
face_cascade.detectMultiScale(frame, 1.3, 2) img = frame for (x, y, w, h) in
faces: # According to face coordinates and length , Draw a rectangle with the width img = cv2.rectangle(img, (x, y), (x+w, y+h),(255, 0
,0), 2) # Determine face range , Search for other features on the face face_area = img[y:y+h, x:x+w] # Human eye detection eyes =
eye_cascade.detectMultiScale(face_area, 1.3, 2) for (ex, ey, ew, eh) in eyes:
cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (255, 0 ,0), 1) # Mouth detection
mouth = mouth_cascade.detectMultiScale(face_area, 1.5, 2) for (mx, my, mw, mh)
in mouth: cv2.rectangle(face_area, (mx, my), (mx + mw, my + mh), (0, 0, 255),
2) # Nose detection nose = nose_cascade.detectMultiScale(face_area, 1.2, 5) for (nx, ny,
nw, nh) in nose: cv2.rectangle(face_area, (nx, ny), (nx + nw, ny + nh), (255,
0, 255), 2) # Screen display cv2.imshow('mytest', img) # Set exit button key_pressed =
cv2.waitKey(100) print(' Stand alone window , Input key , Computer keys are ',key_pressed,' Press esc Key end ') if key_pressed
== 27: break # Turn off the camera cap.release() # Close image window cv2.destroyAllWindows()

Technology