FACE DETECTION & BLURRING USING COMPUTER VISION

FACE DETECTION & BLURRING USING COMPUTER VISION

?? Hey There !!…I Hope You all are Having Fun with Computer Vision & Must Have Learnt Something New From my Previous Articles. It's Time to Rise the Level Now. In This Article we will Learn How to Detect & Blur The Human face using Computer Vision in Python.

???To implement this Practical we will be using Anaconda Python which you can download by Clicking?Here?and from Anaconda Python we will be using Jupyter Notebook to Implement this Practical.

?? Let's Begin.

-----------------------------------------------------------------------------

? Practical Implementation :

?? There is Noting be Learnt before Implementation, So let's Start by first importing a Video as we did in the Last Article (CROP AND DISPLAY VIDEOS IN PYTHON)

No alt text provided for this image

?? So in The First two Lines of Code, We Imported CV2 module and the Face Video, The Third Line is, as we have seen How to Create a Machine learning Model in Simple Linear Regression, In Computer Vision we can Create our Own Models too but Most of the Time The Models Required are Easily Available on Internet So you can Download it Easily & Import the Model Using,

  • Syntax :
  • <Variable> = cv2.CascadeClassifier (<'Model file Name.ext'>)
  • Example : model = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

?? This will Load your Frontal Face model But if it Fails To Load because your Model is Present in any Different Directory or any other Location, use this Syntax

  • model = cv2.CascadeClassifier(cv2.data.haarcascades + 'Model file Name.ext')

-----------------------------------------------------------------------------

?? Now Let's Check if The Video is Correctly Imported or Not and We've Seen to do so in the Previous Article.

No alt text provided for this image
No alt text provided for this image

-----------------------------------------------------------------------------

?? We have the Video Playing, Let's Start by Detecting where The face is, We Know where it is Obviously but For Computer, it Doesn't Know What and Where the Face is & This is the Reason we've Imported the Cascade Model. So Lets Use it .

No alt text provided for this image

?? Below the video.read() Function, We are detecting the face by

  • Syntax :
  • <variable1> = <variable in which model is Stored>.detectMultiScale(<Video Windows name>)
  • Example : face = model.detectMultiScale(frame)

?? Now This will detect the face. It is not necessary that the Model when we will Display the Video will Constantly detect the face because in Some cases, if the Person for few seconds looks somewhere else then Our Model will fail to Detect Face, So we Read using If Else Condition So that if the Model didn't detect any Face i.e 0 Co-ordinates or Axis around the Face. So we Will Print There is No face present in the Frame & if There is any Face Present, Then by Else condition we will Display do What ever we want with the Face.

No alt text provided for this image

?? You can see Behind the Face Frame There's No Faces Printing That means Behind the Scene Our Model is Detecting the Face Correctly but How to Know or How can we See it ?

?? When Face Cascade Detects the Face. it Always Gives 4 Co-ordinates around the Face i.e x1, y1, x2 ,y2 which looks like This

No alt text provided for this image

?? So Using these Co-ordinates, we will Draw a Rectangle Around the Face, Lets see How.

No alt text provided for this image

?? So as I told it Stores 4 Co-ordinates and for easy Understanding We are Storing it in variables with same name & How ?

  • x1 = face[0][0] (The First X Co-ordinate )

No alt text provided for this image

  • y1 = face[0][1] (The First Y Co-Ordinate)

No alt text provided for this image

  • x2 = face[0][2] (The Second X Co-Ordinate) + x1

?? This means After we get the X2 Co-ordinate, we will Connect it with X1 so that we will have a Diagonal

No alt text provided for this image

?? Something Like this, & Same we will do for the Y2 coordinate to Complete the Rectangle by

  • y2 = face[0][3] (The Second Y Co-Ordinate) +y1

No alt text provided for this image

?? So by These Co-ordinates Our Rectangle will Be Complete, Now Let's Plot a rectangle around the face By

  • Syntax :
  • <variable name> = cv2.rectangle(<Window name (for the Video )>, (x1,y1), (x2,y2), <Color Code>, <Thickness of the Border >)
  • Example : rphoto = cv2.rectangle(frame, (x1,y1), (x2,y2), [0, 255, 0], 2)

?? This Simply means we are Drawing a Rectangle using cv2.Rectangle function which requires 4 Arguments, a) The Video in which you want to Draw rectangle. ii) The Face Co-ordinates iii) Color Code iv) Line Thickness. Let's See if we have a rectangle Around The Face or Not.

No alt text provided for this image

?? We Have Detected the Face Guys, Now Blurring it won't be A Big Deal. You can do This By

  • Syntax :
  • ?rphoto[y1:y2, x1:x2] = cv2.blur(rphoto[y1:y2, x1:x2], (10,10))

?? When we Plotted the Rectangle, we Stored it in a Variable Called rphoto, as we Stored the 4 Co-ordinates to draw the Rectangle, We will use the Same to Blur so we are using cv2.blur() Function to Blur the Image which requires 2 Arguments i) The Frame in Which you want to Blur by Specifying the Co-ordinates otherwise it will Blur Out the Whole Video and The Blur Size for one Pixel in the Rectangle (10,10). And we are Storing this in the Same rphoto Frame but by Specifying The Same Co-ordinates around which the Image is to be Blurred.

?? Let's Check Our Final Output

No alt text provided for this image

-----------------------------------------------------------------------------

???So From the Above Article we Saw How we can Detect & Blur The Face using Computer Vision. If you Find This Interesting then Do Follow & Connect???.

THANK YOU !!

-----------------------------------------------------------------------------







要查看或添加评论,请登录

Swaroop Shinde的更多文章

社区洞察

其他会员也浏览了