Mask Prediction
[ Baseline ] Mask Prediction
The baseline with a naive approach of mask detection using Canny Edge Detection, Sklearn Models etc.
Getting Started with Mask Recognition Challenge
In this puzzle, we have detect mask type and bounding box of the mask from the image of human face.
In this notebook, we'll use the naive approach to prepare the baseline for this puzzle.
Naive Approach¶
- We will create bounding box that will detect the face and crop the image as per the co-ordinates of bounding box from image.
- Once we've the cropped image(It'll have the image of mask), we'll feed the data to train classifier which will classify the mask.
Setting up Environment¶
Downloading Dataset¶
So we will first need to download the python library by AIcrowd that will allow us to download the dataset by just inputting the API key.
%%capture
!pip install aicrowd-cli
%load_ext aicrowd.magic
Login to AIcrowd ㊗¶¶
%aicrowd login
Download Dataset¶¶
We will create a folder name data and download the files there.
!rm -rf data
!mkdir data
%aicrowd ds dl -c mask-prediction -o data
!unzip data/train.zip -d data/ > /dev/null
!unzip data/val.zip -d data/ > /dev/null
!unzip data/test.zip -d data/ > /dev/null
Importing Libraries¶
#Reading the file
import pandas as pd
import numpy as np
import os
# Image Reading & Preprocessing
from PIL import Image, ImageDraw
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Misc.
from tqdm.notebook import tqdm
from sklearn.ensemble import RandomForestClassifier
Diving in the dataset 🕵️♂️¶
train_images = 'data/train'
val_images = 'data/val'
test_images = 'data/test'
train_df = pd.read_csv("data/train.csv")
val_df = pd.read_csv("data/val.csv")
train_df.head()
train_df.loc[train_df.masktype == "cloth"][:2]
train_img = os.listdir(train_images)
train_img[:5]
From the above, we can see that ImageId is image_name without extensions(.jpg).
img = Image.open(os.path.join(train_images, '7a0l9.jpg'))
img
Image Preprocessing¶
In this section we are going to learn some opencv functions which can help us detecting the mask from the image!
# Converting the image to numpy array
np_img= np.array(img)
np_img.shape
# Converting the Image to RGB to Grayscale ( black/white )
gray= cv2.cvtColor(np_img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)
cv2.Canny
¶
Now cv2.Canny
is Canny Edge Detection which helps us to detect edges in the image. Let's try it our on the image
canny= cv2.Canny(gray, 120,250)
plt.imshow(canny)
So as you can see the function detected edges including the mask and some noise too, there are many ways to reduce that noise, however you can try changing the parameters in the function and see where that leads.
Countours¶
Contours are lines joining along the bounding of a intensity or color in an image. In the canny image or the original image, we see that the image has much different color as compared to the sky.
# Finding contours in the image
contours, hierarchy = cv2.findContours(canny,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Sorting the contours in ascending order
contours = sorted(contours, key=cv2.contourArea)
# Getting the bounding boxes of the biggest contours
x,y,w,h = list(cv2.boundingRect(contours[-1]))
x,y,w,h
# Showing the contour
draw_img = img.copy()
draw = ImageDraw.Draw(draw_img)
draw.rectangle([x,y,x+w,y+h ], outline ="red")
draw_img
So as you can see, fnding contours did a pretty great job in finfing the mask.
left,top,right, bottom = x,y,x+w,y+h
mask = img.crop((left,top,right, bottom))
mask