computer vision(opencv) 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, but for just simplicity we use it today.
"using this method we can also detect many other features like face,eyes,full body,lowerbody,russian number_plates,etc.."
here the same method modified for video face detection
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\Yourusername\appdata\roaming\python\python39\site-packages\cv2\data 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 = cv.imread('group.jpg')
#resize the image if it is like 1900x1370 or more than 1020x1080 or skip itif less
#resize cause large
bgr = cv.resize(bgr,None,fx=0.25,fy=0.25, interpolation=cv.INTER_LINEAR)
#cvt the bgr to gray cause haars cascade only works in gray
img = cv.cvtColor(bgr,cv.COLOR_BGR2GRAY)
cv.imshow('gray',img)
#add haars cascade
haars_cascade=cv.CascadeClassifier('haarcascade_frontalface_default.xml')
face_rect=haars_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=12)
#if your output doesn't detect all the faces then decrease the "minNeighbors" value(min is 1)
print(f'number of faces = {len(face_rect)}')
for (x,y,w,h) in face_rect :
cv.rectangle(bgr,(x,y),(x+w,y+h),(255,0,200),2)
cv.imshow('detected_face',bgr)
cv.waitKey(0)
OUTPUT :number of faces = 7
Comments
Post a Comment