Getting started with Kaggle using Facial Detection

Anu Ganesan
4 min readApr 2, 2021

Facial Detection is the technology to detect human faces in digital media. This article will guide you to get started with Kaggle using the openCV (Open Source computer Vision) library in python.

What is Kaggle?

Kaggle is often referred as the AirBnB for Data Scientists.

If you are interested to venture into Machine Learning and want to learn by trying out some of the readily available algorithms and libraries, then Kaggle is the right place to start.

It is a set-up free Jupyter Notebook environment with numerous datasets and machine learning codes to playaround.

How is facial detection done using OpenCV?

Let’s go over the code performing facial detection using OpenCV

Import all the necessary libaries needed for facial detection

import cv2
import
sys
import matplotlib.pyplot as plt

Setup the path of the image to be detected along with the cascade xml filepath which is part of openCV library

imagePath = "/kaggle/input/mygithubrepo/girl1.jpg"
cascPath = "/kaggle/input/mygithubrepo/haarcascade_frontalface_default.xml"

Initialize faceCascade variable with the openCV xml needed for frontal facial detection

faceCascade = cv2.CascadeClassifier(cascPath)

Read the image and convert to gray scale

image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Detect Faces with detectMultiScale from openCV library

faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.CASCADE_SCALE_IMAGE
)

Plot the images with face detected in rectangle shape

print ("Found {0} faces!".format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
#cv2.imshow("Faces found", image)

plt.imshow(image, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()

cv2.imshow() doesnot work in Kaggle. Hence it is replaced with plt.show()

How to run facial detection in Kaggle?

Step 1: Sign into Kaggle

If this is your first time signing up into kaggle, then update your bio similar to below summary

getting started with kaggle

Step 2: Click Code and then New Notebook

Kaggle Notebook opens up with the following pre-filled code

Step 3: Click “Add Data” to load datasets (in this case it will be the image)

:

Step 4: Click Upload to upload datasets from github which contains the image file

Step 5: Select the github icon (3'rd icon) and input the github url and click “Create” button

The input section will reflect your recent dataset addition as shown in the below screenshot

Step 6: Input the Facial Detection code into Kaggle by clicking the + button to add code

Step 7: Use the below github repo link to fill in the code section in the Kaggle Notebook

Step 8: Run the code

You can either run each code segment one at a time by clicking the run(triangle) button on the side of the code segment

You can also run the entire code by clicking “run all” button on the top of the code

Clicking the run button will execute the code to detect faces from the image file.

Make sure below 2 entries(Kaggle code section & input dataset name) matches for facial detection to work in kaggle

imagePath = "/kaggle/input/mygithubrepo/girl1.jpg"
cascPath = "/kaggle/input/mygithubrepo/haarcascade_frontalface_default.xml"

Start Kaggling and Begin Your Data Science Journey!!!

Follow us to learn more about machine learning and mlops solutions

LinkedIn

Predera

--

--