35. Face detection and recognition of OpenCV - Example 2 (face recognition training local data)
preface
face detection refers to the process of locating faces in images. Face recognition is to further judge people's identity on the basis of face detection. This example is a summary of the previous codes related to face detection and recognition.
1, Collect face samples
put the face image to be recognized in the face folder:
photos placed in each folder are as follows:
import os import cv2 import imghdr import numpy as np from imutils import * # Face detection and saving def facedetect(image, output): # Get file name name = os.path.basename(image) # Read in picture image = cv2.imread(image) # Change to grayscale image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Cascade classifier, face detection detector = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=3, minSize=(20, 20), flags=cv2.CASCADE_SCALE_IMAGE) # Loop each face for (x,y,w,h) in rects: # The face is intercepted and transformed into a fixed size of 200 * 200 f = cv2.resize(image[y:y+h, x:x+w], (200,200)) # Write to specified path cv2.imwrite(os.path.join(output, name), f) # Detect and intercept face def predict_face(path, output): # If the folder does not exist, create a folder if not os.path.exists(output): os.makedirs(output) # Cycle the pictures under each character's folder for files in os.listdir(path): # Detect whether it is a folder if os.path.isdir(os.path.join(path, files)): # Define the output path of the intercepted face output2 = os.path.join(output, files) # If the folder does not exist, create a folder if not os.path.exists(output2): os.makedirs(output2) # Full path to people folder files = os.path.join(path, files) # Cycle each photo of each person for file in os.listdir(files): # Photo full path file = os.path.join(files, file) # Detect face and save facedetect(file, output2) # Sample path and acquisition path predict_face('faces', 'predict_faces')
get predict after running the program_ Faces folder, which contains the detection pictures of predicted faces. After opening, you need to delete the pictures with recognition errors (the red box is the recognition error image):
2, Generate Label
the program will be based on predict_ According to the number of people in the faces subfolder, labels starting from 0 are automatically generated.
# Generate label file def get_label(path): fh = open("label.txt", 'w') # Represent face label label = 0 for root, dirs, files in os.walk(path): # Cycle through each folder for subdir in dirs: # Folder full path subdir_path = os.path.join(root,subdir) # Cycle through each photo below each people folder for file in os.listdir(subdir_path): # Photo full path filepath = os.path.join(subdir_path, file) # Determine whether the file type is a picture type imgType = imghdr.what(filepath) if imgType == 'jpeg' or imgType == 'png': # Save picture path fh.write(filepath); fh.write(";") # label fh.write(str(label)) fh.write("\n") # Everyone's label is different. Count from 0 label = label + 1 fh.close() # Path to collect face Tags get_label('predict_faces')
generated label Txt will generate corresponding labels according to the classified paths in the folder, as shown below:
3, Train your own data model
when calling the face recognizer, the program will read in the known image and the corresponding label corresponding to the label file for training, and save the training file at the end.
# Save picture data images = [] # Save label labels = [] # Open file fh = open("label.txt") # Loop each line for line in fh: # To; Split string arr = line.split(";") # Part 0 is the picture path to read the file img = cv2.imread(arr[0],0) # Save picture data images.append(img) # Save the corresponding label data labels.append(int(arr[1])) # model = cv2.face.EigenFaceRecognizer_create() # model = cv2.face.FisherFaceRecognizer_create() model = cv2.face.LBPHFaceRecognizer_create() # Training model model.train(np.array(images), np.array(labels)) # Save model model.save("predict_face_NBAstar.xml")
generated label file and saved model:
4, Face recognition (picture file)
prepare unknown face images to be predicted in advance and put them into the test folder.
after detecting the face, pass the model Predict () method.
# Define character names name= ['KobeBryant','LeBronJames','TracyMcGrady'] # Load the trained model model.read('predict_face_NBAstar.xml') i=0 # Read in the test picture to do the test for file in os.listdir('test'): file = os.path.join('test', file) # Determine file type imgType = imghdr.what(file) if imgType == 'jpeg' or imgType == 'png'or 'jpg': # Read in picture image = cv2.imread(file) # Change to grayscale gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # cascade classifier detector = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3, minSize=(20, 20), flags=cv2.CASCADE_SCALE_IMAGE) # Loop each face for (x,y,w,h) in rects: # Draw a rectangular box cv2.rectangle(image, (x,y), (x+w,y+h), (255,255,255), 2) # Face recognition face = cv2.resize(gray[y:y+h,x:x+w], (200,200)) # Predict people params = model.predict(face) # Write the character's name cv2.putText(image,name[params[0]],(x,y-5),cv2.FONT_HERSHEY_PLAIN,1,(255,255,255),1) i += 1 cv2.imshow("test"+str(i),image) cv2.waitKey(0) cv2.destroyAllWindows()
the recognition results are as follows:
5, Face recognition using video
the model generated by the picture file can be recognized by the camera.
import cv2 capture = cv2.VideoCapture(0) frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH) frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT) fps = capture.get(cv2.CAP_PROP_FPS) if capture.isOpened() is False: print('CAMERA ERROR !') exit(0) model = cv2.face.LBPHFFaceRecognizer_create() name= ['Kobe Bryant','LeBron James','Tracy McGrady'] model.read('predict_face_NBAstar.xml') detector = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") while capture.isOpened(): ret, frame = capture.read() if ret is True: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3, minSize=(20, 20), flags=cv2.CASCADE_SCALE_IMAGE) for (x,y,w,h) in rects: cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255), 2) face = cv2.resize(gray[y:y+h,x:x+w], (200,200)) params = model.predict(face) cv2.putText(frame,name[params[0]],(x,y-5),cv2.FONT_HERSHEY_PLAIN,1,(255,255,255),1) cv2.imshow('FACE', frame) k = cv2.waitKey(100) if k == ord('q'): break else: break capture.release() cv2.destroyAllWindows()
the recognition results are as follows:
6, Opencv Python resource download
Opencv Python test pictures, Chinese official documents, opencv-4.5.4 source code
summary
the above content introduces the code example summary of OpenCV Python face detection and recognition. Articles on python, data science and artificial intelligence will be published from time to time. Please pay more attention and connect three times with one button (● '◡' ●).