Face overlay effect using opencv

Hello Everyone, Today I'm here with with new and very useful article for students interested in image processing projects. Have you ever made any opencv Projects? Yes/No 
If your answer is yes, let me ask you one more question. Have you ever heard about overlay effect?
overlay_effect_example_image.png

 If you're confused or saying no, let me elaborate it with one example. Have you seen any sci fi movie
sci-fi_movie_face_overlay.png


 in which some of the things like human, guns, vehicles etc. were being detected, if yes, you may have seen any effect on them which are just for showing off that you will consider it as detection. But as you  might have made earlier any opencv projects like face detection, object detection, face recognition etc. You might have created rectangle 

outside of detected object for showing the user about detection. In this article we're going to make like sci-fi movies. Off course it won't be exact and hard but similar and easy. So let's start.
 First of all we need :-
I hope you've done all things ready.
Now let's open Your IDE or any text editor and begin the development.


1
import cv2
At first line, let's import cv2 module.

2
def detect_and_save():
in the second line, let's make a method called "detect_and_save" and start coding inside it.

3
4
    alpha = 0.4
    beta = 1-alpha
At line 3-4 , Let's make two variables alpha and beta and initialize values to them to 0.4 and 1-alpha respectively.

5
6
7
    cap = cv2.VideoCapture(0)

    classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
At line 5, we're starting our video camera with default camera index value(0) with using VideoCapture method of cv2 module and passing in cap variable.
At line 7, We're calling the classifier file we've downloaded.
Note: Download it from above link and place in same working directory of python project file.


9
10
11
12
    while True:
        ret ,frame = cap.read()
        overlay = frame.copy()
        output = frame.copy()
At line 9, let's start while loop for infinity and under this, we'll do all necessary operations..
line 10 is reading frame with using read method from cap variable we made earlier as object of cv2.VideoCapture(0) method.
At line 11, and line 12, we're making to swallow copies of same frame we're getting in line 10.

14
15
16
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

        faces = classifier.detectMultiScale(gray,1.5,5)
At line 14, we're converting our BGR format frame(image) into GRAY color .
[ Face detection / recognition is easy in grayscale images. ]
At line 16, We're getting all detected faces under gray frame. Here, 1.5 is scaleFactor and 5 is minNeighbors

18
19
      for face in faces:
            x,y,w,h = face
At line 18, we're starting " for loop " for all faces in faces named variable we made in line 16.
and in line 19, we're getting all 4 coordinates( X, Y, Width  & Height) of each face detected.

21
22
23
            cv2.rectangle(overlay,(x,y),(x+w,y+h),(255,200,0),-1)
            cv2.rectangle(overlay,(x,y),(x+w,y+h),(255,0,0),1)
            cv2.rectangle(overlay,(x,y-20),(x+w,y),(25,20,0),-1)
At lines 21, 22 and 23, we're making 3 rectangles on "overlay" frame, first at boundary of the detected face as filled rectangle. second one is also at boundary of selected face but as a border. the third rectangle is at top of the detected face.
Line 21 is making bluish effect, line 22 is making blue border and line 23 is making black rectangle at top


25
        cv2.addWeighted(overlay,alpha,output,beta,0,output)
All the important thing is being happened at line 25.
we're calling a method called addWeighted() from cv2 and passing the parameters as follows:
overlay is one of copied frame on which we've made 3 rectangles. 
alpha is the variable we made at beginning is the value as opacity of overlay frame.
output is second copied frame on which we'll apply overlay effect of first copied frame called "overlay" and
 beta is another variable we made at beginning is opacity of "output" frame. 
0 is gamma and output is as destination where we're applying these overlays of 0.4 opacity of overlay named frame and 0.6 opacity of output frame.

27
28
       cv2.putText(output,"Human face Detected!",(x+10,y-10),cv2.FONT_HERSHEY_SIMPLEX,
                        0.3, (0, 0, 255), 1)
At line 27, we're simply adding "Human Face detetcted!" text at center of black rectangle made on top of the detected face.  [ see👉 parameters of putText() method

 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
        if not ret:
            continue
        cv2.imshow("FACE DETECTOR",output)
        key = cv2.waitKey(1)
        
        if key == ord('q'):
            break
        elif key == ord('c'):
            cv2.imwrite('./images/CID_{}.png'.format(time.strftime('%d%m%y_%H_%M_%S')),output)
    cap.release()
    cv2.destroyAllWindows()
The rest codes from line 29 to line 39 are as follows:
if there isn't any frame caught or we can say value of ret is False, loop will be continued (jump the current iteration)
line 31, is for showing the resultant frame (output) and window name is assigned as "FACE DETECTOR"
line 32, is making one key event variable which will catch all keys pressings from keyboard.
at line 34, we're saying that, if key will be 'q' button, break the loop and come outside.
else, if key is 'c' button (line 36), let's save current frame as image(line 37) in images folder of current working directory in png format with file name as format CID_ddmmyy_H_M)S.png with using function strftime of time module which will save all frames as different names at current date-time format.
At line 38-39, we're releasing all resources occupied by cv2 module and destroy all open windows of cv2 under this program respectively.

44
45
46
if __name__ == "__main__":
    import time
    detect_and_save()
Under block of if condition (line 44), now time to import time module and run our method "detect_and_save()"

Now, we've done all thing successfully.

Let's see .
Whole Code Snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import cv2
def detect_and_save():
    alpha = 0.4
    beta = 1-alpha
    cap = cv2.VideoCapture(0)

    classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

    while True:
        ret ,frame = cap.read()
        overlay = frame.copy()
        output = frame.copy()

        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

        faces = classifier.detectMultiScale(gray,1.5,5)

        for face in faces:
            x,y,w,h = face

            cv2.rectangle(overlay,(x,y),(x+w,y+h),(255,200,0),-1)
            cv2.rectangle(overlay,(x,y),(x+w,y+h),(255,0,0),1)
            cv2.rectangle(overlay,(x,y-20),(x+w,y),(25,20,0),-1)

            cv2.addWeighted(overlay,alpha,output,beta,0,output)

            cv2.putText(output,"Human face Detected!",(x+10,y-10),cv2.FONT_HERSHEY_SIMPLEX,
                        0.3, (0, 0, 255), 1)
        if not ret:
            continue
        cv2.imshow("FACE DETECTOR",output)
        key = cv2.waitKey(1)
        
        if key == ord('q'):
            break
        elif key == ord('c'):
            cv2.imwrite('./images/CID_{}.png'.format(time.strftime('%d%m%y_%H_%M_%S')),output)
    cap.release()
    cv2.destroyAllWindows()




if __name__ == "__main__":
    import time
    detect_and_save()
                        
                        
        
        
        
                    
        
        
        
        
        
        

Now, time to run and see the effect.

Watch full detailed elaboration of the project here.


Enjoy !

Comments

Popular posts from this blog

Make CCTV Camera Software using Python || AI and Machine learning tutori...

Drowsiness detection system using openCV in python

Make Barcode Scanner using Python