In this tutorial, we will write a script that uses OpenCV and Python to extract faces from an input image. We will be using the pre-trained model to detect faces named Haar Cascade on Github.
- Python 3 installed on your system
- Numpy Library
- OpenCV Library
- First, we will load the required libraries into the python file ( NumPy, OpenCV, etc.).
- Now, create a variable to store the path of the image using the sys.argv[] function.
- Read the image file using the cv2.imread() function.
- Convert the image to black and white using the cv2.cvtColor() function as the Haar cascade only works on the gray images.
- Load the Haar cascade from the cv2 library and store it in a variable.
- Now, detect faces from the image using the detectMultiScale() function from the cv2 library.
- Then, we will use the cv2.rectangle() function to create a rectangle around the detected face.
- Now, save the image file using the cv2.imwrite() function with the name of your choice.
- scaleFactor: This variable is used to reduce the image size at each image scale to improve the detection of the faces.
- minNeighbors: This variable is used to specify how many faces in a rectangle can reside in it. Too high value can Ignore True Positives. So we will be using 3(value) to reduce all false positives.
- minSize: This variable is used to define the minimum possible face size (in pixels). Smaller values will be ignored and all values higher than this will be considered.