Real-time Glass Detection on face using python - Easiest method

Detecting glass on human faces in real-time using a python project

Introduction

Can you tell me how many people today have not heard of Artificial Intelligence, Machine Learning, etc.? If you are a tech guy then you must have heard about it.

From an auto-drive car to detect different objects machines or computers are doing well. In these fields, programming plays a major role, and Python in particular is leading the way here.

Recently, I have developed a machine learning project that will tell us whether a human is wearing glass on his/her face or not through the WebCam. Today, I will share with you how I created this glass detection application in the most easiest way using Python.

👉Visit AlsoCreate a Face Recognition Project in Python - with Money Heist Team

Requirements

Since you are going to deal with a little complicated task here, you need to meet some basic requirements before proceeding.

Let's perform the installation process one after one. Use pip3 instead of pip if you are running Linux Machine.

Install cmake

☛pip install cmake

Install dlib

Dlib is a cross-platform software library; written in C++. You've to install cmake before installing dlib. It may take around 15-20 minutes to install. So, don't be surprised if it takes extra time.

☛pip install dlib

Install OpenCV

OpenCV-Python is a python library that is designed to solve computer vision problems. It's a python wrapper for the original OpenCV, implemented using C++.

Python is slower as compared to C/C++. But it can be extended with C++ using Python wrappers, which will be used as Python modules. It will give us two advantages. First, the code will be as fast as the C++ code, and second, it will be easy to code in Python than in C++.

☛pip install opencv-python

Install NumPy

NumPy is a Python library that provides a powerful N-dimensional array object, several mathematical functions, and much more. It's used widely in machine learning, deep learning, image processing, etc. for working with multi-dimensional arrays. I've used it in this project for performing a specific task.

☛pip install numpy

How does the Glass Detection Program Work?

Let me tell you an interesting hint. Every pair of glasses have a bridge through which they are connected. We just need to build our program such that, it will capture a face rectangle and then set the focus on the middle regions of two eye-brows

Next, it will smooth that region using the Gaussian Blur and filter it through the canny filter(for edge detection) to detect the edges of the glass bridge in that selected region(between the two eyebrows). We must use these two features(Gaussian Blur, and Canny Filter) in our program to get the desired result.

After performing the canny filter the frame will show that selected place in a black-and-white format. The white color represents any edges(object) on there and it represents by a number: '255'. 

If the program can find any '255' there(between the two eyebrows), that means edges are existing. In this case, the program will show the "glass is present" message and its' opposite message for the else case.

Import the modules

Create a separate folder for this project and declare a python file there with this name, "GlassDetection.py". Now start writing your code by importing these modules.


import dlib
import numpy as np
from threading import Thread
import cv2
import time

Declare dlib face detector


# a cv2 font type
font_1 = cv2.FONT_HERSHEY_SIMPLEX
# Declare dlib frontal face detector
detector = dlib.get_frontal_face_detector()

Create a class for capturing the video frames

It will be capturing video frames through the WebCam in a different thread. The reason behind announcing a different thread here for capturing video is so that other tasks in the program can be performed more efficiently.


# This class handles the video stream
# captured from the WebCam
class VideoStream:
def __init__(self, stream):
self.video = cv2.VideoCapture(stream)
# Setting the FPS for the video stream
self.video.set(cv2.CAP_PROP_FPS, 60)

if self.video.isOpened() is False:
print("Can't accessing the webcam stream.")
exit(0)

self.grabbed , self.frame = self.video.read()
self.stopped = True
# Creating a thread
self.thread = Thread(target=self.update)
self.thread.daemon = True

def start(self):
self.stopped = False
self.thread.start()

def update(self):
while True :
if self.stopped is True :
break
self.grabbed , self.frame = self.video.read()
self.video.release()

def read(self):
return self.frame

def stop(self):
self.stopped = True

Start capturing video frames through the WebCam


# Capturing video through the WebCam. 0 represents the
# default camera. You need to specify another number for
# any external camera
video_stream = VideoStream(stream=0)
video_stream.start()

Create an infinite while loop

Let's declare an infinite loop so that the program captures video frames via WebCam or any external cameras(In this case, you need to specify it to the program) until the user interrupts.

I have declared a single comment line before each line of code. This will help you to grab the objectives of this part of the code more precisely.  

An Important Message - You need to press the 'q' button to break from this loop.


while True:
if video_stream.stopped is True:
break
else:
# Reading the video frame
frame = video_stream.read()
# Convert the frame color-space to grayscale
# There are more than 150 color-space to use
# BGR = Blue, Green, Red
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

rects = detector(gray, 1)
# Get the coordinates of detected face
for i, face_rect in enumerate(rects):
left = face_rect.left()
top = face_rect.top()
width = face_rect.right() - left
height = face_rect.bottom() - top

# Draw a rectangle around the detected face
# Syntax: cv2.rectangle(image, start_point, end_point,
# color, thickness)
cv2.rectangle(frame, (left, top), (left+width, top+height),\
(0,255,0), 2)
# Draw a face name with the number.
# Syntax: cv2.putText(image, text, origin, font,
# fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
# For better look, lineType = cv.LINE_AA is recommended.
cv2.putText(frame, f"Face {i+1}", (left - 10, top - 10), \
font_1, 0.7, (0, 255, 0), 2, cv2.LINE_AA)

'''Cropping an another frame from the detected face rectangle'''
frame_crop = frame[top + 10:top+height-100, left + 30: \
left+width - 20]

# Show the cropped frame
cv2.imshow("Cropped Frame", frame_crop)

# Smoothing the cropped frame
img_blur = cv2.GaussianBlur(np.array(frame_crop),(5,5), \
sigmaX=1.7, sigmaY=1.7)
# Filterting the cropped frame through the canny filter
edges = cv2.Canny(image =img_blur, threshold1=100, threshold2=200)
# Show the Canny Sample of the frame: 'frame_cropped'
cv2.imshow("Canny Filter", edges)

# Center Strip
edges_center = edges.T[(int(len(edges.T)/2))]
# 255 represents white edges. If any white edges are detected
# in the desired place, it will show 'Glass is Present' message
if 255 in edges_center:
cv2.rectangle(frame, (left, top+height), (left+width, \
top+height+40), (0,255,0), cv2.FILLED)
cv2.putText(frame, "Glass is Present", (left+10, \
top+height+20), font_1, 0.65, (255, 255, 255), 2, cv2.LINE_AA)
else:
cv2.rectangle(frame, (left, top+height), (left+width, \
top+height+40), (0,255,0), cv2.FILLED)
cv2.putText(frame, "No Glass", (left+10, top+height+20), \
font_1, 0.65, (0, 0, 255), 2, cv2.LINE_AA)

# delay for processing a frame
delay = 0.04
time.sleep(delay)

# show result
cv2.imshow("Result", frame)

key = cv2.waitKey(1)
# Press 'q' for stop the executing of the program
if key == ord('q'):
break

Stop capturing video frames and close all windows

You are here which means you have pressed the 'q' button. But what next? Stop capturing video frames and destroy all windows generated by the program.


# Stop capturing video frames
video_stream.stop()
# closing all windows
cv2.destroyAllWindows()

Full Code

Here is the full code for your convenience.


'''Detect whether a person is wearing a glass or not,
using this python program. It's the easiest
approach to perform this task.

Developed By: Subhankar Rakshit
Date: 2022-04-12
'''

import dlib
import numpy as np
from threading import Thread
import cv2
import time

# a cv2 font type
font_1 = cv2.FONT_HERSHEY_SIMPLEX
# Declare dlib frontal face detector
detector = dlib.get_frontal_face_detector()

# This class handles the video stream
# captured from the WebCam
class VideoStream:
def __init__(self, stream):
self.video = cv2.VideoCapture(stream)
# Setting the FPS for the video stream
self.video.set(cv2.CAP_PROP_FPS, 60)

if self.video.isOpened() is False:
print("Can't accessing the webcam stream.")
exit(0)

self.grabbed , self.frame = self.video.read()
self.stopped = True
# Creating a thread
self.thread = Thread(target=self.update)
self.thread.daemon = True

def start(self):
self.stopped = False
self.thread.start()

def update(self):
while True :
if self.stopped is True :
break
self.grabbed , self.frame = self.video.read()
self.video.release()

def read(self):
return self.frame

def stop(self):
self.stopped = True

# Capturing video through the WebCam. 0 represents the
# default camera. You need to specify another number for
# any external camera
video_stream = VideoStream(stream=0)
video_stream.start()

while True:
if video_stream.stopped is True:
break
else:
# Reading the video frame
frame = video_stream.read()
# Convert the frame color-space to grayscale
# There are more than 150 color-space to use
# BGR = Blue, Green, Red
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

rects = detector(gray, 1)
# Get the coordinates of detected face
for i, face_rect in enumerate(rects):
left = face_rect.left()
top = face_rect.top()
width = face_rect.right() - left
height = face_rect.bottom() - top

# Draw a rectangle around the detected face
# Syntax: cv2.rectangle(image, start_point, end_point,
# color, thickness)
cv2.rectangle(frame, (left, top), (left+width, top+height),\
(0,255,0), 2)
# Draw a face name with the number.
# Syntax: cv2.putText(image, text, origin, font,
# fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
# For better look, lineType = cv.LINE_AA is recommended.
cv2.putText(frame, f"Face {i+1}", (left - 10, top - 10), \
font_1, 0.7, (0, 255, 0), 2, cv2.LINE_AA)

'''Cropping an another frame from the detected face rectangle'''
frame_crop = frame[top + 10:top+height-100, left + 30: \
left+width - 20]

# Show the cropped frame
cv2.imshow("Cropped Frame", frame_crop)

# Smoothing the cropped frame
img_blur = cv2.GaussianBlur(np.array(frame_crop),(5,5), \
sigmaX=1.7, sigmaY=1.7)
# Filterting the cropped frame through the canny filter
edges = cv2.Canny(image =img_blur, threshold1=100, threshold2=200)
# Show the Canny Sample of the frame: 'frame_cropped'
cv2.imshow("Canny Filter", edges)

# Center Strip
edges_center = edges.T[(int(len(edges.T)/2))]
# 255 represents white edges. If any white edges are detected
# in the desired place, it will show 'Glass is Present' message
if 255 in edges_center:
cv2.rectangle(frame, (left, top+height), (left+width, \
top+height+40), (0,255,0), cv2.FILLED)
cv2.putText(frame, "Glass is Present", (left+10, \
top+height+20), font_1, 0.65, (255, 255, 255), 2, cv2.LINE_AA)
else:
cv2.rectangle(frame, (left, top+height), (left+width, \
top+height+40), (0,255,0), cv2.FILLED)
cv2.putText(frame, "No Glass", (left+10, top+height+20), \
font_1, 0.65, (0, 0, 255), 2, cv2.LINE_AA)

# delay for processing a frame
delay = 0.04
time.sleep(delay)

# show result
cv2.imshow("Result", frame)

key = cv2.waitKey(1)
# Press 'q' for stop the executing of the program
if key == ord('q'):
break
# Stop capturing video frames
video_stream.stop()
# closing all windows
cv2.destroyAllWindows()

Summary

Today you learned about how to detect a glass on human faces through the live WebCam using a python program. We build this application with the help of python OpenCV, dlib, and NumPy libraries. The program uses a different thread to capture the live video stream through the WebCam.

One piece of advice from me to you, please maintain a good quality of brightness before the face so that the program can recognize the actual place and give the answer correctly.

One more important message, Welcome you to all to suggest me for improving the code better.

To get more lovely projects like this, visit the separate page created only for Python Projects. Some examples are given below.

👉Convert any Images to a Pencil Sketch using Python

👉Convert an Image to a Cartoon using OpenCV Python

👉Face Recognition based Attendance System using Python

I hope you enjoyed this tutorial. For any queries, leave your message below. You'll get an immediate response.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post