Glasses Detection - OpenCV, DLIB & Edge Detection

Siddharth Mandgi
5 min readSep 21, 2021

Can AI detect if you wear Glasses? Let's find out...

https://unsplash.com/@lunarts

print(‘Hello World’)! This article is about detecting if the user is wearing a pair of glasses/shades using OpenCV and Dlib. OpenCV is a popular library/module in python for Image processing and Computer Vision. Dlib is another popular library in C++ to carry out different Machine Learning applications. Apart from these libraries, here are the requirements for this project.

  1. Python 3 (preferably a version ≥ 3.5+)
  2. Numpy
  3. Matplotlib
  4. Statistics
  5. Pillow

1. Theoretical Approach

A pair of glasses is always connected by a bridge across the nose (let us call this ‘the frame bridge’) and this will be our main ROI for detection. If we can detect the presence of this bridge, we can effectively detect the presence of glasses. Hence our approach will be to detect a person’s facial landmarks using Dlib and then focus on the nose using these landmarks to detect the presence of the bridge using Edge Detection.

2. Coding

Enough talk! Let us begin coding….

  • Lets us import the import libraries required for this project
import numpy as np
import dlib
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import statistics
  • We also need to download ‘shape_predictor_68_face_landmarks.dat’. This is freely available on the internet and this has all the information about the different facial landmarks. There is a total of 68 such landmark points on a person's face.
Universal Facial Landmarks

We are interested in points 28, 29, 30, and 31. Each point will give us an x and y coordinate. For the sake of clear visualization, we will also consider 33, 34, and 35.

  • Let us import the Dlib face detection model trained on the above landmarks
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

Let us take an image let me take my photo.

  • Let us get all 68 facial landmarks.
img = dlib.load_rgb_image(path)
plt.imshow(img)

rect = detector(img)[0]
sp = predictor(img, rect)
landmarks = np.array([[p.x, p.y] for p in sp.parts()])

This piece of code will help us load in the image, get the rectangular bounding box and generate facial points for each feature. The landmark variable will generate x, y coordinates.

  • Now we want to get my *coughs* the subject’s nose.
nose_bridge_x = []
nose_bridge_y = []
for i in [28,29,30,31,33,34,35]:
nose_bridge_x.append(landmarks[i][0])
nose_bridge_y.append(landmarks[i][1])


### x_min and x_max
x_min = min(nose_bridge_x)
x_max = max(nose_bridge_x)
### ymin (from top eyebrow coordinate), ymax
y_min = landmarks[20][1]
y_max = landmarks[31][1]
img2 = Image.open(path)
img2 = img2.crop((x_min,y_min,x_max,y_max))
plt.imshow(img2)

Focusing on the coordinates mentioned above we get this image.

The nose section and the frame bridge

We will now use the famous Canny Filter used for edge detection to get the edges (contours). Note that for accurate detection we will blur the image first using Gaussian Blur.

img_blur = cv2.GaussianBlur(np.array(img2),(3,3), sigmaX=0, sigmaY=0)edges = cv2.Canny(image =img_blur, threshold1=100, threshold2=200)
plt.imshow(edges, cmap =plt.get_cmap('gray'))
The edges of the above image

Notice, we can see the frame as a strip of white pixels (around y - 50 to 100). We will detect these pixels using a single column vector along the center of the nose.

#center strip
edges_center = edges.T[(int(len(edges.T)/2))]
  • Taking the transpose of the image to get column vectors and taking a vector along the center of the nose.
A strip of 0s between 255s
if 255 in edges_center:
print(“Glasses are present”)
else:
print(“Glasses are absent”)

As you see above we have a strip of white pixels (denoting the frame bridge) between black (denoting the nose). Confirming the presence of glasses.

3. Another Example

Let's take an example. Here is Rihanna, Let’s see if we can detect if she is wearing glasses.

  • The Input Image
  • The Crop
  • The Edges and Detection

Hence we can affirm that Rihanna is indeed wearing glasses.

4. Full Code

Here is the GitHub repo for the project, where I have also put everything in one useable function - glasses_detector. I have also added a Real-time Detection functionality.

5. Results

Here are some more results, 1, 0 - Glasses present /absent

--

--