video face detection using HAAR'S CASCADE with python

face detection using HAAR'S CASCADE with python

In this tutorial we're gonna look into a face detection using "HAAR's CASCADE" algorithm in open cv. haar's cascade a is face detection classifier built-in  opencv for face and many more detection. Haar's cascade is originally developed from viola-jones algorithm. but it's not perfect it is prone to noise often, and this algorithm is best suited for frontal face detection, but for just simplicity we use it today.

Let's be clear about 2 things they are face Recognization and face detection. They both are 2 separate things. This haars cascade focus and used for face detection means in picture or a video it can detect the faces. Where face recognization is detecting a particular face of person.

The problem in computer vision has been bothered so long untill Development began on similar systems in the 1960s, here is wiki page on history of face detection.

The viola jones algorithm achived the face detection with fast mostly accurate. Before viola's algorithm the face detection is only possible through detecting edges in a photo and looking for facial features which takes lot of computation and processing power of computer.

The first contribution to the research was the introduction of the haar features shown figures(a,b,c,d,e). These features on the image makes it easy to find out the edges or the lines in the image, or to pick areas where there is a sudden change in the intensities of the pixels. Haar's cascade works in 2 stages i.e 

I) Face Detection     II) Traning and cascading. 

The Ist part is to look for facial features by loading an image in grayscale by doing this we can work with only 8-bit(0-255) info rather than RGB(255,255,255) 3D-array.

our face is literaly different in with respect to other shade in parts. For example our eye brows are darker than skin in face, Eye pupil is darker than cornea. 

Depending on the feature each one is looking for, these are broadly classified into three categories. The first set of two rectangle features are responsible for finding out the edges in a horizontal or in a vertical direction (as shown above). 
The second set of three rectangle features are responsible for finding out if there is a lighter region surrounded by darker regions on either side or vice-versa. The third set of four rectangle features are responsible for finding out change of pixel intensities across diagonals.
The viola-jones algorithm introduced another concept known as The Integral Image to perform the same operation.
more about from this link, An Integral Image is calculated from the Original Image in such a way that each pixel in this is the sum of all the pixels lying in its left and above in the Original Image. The calculation of a pixel in the Integral Image can be seen in the above GIF. The last pixel at the bottom right corner of the Integral Image will be the sum of all the pixels in the Original Image.
The 2nd stage is cascading and traing. The part involves the adaboost and cascading methods.more about from this link.

"using this method we can also detect many other features like face,eyes,fullbody,lowerbody,russian number_plates,etc.."here the same method modified for face-recognition in image.

To get started u need few things, and just few line of codes you get ur face detection. we  need the following things to get started:

1.python 3 recent would be nice!

2.opencv lib package installed using pip

3.laptop/desktop with webcam

step 1:    go to www.python.org and download latest version of python3  choose the install python along with pip and Option says add path variable to system. if ur already installed python 3 u can skip to step 2. if you prefer using any online or offline code editor its fine or we can use one provided with python IDLE default.

step 2:    open cmd prompt if ur in windows or terminal in linux, use cmd: "pip install opencv-python" in windows, cmd: "sudo pip install opencv-python" in linux.  we can install a different package for more option i.e contributed package in opencv i.e "pip install opencv-contrib-python".

after installed goto C:\Users\user_name\appdata\roaming\python\python39\site-packages\cv2\data or "/usr/local/lib/python3.9/dist-packages(in-linux)" folder to get the,"haarcascade_frontalface_default.xml" copy it in current working directory we are working now(the current working directory is where u save ur python code!).

Step 3:    let's start coding, lets open idle editor start coding, the code looks like the following one : 

code:<
import cv2 as cv
bgr_video = cv.VideoCapture(0)
#add haars cascade
haars_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')

#add resize func
'''
def ResizeVideo(frame,scale=0.5):
	width = int(frame.shape[1] * scale)
	height = int(frame.shape[0] * scale)
	dimensions = (width,height)
	res = cv.resize(frame,dimensions,interpolation=cv.INTER_LINEAR)
	return res
'''
#capture of video loop
while bgr_video.isOpened():
	ret,frap = bgr_video.read()
#	cv.imshow('normalvideo',frap)
#cvt the bgr to gray cause haars cascade only works in gray
	img_gray = cv.cvtColor(frap,cv.COLOR_BGR2GRAY)
	cv.imshow('gray',img_gray)
	#img_gray = ResizeVideo(img_gray,scale=0.75)
	face_rect = haars_cascade.detectMultiScale(img_gray,scaleFactor=1.1,minNeighbors=5)
	print(f'number of faces = {len(face_rect)}')	
	for (x,y,w,h) in face_rect :
		cv.rectangle(frap,(x,y),(x+w,y+h),(0,255,0),2)
		cv.imshow('detected_face',frap)
		face = face_rect
		cv.imwrite('intruderface.jpg',face)
	if cv.waitKey(1) & 0xFF == ord('q'):
		break
	'''
#resize cause large for image
bgr = cv.resize(bgr,None,fx=0.25,fy=0.25, interpolation = cv.INTER_LINEAR)
'''
bgr_video.release()
cv.waitKey(0)
OUTPUT :

number of faces = 7


for more information visit this post

Comments

Post a Comment

Popular posts from this blog

"Kernel Panic” error After upgrading Parrot OS

HACK HC-05(BLUETOOTH MODULE) FRIMWARE INTO HID FRIMWARE

HACK AND CLONE CAR KEY FOB, MY EPIC FAILURE...!!