Face Detection Haarcascade

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

OpenCV Face Detector HaarCascade

Python Face Detectors Algorithm

Learning Outcomes:

• Familiarize the Anaconda Environment or Google Collaboratory

• Articulate the function used in building the model

• Test Haarcascade Face Detector Algorithm

What is Anaconda Navigator

• It is jupyter notebook environment

✓ With installation

✓ write phyton directly inside browser

✓ create/upload, share notebook

DR. ANNA LIZA RAMOS & JHENO S, CERBITO Page | 1


Author
OpenCV Face Detector HaarCascade

Python Face Detectors Algorithm

Step 1. Open Anaconda Navigator then choose Jupyter Notebook

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.

DR. ANNA LIZA RAMOS & JHENO S, CERBITO Page | 2


Author
OpenCV Face Detector HaarCascade

Python Face Detectors Algorithm

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.

DR. ANNA LIZA RAMOS & JHENO S, CERBITO Page | 3


Author
OpenCV Face Detector HaarCascade

Python Face Detectors Algorithm

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.

This function's input such

• (image, scaleFactor, minNeighbors); a smaller scaleFactor and a lower


value for minNeighbors will often detect more faces but raising these
values will often produce better matches. Change these values to match
the image you're working with.

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.

DR. ANNA LIZA RAMOS & JHENO S, CERBITO Page | 4


Author
OpenCV Face Detector HaarCascade

Python Face Detectors Algorithm

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.

DR. ANNA LIZA RAMOS & JHENO S, CERBITO Page | 5


Author

You might also like