FACE DETECTION & BLURRING USING COMPUTER VISION
Swaroop Shinde
1x Red Hat Certified (EX180) ★ DevOps enthusiast ★ Docker ★ Ansible ★ Terraform ★ Jenkins ★ Linux ★ Kubernetes ★ Git-Github ★ Cloud Computing
?? 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)
?? 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,
?? 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
-----------------------------------------------------------------------------
?? Now Let's Check if The Video is Correctly Imported or Not and We've Seen to do so in the Previous Article.
-----------------------------------------------------------------------------
?? 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 .
?? Below the video.read() Function, We are detecting the face by
?? 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.
?? 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
?? So Using these Co-ordinates, we will Draw a Rectangle Around the Face, Lets see How.
?? So as I told it Stores 4 Co-ordinates and for easy Understanding We are Storing it in variables with same name & How ?
领英推荐
?? This means After we get the X2 Co-ordinate, we will Connect it with X1 so that we will have a Diagonal
?? Something Like this, & Same we will do for the Y2 coordinate to Complete the Rectangle by
?? So by These Co-ordinates Our Rectangle will Be Complete, Now Let's Plot a rectangle around the face By
?? 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.
?? We Have Detected the Face Guys, Now Blurring it won't be A Big Deal. You can do This By
?? 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
-----------------------------------------------------------------------------
???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 !!
-----------------------------------------------------------------------------