In this lab, we will continue learning about Machine Learning through Computer Vision. Practice implementing the API
Create a new folder on your Desktop or projects folder called Facial_Recognizer or similar.
Create a folder called cascades. This is where the cascade algorithms will be stored. We will use the front face default, but you can actually get many more to detect smiles, eyes, etc., here.
Download the frontalface_default.xml file into that directory. Made sure to cd cascades. The -O flag saves the file with the last part of the link as a filename.
$ curl https://raw.githubusercontent.com/CodeNextAdmin/TE-opencv-faces/main/cascades/haarcascade_frontalface_default.xml -O
Make a new file: face_detector.py and add this code.
Run the code and test. You should see a marquee or frame around each face that comes into view.
Adjust the parameters, colors, as needed.
Make a new file in the same directory called face_recognizer.py.
Make a new directory, faces_dataset in the main project directory.
Copy the code from the face_detector.py into your new file.
Add the code to take a user ID as an input and store it as a face_id.
Add the counter and the code to save the image to the directory, based on the user ID in the while loop:
cv2.imwrite("faces_dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
Add the new condition that breaks out of the while loop after 30 images have been captured.
Gather some family members or other nearby humans with faces.
Run the script and enter 0 (zero) for the first user, and tell them to stand close to the camera and try slightly different expressions. This process can take a few seconds.
Run it again with at least one other face. You can also use face images from the internet and store them in the faces_dataset folder, if you want to detect other individuals.
Look in that directory after running each model to verify images were saved. Notice that you captured only the ROI (region of interest) for each face.
Create a new file called face_training.py
Makes sure you have
Create a new directory inside the same project directory called trainer.
Add the code from the repo, including:
The function that adds labels to each face: getImagesAndLabels()
The function call that trains the recognizer
The function call that saves the .yaml file.
Run the file only once without errors.
Verify that there is data present in .yaml file. YAML files are written in a serialized language that is easy for humans to read, and is built on key/value pairs. Read more about YAML here.
Create a new file, alongside the others in the same directory called face_recognizer_final.py or similar.
Open the corresponding file in the repo and add the code from to your file.
Replace the names in the list [“Person1”, “Person2”...] with names of the individuals you trained your model on, in the correct index order.
Make sure your cascade path matches your directory structure.
Run the code and verify that you and the people around you are who they say they are!