Python Face Detection and Face Mesh (python OpenCV & MediaPipe package)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to talk about how to detect faces and draw a face mesh using the media pipe package in python programming language so please stay tuned but before going further remember that in previous videos we have talked about how to read from webcam or let's say a stream from webcam so if you're not familiar with these lines you can check that video which there is a link in the description below so now if i run the code you can see i can stream from my webcam properly so first of all we should import the media pi package so i import media pipe as mp then i simply type mp.solutions because this media pipe provides you a couple of solutions for different stuff for example for face detection for face mesh detection and etc so i simply type mp.solutions and from those solutions i want to use the face mesh solution that's it and i store that in a variable let's say called mp underscore face underscore mesh there it is and because we need to draw some stuff on our image so i simply type mp.solutions dot drawing utilities and i store that in a variable let's say called mp drawing and also because i want to use some default styles in media pipe so i simply type mp dot solutions dot drawing styles and i store that in a variable let's say called mp drawing is tied so that's it so now i want to apply the face mesh model on our image which we have on our image or let's say our frame which we have a streams or let's say which we have read from our webcam so here you want to apply face mesh model using the media pipe package and now in order to do so we simply type mp underscore face mesh which is this variable which we have defined here and then i simply use the face mesh function and i should pass an argument called refine landmarks and because you want to refine the landmarks you should pass true and you will see what do i mean by refining landmarks by the end of the video so please wait a minute so from the mp underscore face underscore mesh we are calling the face mesh function and we are passing this argument which i'm going to talk about that at the end of the video by the end of the video let's say and then i want to process the image so i want to pass this image to this model in order to process and detect the landmarks and do other stuff but remember that the opencv package doesn't use the rgb system it uses the bgr system means blue green and red but a lot of packages like mediapipe uses the rgb system not bgr systems so opencv uses bgr but mediapipe uses rgb so we should convert these two systems in order to get proper results so we should convert them first and then we should pass that to the phase mesh model that's it so i simply use cv2 which is that opencv package and i use cvt color which stands for convert color and the source image the source image is our image you can see here and then we are going to use cv2 dot color bgr to rgb you want to convert bgr which is the opencv system to rgb which is the phase mesh system the media pipe system so i store that in a variable let's say called image so as a matter of fact i'm updating the image and now we don't have any problems because you are passing an image with rgb system to media pipe and now after applying the face mesh model we want to draw annotations on our image but remember from now on to the rest of the video we are going to use the opencv package in order to show the image and etc so as a matter of fact we are going to work with the opencv package and the opencv system is bgr not rgb so as you can see we have converted bgr to rgb in order to pass our image to the media pipe package but from now on we are we are going to use the bgr system so again we should convert that to the bgr because from now on we are going to use the opencv package so we should we should restore that to the bgr system so that's it so i can copy this and paste it here but the system the conversion that we are going to use is rgb is rgb2 bgr so that's it and before going further let's restore the results of this line in a variable let's say called results so after applying the face mesh model on our image then we are going to store the results in a variable let's say called results then we want to say if there is any face in the image i mean if any face have been detected then we are going to draw annotations on the image so we simply type if results dot multi underscore face landmarks then we want to loop over each face so we simply tap for every face landmark for every face landmarks so for every face landmarks in results.multiface landmarks and results dot multi-face landmarks then i'm going to draw annotations and now we want to draw a couple of stuff the first thing is you want to draw the irises so let's do that so i simply type mp underscore drawing because we have defined this variable here so i simply type mp underscore drawing dot draw landmarks because you want to draw some landmarks on our image and our image so our image is the image variable which we have defined above and then we are going to use other stuff so here i should pass the landmark list so the landmark list is our face landmarks there it is and now we want to tell the media pipe that we want to connect the irises so the connections that we want to build is mp underscore face mesh dot face underscore irises so this mp underscore face mesh is this variable which we had to find here and we want to connect the face mesh irises now then we are going to add other stuff but now for now you want to connect the face mesh irises landmarks there it is and also we want to pass another argument which is landmark drawing specifications which i'm going to pass none and you will see what do i mean by a landmark drawing specifications if you wait a minute because after running the code i will show you what do i mean by a landmark and then you will see that what do i mean by landmark drawing specifications that's it and now we want to pass connection drawing specifications connection drawing specifications i want to use the default specifications the default style in the media pipe so i simply use this variable which we have to find here mp underscore drawing styles mp underscore drawing styles and because i want to connect the irises i want to get default face mesh iris connections style so that's it and now if i run the code now you can see it works properly but now let's talk about what do i mean by landmark drawing specifications which i have passed on for example suppose that i want to pass a red color to landmark drawing specifications so in order to do so i use this variable which we have defined here mp underscore drawing so here i simply you i simply use that variable mp underscore drawing and i want to use the drawing specifications function and i for example i want to pass the red color so i simply type color is equal to and i pass the red color 0 0 255 so now if i run the code you can see these are the landmarks and this is the reason that i pass none because we don't want to have all that landmarks on the screen so i pass none to the landmark drawing specifications and also i want to draw other stuff so let's copy these lines and let's paste them here so also i want to draw face underscore contours face mesh underscore contours and now the style that i want to get is get the default face mesh contours style so if i run the code you can see the contour and the irises and also now let's add another thing called tessellation which you will find out what do i mean by tessellation after running the code so here i simply pass face mesh underscore tessellation and here i want to get the default style for tessellation so get default face mesh tessellation style so now if i run the code you can see we have contour tessellation and irises and if you want you can change the order for example to make the irises more apparent so let's put them in the last and also the contours after the tessellation so now if i run the code you can see now the irises and also the contour is more apparent more visible and finally let's talk about this argument refined landmarks which we have passed through so if i remove this and if i run the code you can see we get this error it says value error landmark index is out of range invalid connection from landmark number 475 to landmark 476. so remember that you should pass refine landmarks equals to true in order to get rid of this error and remember that this face mesh function has other arguments as well for example it has an argument called maximum number faces which its default value is one but if you want to detect more faces you can change the maximum number phrases to 2 3 and etc and also as we have discussed in the face detection video you can see that it has other arguments like minimum detection confidence and minimum tracking confidence which if you choose a higher value the model will be more strict so for example if you choose 0.70 then the model would be more strict in detecting the faces and if you pass 0.7 for the minimum tracking confidence then it is going to be more strict in tracking
Info
Channel: Koolac
Views: 6,466
Rating: undefined out of 5
Keywords: face detection using opencv python, face detection python, face detection, face detection project, face detection using python, face detection and recognition using opencv python, face recognition python, face recognition, face recognition project, face mesh mediapipe, face mesh python, face mesh mediapipe python, mediapipe face mesh, mediapipe tutorial, mediapipe python, python mediapipe face detection, python mediapipe face mesh, face recognition python mediapipe
Id: yvXPKfil1hY
Channel Id: undefined
Length: 10min 51sec (651 seconds)
Published: Tue Aug 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.