Professional Documents
Culture Documents
Face Detection Haarcascade
Face Detection Haarcascade
Face Detection Haarcascade
Learning Outcomes:
✓ With installation
Step 2. Create a new Python3 file and a new working directory. Please download
HaarCascade-FrontalFaceDefault and place the file in Python3 file.
Step 3. Then, download the sample image and place it in the img
directory.
Step 3. Open your Python file and import matplotlib, NumPy, and cv2 first. NumPy will be
used to vectorize an image, and matplotlib will be used to plot and display our sample
image. In addition, we use the cv2 library for computer vision and filtering techniques.
Step 4. The next step is to read the image from the directory. We need convert the BGR
to RGB color space after loading the image from the directory, and then show the
image with a 20x10 dimension using the matplotlib library as shown below.
Step 5. Convert the image from color to grayscale before using a face detector. Note :
Human face has a lot of non-color specific structure for our detector to learn
Step 6. When you run the line of code, the RGB color image will be transformed to
grayscale.
Step 7. After that, we utilize the fully trained face detector architecture, which is
located in the same directory of our Python3 file. This is to find faces in an image.
Step 8. The function detectMultiScale, aims to detect faces with size variation,
determines how many faces are detected.
Step 9. The classifier generates an array of detections, which are coordinates defining
the size of a bounding box around each face. It's worth noting that this method always
yields a square bounding box.
Step 10. Duplicate the original image in order to plot rectangle detections on top of it
Step 11. Loop through our detections and draw the boxes that correspond to them on top of our
original image. Next detection should be drawn as a blue rectangle on top of the original
image. Important note: the fourth argument (255,0,0) specifies the color of the rectangle, and the
final argument (here set to 5) specifies the width of the drawn rectangle.
Step 12. The haarCascade face classifier should be able to predict four faces in a
frontal angle.