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.
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:
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".
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 :
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)
number of faces = 7
hell yeahhhh........
ReplyDelete