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 to 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 play around with.
How is facial detection done using OpenCV?
Let’s go over the code performing facial detection using OpenCV
Import all the necessary libraries 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 the 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 it to grayscale
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() does not work in Kaggle. Hence it is replaced with plt.show()
How to run facial detection in Kaggle?
Step 1: Sign in to Kaggle
www.kaggle.com
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 the “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 the “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
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.