Facial Activity Recognition - Project 1
ARNAB MUKHERJEE ????
Automation Specialist (Python & Analytics) at Capgemini ??|| Master's in Data Science || PGDM (Product Management) || Six Sigma Yellow Belt Certified || Certified Google Professional Workspace Administrator
Project Flow :
1. We will be using the OpenCV library
2. By using the opencv we will have access to our webcam
3. The webcam turns on
4. If the person in front of the camera is doing the particular facial action that the AI is trained on then it will create a bounding box and show that it is smiling
5. If there is no such movement, it will not detect it
Process
Step 1: At first we will install the opencv library on our IDE. Always remember to use local IDE, as you will need access to your system webcam. Avoid online IDE.
To install the opencv module from the terminal, open the terminal and write :
pip install opencv-python
As you have installed the opencv, write in the code: import cv2
Step 2: Now we need to download an XML file name - haarcascade_frontalface_default.xml. Go to the browser, copy and paste this file name, and download. Make sure the XML file is in the same folder in which the project is running.
Now we need to download another XML file name - haarcascade_smile.xml. Go to the browser, copy and paste this file name, and download. Make sure the XML file is in the same folder in which the project is running.
Link to download the XML file : https://www.kaggle.com/datasets/lalitharajesh/haarcascades?resource=download
Step 3: Now input the XML file into a variable by writing the next line of code.
We are going to do it for the face first.
cascade_face = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Here we use the function "CascadeClassifier", and put the name of the XML file inside the ().
Now we are going to do it for the smile.
cascade_smile = cv2.CascadeClassifier('haarcascade_smile.xml')
Here also use the function "CascadeClassifier", and put the name of the XML file inside the ().
Step 4: Now we are going to open the camera of our desktop and with the help of our camera, we will capture our video
For that, we will use this code: cap = cv2.VideoCapture(0)
The '0' inside the brackets, stands for the primary camera. If there is more than one camera it would count as 1, 2, 3, and so on. We are going to capture the video with the help of this function 'VideoCapture' and store it in a variable called 'cap'.
Step 5: Now we are going to put up an infinite loop. The reason for this is that it will capture the smile, every time we smile and not just stop after one iteration.
We are also going to use the read function, for the video that has been captured, now we are going to read out that respective video.
We use the code :
while True:
? ? ret, image = cap.read()
print(ret)
Step 6: Now we are going to convert this BGR to GRAY. We are going to convert the captured video from BGR to GRAY Scale. We are going to use this code: g = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY). Here we have used a variable 'g' and used the 'cvtColor' which converts the BGR to GrayScale. So whatever our image is, it will convert that from BGR to GrayScale.
Step 7: Now we are going to use the face cascade, the cascade_face variable, for that we will use a variable 'f'. We are going to use the 'detectMultiscale' function. We are going to use this code:
?f = cascade_face.detectMultiscale(g, scaleFactor = 1.3, minNeighbors = 5, minSize = (30,30),)
Inside the (), we put the variable 'g', another function called 'scaleFactor' whose value is equal to 1.3. Now we need to use another function called 'minNeighbors' whose value is 5. Now we need to put the minimum size of the image it has to work with, using the function 'minSize', whose value is (30,30)
Step 8: Now we will use the 'For loop', where we will use x, y, w, h, where x and y stand for coordinates and w stand for width and h stands for height. We are going to add (x+w) and (y+h), as horizontal gets added to horizontal and vertical gets added to vertical. Then we will put the BGR values and finally the value 2. Now we are converting from the BGR to Gray Scale.
for (x,y,w,h) in f:
? ? cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 2)
? ? gray_r = g[y:y+h, x:x+w]
Step 9: Now we are going to use the smile cascade, the cascade_smile variable, for that we will use a variable 'f'. We are going to use the 'detectMultiscale' function. We are going to use this code:
s = cascade_smile.detectMultiscale(gray_r, scaleFactor = 1.5, minNeighours = 15 , minSize = (25,25),)
Inside the (), we put the variable 'g', another function called 'scaleFactor' whose value is equal to 1.5. Now we need to use another function called 'minNeighbors' whose value is 15. Now we need to put the minimum size of the image it has to work with, using the function 'minSize', whose value is (25,25)
Step 10: Now we need to use the For loop in which we will mention the text to be displayed as "SMILING", the Font in which it will be written, the color, etc.
Note: Run this for loop inside the previous for loop.
Use this code :
for i in s:
? ? ? ? if len(s) > 1:
? ? ? ? ? ? cv2.putText(image, "SMILING", (x, y - 30), cv2.FONT_HERSHEY_PLAIN, (0,255,0), 3, cv2.LINE_AA)
Step 11: Here we will use the 'imshow' function outside the "For loop".
Code :
cv2.imshow('video', image)
k = cv2.waitKey(30) & 0xFF
Step 12: We have to run another 'If' statement, that if K is equal to 27, then we need to break the loop.
And also use the 'capture dot release' and 'destroyAllWindows ' functions.
if k == 27:
? ? break
cap.release()
cv2.destroyAllWindows()
######################################################
Complete Code
##Terminal # pip install opencv-python
#Download xml file name - haarcascade_frontalface_default.xml
#Download xml file name - haarcascade_smile.xml
import cv2
cascade_face = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cascade_smile = cv2.CascadeClassifier('haarcascade_smile.xml')
cap = cv2.VideoCapture(0)
while True:
? ? ret, image = cap.read()
print(ret)
? ? g = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
? ? f = cascade_face.detectMultiScale(g, scaleFactor = 1.3, minNeighbors = 5, minSize = (30,30),)
? ? for (x,y,w,h) in f:
? ? ? ? cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 2)
? ? ? ? gray_r = g[y:y+h, x:x+w]
? ? ? ? s = cascade_smile.detectMultiScale(gray_r, scaleFactor = 1.5, minNeighbors = 15 , minSize = (25,25),)
? ? ? ? for i in s:
? ? ? ? ? ? if len(s) > 1:
? ? ? ? ? ? ? ? cv2.putText(image, "SMILING", (x, y - 30), cv2.FONT_HERSHEY_PLAIN, (0,255,0), 3, cv2.LINE_AA)
? ? cv2.imshow('video', image)
? ? k = cv2.waitKey(30) & 0xFF
? ? if (k == 27):
? ? ? ? break
? ?
cap.release()
cv2.destroyAllWindows()