🔴 Build Real Time AI Face Detection with Python for Beginners (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
programmer comm today I'm here with mr. Aaron burn-up what's up you guys we offer the Python tutorial and by the way guys I want to show you we're gonna be working on today look at the screen yo Aaron what are we looking at right now this is a Robert Downey jr. 's glorious face with his Oh going to be learning real-time face detection with Python if you are excited please drop it in the comments below and let us know that you are excited about this this took us forever to put together I apologize we were late today to the stream or 45 minutes behind but a lot of things set up for you guys and make sure you guys get a great experience now that we're here guys take a look at this here is Aaron's face and it is actually what's up did right now right so look at that he can move around right it detects his face with a pretty high level of confidence and this is all real time you guys this is with Python and today the biggest thing we want to do for you is show you how you can do this yourself with a few lines of code and we want to make it as simple as possible because there's so many different places to show you how to do this but they don't make it simple right Aaron yeah a lot of the other tutorials like they added a bunch of unnecessary code to get up and working like this is actually a pretty small app to be honest there's not that much code but there's a lot going on behind the scenes and we're gonna build it first cuz that's the fun part we're gonna get it all going you guys can have it on your own computer running you can detect your own face and whatever other embarrassing videos you have of yourself but then I'm also going to explain it at the end of actually what's going on instead of just like a bunch of like coding your face I'm gonna explain in layman's terms so it's gonna be interesting a different approach than what you've probably seen before but it's gonna be good you guys excited awesome okay beautiful we are pumped so I think at this point what should we take a look at now Aaron should we go ahead and look at like how the algorithm works yeah so Oh first of all here's the Robert Downey jr. photo I caught from the internet just to prove that it's what didn't really have the green square yeah but yeah I have this little presentation here so let's just start this up first just to give you a little debriefing before I jump into the code you guys yeah so first thing here so in a super simplified way but just to give everybody context because I know a lot of you guys are beginners we were all beginners ones me cause II all of us yeah and I just want to give you a short step-by-step thing of how like face detection actually works with Python all right so first step the first thing you want to do is of course this is going to be a machine learning thing that's the really the only way or the most popular way to do this kind of things you have to train it on what a face actually is and then from there then you're able to classify faces down the road so step one get a crap load faces clear good yeah and from there we actually don't need to be color so step two we're just guys real quick go back to step one I want to highlight what you're saying cuz you're saying something really awesome there and I want to make sure everybody who truly really understands this you guys so this is awesome how Aaron is putting this together for you take a look at that like but first thing we do is like we give our algorithm we want to give it tons of faces so it knows how to detect a face so we're just giving a ton of faces you can see George W Bush in the middle and then you can see like there's thousands of faces around it so that's our first thing that we're doing get all of the faces right so what are we doing that on step two so after you have all the faces we actually don't need to be color reason being for this is because what defines a face isn't so much the color it's more so the composition of like this set of pixels like okay there's two eyes and nose and then there's like some reddish or like dark color or lighter colored lips depending on maybe some teeth or something like that so we get rid of the colors because it it hurts the performance of the algorithm I believe but the way OpenCV does it which we're gonna be using it OpenCV if you don't know what OpenCV is OpenCV is an open-source computer vision library that some people put together I don't know who put it together but it's called open CV we're gonna be using that and they have a lot of cool computer vision functions that we can use to build our app so the first step we're gonna do is we're gonna turn everything black and white all of those faces that we had at the beginning here because very first step and we turn them black and white because the computer the only way that algorithm knows how to look at a face is in black and white so you want to give it that it's not gonna be looking at it in color so we turn it into what we call a grayscale grayscale that's the word yeah and then from here once we have all of our great our black and white images our face images we can pump those into the algorithm and it will eventually learn how to detect faces so as you can see here it's detecting all these different faces here where they have glasses open mouths frowning smiling eyes closed hair I mean the hair is all similar here but you get the idea if you train it on enough data it can it can recognize babies females males even monkeys so that's that's pretty much the oversimplified three-step thing you just get a bunch of faces you change them to black and white and then you train it and then you run your class your face classification code and it will find all the faces in your image awesome so that's a pretty simple three-step formula right so step one you just go to step one real quick step one get a crapload of faces step to turn a black and white step 3 train the algorithm that's it guys it's just simple 3 steps okay hopefully you guys understand that so again ya get lots of faces to turn them black and white and then 3 just train the frickin algorithm and now you're good to go now it's gonna start detecting everything so like to actually visit you guys it's stupid simple it's like 10 lines of code and then boom your webcam is working and everything is working so like step one get away from the fear of like what it means to actually use these types of algorithms first use them play with them and then go deeper into the complexity and everything so you can start to understand and make your own models mm-hmm Wow we have 664 people live that is possible oh that is awesome welcome you guys anybody just coming in we're making face detector if you came in a little bit late but yeah so with that said this simple three-step process let's just jump straight into the code you guys okay let me give one more quick demo on case with anybody I'll meet you Aaron and what I was actually even thinking of maybe you could have code on the left hand side so you could actually like make your code like on the left hand side on the right hand side you could have the webcam open the entire time ah you see what I'm saying yeah the thing is the way this is implemented oh yeah yeah it's kind of it's all you have to keep running it you know keep running it yeah that's okay that's okay notice so we'll just keep it this way is the code the code is big enough for you guys to see right I think that's nice bigger and by the way you guys if you are enjoying this video Aaron open up your face detection again just so we can see it unless you comment so guys if you guys are enjoying this and if you think this is cool I think this is so freaking cool that we're doing this with Python right now make sure to hit that like button it's free it doesn't cost you anything it helps us get the video out to so many places so please go down below and like smash that like button subscribe to the channel if you want to learn from Aaron myself and all of us that will just give you so much value so please please please is that like button and let's keep it rolling by the way pew says read this comment dinner and he says Aaron taught me how to code in Python today I finally completed my Titanic data set thank you Titanic data set technique probably something like he probably learned from you and then when and like applied it on something else you'll be right on I don't know what that means but it sounds like something I wouldn't understand so keep that that is awesome all right you got 26 people live 826 what's up you guys okay just keep this running he seems like people keep flooding in where is uh here we go you guys okay we're this is what we're building you guys let's just jump into the code I think we're almost at a thousand so let's just get started all right so let me calm about all the code that is pre-existing here so we can start from scratch for all you guys and actually just all of it okay so the very first thing that we're gonna have to do is obviously install OpenCV so if you're following along with me then please install OpenCV and you can do that with the command wait I got a quit out of this one second it should be pip in fear on Linux or UNIX system Jimmy a favor a few things Aaron do me a favor take your terminal put in the middle one and then also hit command plus multiple times hit command + just to zoom in and make your screen smaller listen it's easier my see for everybody terrible habit is that that's enough right that's really good that's really good so it's easier to see for everybody alright you guys so the first thing you wanna you know what I'm gonna want to run is pip install Python - open CV okay I've already installed it oh also maybe add headless here if you're getting errors this is optional but like if you're getting weird errors try running this as well and it might work but this - headless is optional but point is install OpenCV that's the very first step honestly there's no way I can help you install it it forget there's an endless amount of errors you can get but you're gonna have probably google it and look on open CVS like if you are getting issues let me just do that let's see open CV installing issues or something I'll just give like a brief thing find a Stack Overflow here open see the installation problems probably come into here and there's no answers on this one but uh I mean point is yeah just install OpenCV and hopefully that that command should work for you the one that we were just looking at here and once that's installed then you're pretty much ready to go alright so from here you can come into our code and the first thing we're gonna want to do is simply well well well first all you're gonna want I can face it Specter PI file of course so start with making your first python file phase detector there and just to make sure that it's running let's just do this this is a live shot just a real quick shout-out to pratik he says today I had an interview and I showed yesterday's project of Amazon price detector and I got selected holy crap that is amazing amazing go Team Jacob yo we literally built the prize detector exactly we built a prize detector yesterday you guys would Jacob so that is huge that is awesome all right let's keep accommodations dude yeah that is sick all right you guys so once you have your Python file created face detector pie then let's just make sure it's working correctly okay so the first thing you're gonna want to do is just run pipe will make sure you're in the right directory so you want to make sure that the face detector that PI file is in my in in there in the same directory and then from there then you can just run Python and run face detector dot pi just like that okay nice and then hit enter and as you can see it says code completed here which is the print statement so everything is hooked up and working correctly that's the very first step and I like personally when I'm coding I like to keep this at the end of my program because then I know that if this shows up everything in my code ran and no errors popped up so I leave this at the end so that's just kind of an arrant convention that you can adopt if you like but now that we have OpenCV installed the first thing you're gonna want to do is actually import that library so that library is called CD 2 this is i think the two just means version two it's been seen me too for a long time but just it's called CB 2 you have no choice just type it in and from here go again and then let's see if we get any errors around the same command and as you can see it says code completed which means it ran this and imported library without any issues because it reached this line so from here now we can actually start coding with open CV so coming back down here one thing you're gonna have to do is you're actually let me let me go here so open CV like I mentioned earlier is a open source computer vision library provided by some people and they provide a lot of pre-existing code of course it's a library so they have code that we can use that we're going to be using that we just imported but they also have some data files for us so what this is here is actually train data so if you look here there's what okay so hard cascade is an algorithm we'll get into that in a little bit but just ignore this big scary word our cascade and then it might say this is one we're gonna be using the front frontal phase default because this they provided us with a bunch of data and they actually trained this on a bunch of face images like the ones I showed you in the presentation before like this but they're basic like that yeah so that data is actually trained on ton's and tons of faces like frontal faces right just like this one hundreds yeah like all of these like all ages all that kind of stuff but frontals you know so there's no like blockages there's no side faces yep I'm gonna that just in just front faces yeah so from there they provide all that so what you're gonna have to do is actually download this so if you can just go to this URL okay github slash OpenCV slash OpenCV slash tree blah blah blah and just go to the default front this one frontal face default xml and download it it'll be right here and it's just a big XML file and all this is this is basically a machine learning machine and when you pump an image through all of these all of these numbers it'll tell us and find if there's a face in the image or not and as you can see this goes on forever so this is actually what was already trained on like thousands of images so you want to download this because open C provides it for you training it yourself is gargantuan task of its own so we're not doing that in this video we're just using what they're providing for us maybe we can do that in a future video but point is download this from this URL and then we can continue on from there so any comments Kazi awesome no I love it basically guys to summarize that all we did so far is literally just downloaded that file that's it so it's really simple just hard cascade frontal faces I think default or whatever and then download it put it in the same directory that you're in and that's all we've really done so far and I have mine right here see right now parallel to here along with my Robert Downey jr. photos and stuff oops not so once you have this installed then we can go and import it into our app so the first thing is well first first of all I like I called my variable trained face data because it's what the heck it is and this is the name of the file we just downloaded an XML file I'd actually don't I think I forgot what XML stands for but it just looks like it looks like this with all these tags from here you're gonna want to call the opencv library and call this function to make a classifier so all the classifier is it's a fancy word for detectors so a detector so we're making a face detector app a classifier can classify something as a face that's all really is and then cascade is again the algorithm the HAR cascade I was talking about we'll get into that later just ignore that the scary word but what we're gonna do is we're just gonna pass in that training data and create a classifier for this and since is the front-facing training data then this classifier now will be able to detect front front front word facing faces and that that's what we have here okay so as you can see I put a comment here to load some proof train data on face rentals for OpenCV from there we can continue to choose an image or a video stream or our webcam and then we can actually pass that into this classifier and be able to find the location of faces and then we can put the rectangle around it and yet like you're all that rectangle on the image or the video and we'll get da to all of that but moving on the first thing we're gonna do let's just do the Robert Downey jr. photo I'm just gonna copy and paste it just so I have to type it out but I'll go slowly so you guys can type it out yourself so the four thing we're gonna want to do is choose an to detect the faces in so mine is the Robert Downey jr. photo so the way you do this is you want to call the image read function from OpenCV so Grayson very simple the image you're gonna read it in and it's going to be the the photo that I just had of Robert Downey so that should be this one okay so it's on my desktop here and once you have it over there somebody just donated to us bro that is awesome really yeah hashtag yograj yeah I can buy lunch today yeah finally thank you appreciate that so that's awesome thank you so much for that it's like please make a Ober or oh L a clone app using react native so we might not be using react native but we're gonna be making bunch of clones would react so uber is not on our list yet you might think about putting it on our list we are definitely gonna be doing tick tock and I think Spotify and stuff like that and then uber we might do it at some point as well I'm not sure what Ola is yeah not sure I'll look that up later but anyways this is how you import an image into OpenCV okay so this is actually I mean it image is just an array like a big matrix of a bunch of numbers you know pixels are just numbers so really what this is you're just reading the image into a big like double um two dimensional array so a big image a bunch of numbers and bits from there now we can actually change it to grayscale so again if I let me see if I can actually show this yeah so let's let's just press that before the hard cast Kate frontal XML file link so maybe you want to just either that github real quick and maybe just show the URL and then they can pause it on there so just go there and show the URL so you guys can pause it here and type that in and then go to it okay alright we should put it in the description or something don't wait oh yeah we'll put it in the description to at some point for sure after okay here and make sure you're downloading the frontal face default this one okay there's also a cat faces and stuff I mean you could actually use all these other ones like lower body upper body the just a profile of the face it detects smile all this stuff cool yeah just eyes yeah what's cool about this is you can detect anything pretty much you could you could detect coca-cola cans and stuff but we're just doing faces like the same algorithm works for anything like it's very generic and robust but play trooper I don't know what that is Oh probably like a car it's probably a license plate right yeah like for Russian cars you know but anyways yep so here again URL download this one okay and once you have that then we can continue on so let me just show this this image first so I imported the Robert Downey jr. image and so image read we read it in and then to show this image there's another OpenCV function called image show I am show and this string here is just gonna be the name of the window that pops up and then this is the image you want to display so let's just save this and run this to show you that the Robert Downey jr. stuff is gonna pop up and what is wrong oh I forgot you also need this thing let me just make sure works for us before right so wait he basically waits serving on it yep so the wait key you need it because yes this closes instantly go ahead yeah so what this does is it actually pauses the execution of your code so it will show the image and then it will continue on to the end of the program and it'll it'll terminate the program because they got they got that last line here and it's like okay I'm done so it'll open this for a split second you won't even see it then it'll close it immediately so what this means is this will wait until the key is pressed so you can press any key to continue in the execution so it gets here and it waits and it keeps this open so that you can view the image that that's that's just the way open CD is implemented I mean if you like it or hate it it's just how it is but once you have that then you can run the program and of course Robert Downey jr. pops up here in color just like I said okay and of course you can hit any any button on the keyboard I this his face bar and it just closes it and then it's of course it says codes completed and I'm back to the turbo so I'll show you step by step as we're going what it looks like so we'll just keep these two together okay at the very bottom and okay so we have a Robert Downey jr. image in here next we are going to like we said in the presentation we got to make it black and white because the algorithm that we're gonna be using the the HAR cascade algorithm the way it's implemented only takes green skill images because we can still identify faces in greyscale we don't need the color I mean you can do color but that's a much more complicated like if you want to classify like skin color or race or even like actually identify people that's even more advanced that's what Facebook does you know how you can like tag your friends it automatically knows their face that's way more you need way more training in it of like one person to do that so we're not gonna be doing that here but I just want to mention that so Erin one real quick side note what can they do if they want to learn not only these types of skills but also learn how to make an income with these skills where can they go uh yeah so if you like coding and Python and you want to learn how to actually make a living from it so if you want to start coding and landing clients or land a job and just like get your skills and your confidence up and have projects on a portfolio and all those things and really just do this for a living then we have a course called profit Python for building a profitable programmer so if you see here either named with either name they're the same and the link is in the description if you want to join that yep um we can actually and I'm showing it on the screen actually so I don't know if you can read natalie not see my screen but yeah I'm showing it on the screen so guys the program is called profit with pythons and this program was showing you all of these things that you're gonna be learning today you know whether it's like detecting faces or building ecommerce stores or any kind of project that you can take from up work web scraping as advanced as you can think it and then we show you how to actually land those clients our biggest one of the biggest things we focus on is like how can you actually earn a killing with Python that's huge for us we have dedicated Python success coaches and really one of the most important things in this program is the fact that you get weekly live Python training calls now Aaron like what do you think is a benefit of those like live calls you know for the Python like live training calls mmm live training calls I mean humans learn live that's how I've been learning all throughout history so having somebody there to give you real-time feedback and answer your questions right then and there is like evolutionarily blow if that's even a word the way to learn so it's pretty much like you're in a college course you know but I mean what a cool one cuz you have a cool teacher and you can show up and do it everyone but there's live calls where you build apps together and you can ask all your questions directly to a Python expert literally build a track little projects every week so like Jacob is building an e-commerce store like you guys think we're building live projects on our YouTube wait till you get inside of the program because they're the conversation turns over to you and you are the one building all these projects and then we're giving you personal video feedback on each of your projects that you built and we get tons and tons of submissions from people like even right now you know if I show you guys here like we have tons of projects that people are sending in that they're building with Python that they're building like people are building kovat trackers and all kinds of stuff so here's an example of a project boom here's something that was built and I can switch it up into any different country and you can see that it pulls the queries it shows all this data and people are using these skills to freelance so if you guys want to learn these skills you wanna you want to be able to make an income with Python then definitely go and check out in the description below the program is called profit with Python and we would love to see you inside of there yeah so cuz coatings fun you know building projects like this this is all fun but I mean if it's fun then it's just a hobby if you can make a living from it I mean I say why not because then you can have your fun and make a living from it and do it more so that's why I provide you these courses we love helping people like do what they love be that being coding and just live the lifestyle they want instead of you know slaving away at a job they hate whether it be like retail or fast food or whatever it is yeah those were mentioning that optional for you but definitely there we would highly recommend it and just check it out but continuing on talking for a little bit long man this is keep going go out for though for the for the viewers yep where was I I was I imported the image so like I said we need to make it grayscale because the algorithm requires to be grayscale because it's just easier to only deal with one number on each pixel the the color there's it's like on a range from like to white instead of like our there's no RGB in each pixel it's just one one number I said the three numbers across red green and blue channels so we've got to change it to grayscale so of course OpenCV allows us to do that too and let me just go grab that code down here and I will just paste it and you guys can type it out yourself as well so as I as I said here in the comment you must convert it to grayscale and so I just called the variable grayscale image because it's the same image but grayscale and the function that you want to call is convert color so open CV dot convert color and then you give it the image you want is the first argument and then the the second one is what kind of conversion you want to do because this function actually you can do a lot of different things why don't we pull the documentation I'll just show you a little bit really quick convert color so open CV documentation and let's just go here not this one this one you shut up ugly yeah it is I got out of there quick we just lost 600 people in 10 millisecond where is it probably I guess there was a bunch gray basics threshold in options is this the right documentation open CV two point okay once you everything okay see become color not it awesome yeah so here comfort color you have a bunch so the first one is the source image which is the Robert Downey jr. image that we had first and then there's a bunch of different things you can do so for us we're gonna be using greyscale but you can also change like you can change the gray you could change color to just the red Channel or just a blue Channel or you could like dim the whole thing pretty much you can convert the image to whatever you want there's a bunch of stuff in here to definitely check out documentation but just want to show you guys that because this is actually where you would find I do that instead of just like Oh Erin told me to type it no the documentation has all the information you need so you can go check it out you can play with it but for now we're just gonna do that very simple and cb2 color BG art - grey one caveat - or one little quirk OpenCV is I'm sure everybody knows what RGB is red green blue that's how each pixel can like mix those three channels colors together and make any color on for each pixel but an open CV the channels are actually backwards so instead of RGB it's BGR and that was very frustrating when I was first learning it but just be aware of that and that's why it's this BGR to gray because it's taking this image and then turning it to gray so let's just show you what that looks like so instead of displaying the color image I'm gonna display the grayscale image let's go back to our terminal and type in Python face stick without pi again enter and wallah we have a great scale Robert Downey jr. beautiful it looks just as sexy that is nice yeah that is nice all right you guys and then of course any button to quit out and we are back to our terminal and now we can continue so once so that's pretty simple straightforward I'm sure you guys all understand at that point again go check out these different settings there's a bunch you can do and of course you could even convert it from group from gray back to RGB and it would be considered RGB but the thing is when you lose the color and it's gray then it actually is just gray so you won't actually get the color back but it'll technically be a color image but it's not actually going to look like it but I'm getting off topic there from here the thing we're gonna want to do next is going back to our our little presentation here is we want to train the algorithm right but that already happened OpenCV was already able to do that so all we want to do now is to plug that gray image into our algorithm that we're given by OpenCV the XML file we downloaded and from there it'll be able to open up all these or detect all of these faces so like this is actually one image and it detected all eight and that's what we're gonna be doing next so let's just quit out of here go back and let's just detect faces so yes yes some of you guys are probably like oh maybe this this is kind of fake it's just one liner that is a technology sees that is true but like I said at the end of this video I'm actually gonna go through the whole algorithm to explain it in layman's terms so that you can actually understand how this is working and why it's working in all of that because then you have a full understanding and there's no point of coding yourself if it already exists so this is actually a better approach because you can understand it you can get the result and that's all you need once you have those two things then you can innovate further and go on and optimize or create your own algorithms for your own problems so yes this is just one line defeat face to detect the faces but I mean shorter coat is better right cause II shorter coat is better because like look here what was that it's shorter quote shorter code is always better less errors I hate having a lot of code I even tried to get my functions never to be more than like four lines of code or I'll refactor it yeah nice having nice encapsulated code that's the word for it like if your function is just it does one thing and one tiny thing then you want to encapsulate it in one thing and keep keep your code very modular so you can plug and play functions all over the place so then says you guys are amazing you guys are putting so much effort to make us understand things like Python and you were doing this at midnight as well so amazing you guys love you all thank you so much Johnson we're not doing this at midnight it's money time for me yeah really early for Aaron but I thank you we appreciate the support guys and thank you for the positivity that really keeps us going yeah guys you wouldn't be doing we wouldn't be doing this if it wasn't for you guys like there's no there's no reason if nobody's watching them what's the point so yeah thank thanks for thanks for sticking around and putting up with us all right so here what this is what's happening here is before we we created our classifier the trained face data that OpenCV supplied for us within this XML document that we downloaded and from there we're gonna call a function called detect multi-scale okay so this probably sounds a little bit maybe scary maybe not to others but all this means is whatever this classifier is which is a face face classifier since that's what we trained it on the trained face data then we want to detect all of the faces with a multiscale thing so all that means is no matter the scale of the face if it gets smaller or bigger then if it's small or big that it'll detect it anyways it's just looking for the overall composition like the relations of eyes to the nose to the mouth whether it's smaller up close or if there's multiple of them then it just wants to detect all of them so that's what detect multiscale is - why don't I actually pull this on the by documentation and quite question Erin proc R says can we use phone camera for detecting the face in real time mm phone camera I mean you would have to somehow connect your phone camera to your laptop so that you could run OpenCV on there I think OpenCV has mobile support or to some extent I actually remember I've never done open C up on mobile but they might not take a look at undo it yeah but you would have to somehow hook up your phone camera I mean I'm sure it's possible somehow I haven't personally done it but you'd have to hook up your phone camera to your laptop and then pipe that into OpenCV somehow like maybe you make it your default camera I actually think yeah I'll take I'll take a look at that you don't have to worry about that but I'll take a look look at it and see if something like that exists but if you guys do understand how to use it this way then you'll be able to even use it later for phone camera and things like that yeah we're focused on teaching a thorough understanding of the basics the basic building blocks like all encompassing everything so that when you learn this then you're equipped to actually go do your own innovation your implement your own creativity and do those kinds of things cuz that's where the real learning happens and then you can be original like I don't just wanna be like hey here's this project it's like you know here's the skill now you can go do what you want with it awesome all right let's keep going so let's just open up this detect multi scale here we go cascade classifier and here we go so detect multi scale this one it basically detects objects at different sizes and the input image so objects is just faces in our case because that's what we trained it with we could have trained it with dogs we could have trained it with cars with houses but in our case it's faces and from there the detected objects are returned as a list of rectangles so pretty much it just returned as the coordinates of those green rectangles and once we have those coordinates are able to actually draw those rectangles on our image so that's what we're gonna be doing next okay so from here detect multi scale we feed in the grayscale image which is a Robert Downey jr. image and like like we said this returns the this returned the the coordinates of the rectangles so that's why I called it face coordinates because it'll give us the coordinates of the rectangles surrounding the face and then from here once we have these coordinates it's it's very simple to just draw rectangles onto the image or the the current frame of the video if we're doing a video and it just runs on every frame so from here why don't we actually print out first of all we don't need to display Robert Downey jr. anymore but why don't we actually print out the coordinates okay so face coordinates I spelled that correctly yeah and let's just run the code okay so the the coordinates of Robert Downey jr. face should be displayed in the terminal can you make your can you do me a favor can you make your terminal like wider that way your line of code can show on one line instead of wrapping perfect perfect perfect am i dirty coding habits I've got it's all good bad habits beaten into me but all right you guys so this is the location of Robert Downey's face in the image is it possible Aaron is it is it possible to also later show the face coordinates but with your face with the webcam open and so then as you move it like actually prints out the console logs in the terminal yes actually yeah okay so let's do it like let's do that yeah let's do that once we get to the webcam but right now we're also focused on getting one single image up and running got it so I'll explain this so what this is like I said ever it turns the coordinates of a of the face so all this is is the upper-left coordinate of the face and this is the bottom right coordinate so just a bounding rectangle that's all you just show it visually like can you open up the dot Robert Downey jr. Robert Donnie Jr's photo and then like show what you mean yeah so like I was saying of course this is gonna be great skilled in within our app right now but just for demonstration purposes the face is about right here okay so this 307 115 would probably be raid around here the upper-left-hand of ghana face and then this 336 336 would be right around here the bottom right corner bottom looks like you have a bounding rectangle and then from here it's very easy to just actually draw the green the green box surrounding the face and that's how it happens okay we're doing all that manually we're actually drawing we're finding the location of the face and then we're drawing the rectangle around the face and then we're displaying that new image with a nice finger so that's exactly what's happening with and they're rectangle that we're drawing we're drawing it from a module and OpenCV lets you draw as well yes got it how do I get rid of yo Tony how's it going awesome this is Tony that was actually yeah you know I'm right it is yeah yeah Tony Orbitz what's up man yeah this is awesome dang it I think my terminal bugged out let me just terminate and open a new one cool terminal desktop and clear mm-hmm and again Python is gonna be face detector you're not in that file I think you have to go in that file why don't you open up your terminal in Visual Studio code um I could do that where's it just command J and command J yep and go to my bash you can you can be in your open that's my desktop and from here we can run Python and go to face detector or dot pi and there we go I actually haven't run it from here let's see if it actually displays the image I've been here actually your arm will swipe up no no it is running it's running so use your four fingers to swipe up on your mousepad and then like yeah and then it'll show oh it doesn't have yet I thought it was visually it's because I'm printing the coordinates and then the wait key but it's not um displaying something it's just bugging out so god I just let's just terminate this is how you terminate it yeah just open up a new yeah perfect you can just open up a new terminal if you want oh stick to this one because then I can cuz I like having the full code open the whole thing at once I'm good got it so okay back to desktop Python face detector hi okay there we go so from here then we can continue on yeah the wait the wait key bugged out because there is nothing popping up there there is nothing to close so nothing to actually capture a key and then continue but that's not that important oh hello Kazi hi guys and yeah so those are the coordinates now from there now like I said we want to superimpose a rectangle over the image so that we can display back and of course you want to do it with the color image again so once we have the coordinate we can go back to the color image and draw the rectangle on the color image once we get the coordinate from the grayscale image in the algorithm so let's just comment this out and the next thing is we're just gonna draw rectangles and of course if there's multiple faces then it'll draw rectangles around all of them so we have a loop but actually let's just let's just leave that for now let's just start with one rectangle so to draw a rectangle this is the function so OpenCV again allows us to just call something called a rectangle and what it takes is an image that you want to draw a rectangle on so of course we use our color image originally then it the next one it takes a tuple of the upper left hand the upper left hand coordinate got it this is the lower right hand coordinate so in this X Y and X this doesn't work because I had the loop so let's actually just delete this for now and like I said the face coordinates is what we want so the upper left hand would be what is it yes a phase coordinates gave us like that that 3:05 thing right like three or five one one six whatever we had in the command line earlier for the Robert Downey jr. space yeah so actually why don't we yeah just moly yeah running type that out perfect like actually just like manually put in the numbers just to show you guys that it actually okay so I'm just gonna show visually what we're doing here you might not be able to see my screen for a second okay so just hold on and I'm gonna just explain this also visually alright guys so basically you know if you guys are with us so far what Aaron is about to do right now is we're gonna have Robert Downey jr. face like this okay and I don't know if he's smiling or whatever and then around this we're gonna draw a rectangle now what we need is the top left so Aaron the 307 and then the one one six do both of those coordinates refer to like X&Y pixels X&Y pixels okay so one is like okay the top so basically just two points right one is the top left point and one is the bottom right point yep that's it okay so this is gonna be and what are the first two numbers in turn is it 305 comma 1 what are they can you tell me you a seven and 115 okay so guys that's that point over there okay then we have the second point here and what are what are the coordinates for the second point three three six three three six okay so it's three three six three three six all right so here are the two tuples this is tuple 1 this is tuple 2 right over here and that's how we're gonna draw the rectangle around somebody's face we need those two points once we get those two points cv 2 allows us to actually make a rectangle around the face okay so that's what arrant the line of code that aaron has if we go look at his screen right now right here yep so that's those coordinates that he's referring to these right here yep and that's what we're gonna do this hard-coded them in just to show you guys for now exactly and then I imagine the 0 255 or whatever that's the color like it's gonna make a green color or something mmm-hmm I choose green so this of course OpenCV is backwards so this is BGR instead of RGB so if this was 255 this would be blue if this was 255 this would be red if I zeroed this one out but I like green so I just chose green but you can you can even make me white you make it black that's why I have to the Hecky one now what's the last two at the end the thickness of the rectangle okay perfect how thin that's all it is so of course if it's ten is gonna be really thick but two is it nice there's a nice thin line awesome so this is guys this is a cartoon way of looking at more cartoony way of looking at it however drawn here you saw Aaron's lot want the one line of code that he has right over there so now let's go ahead and actually run it got it so here we have the the rectangle being drawn on image okay and then down here we have an image show and here we saw the grayscale image so all we got to do is just change this back to image because now image has the green rectangle drawn on it around his face let's give it a save let's go to the terminal and let's run it and something is wrong might have these backwards yeah that's pop try and think this is get what angle documentation know what like that a good stance what it does is it adds them so if we actually look at the code that we had earlier and the one we commented out or whatever it's like X plus W and then y plus h so you want to take the x coordinate and it would exactly so you want to take the x coordinate and add the width to it and then you want to take the y coordinate and the height to it and that will give you what you're looking for so just add the width and at the hyatt whatever yeah you're right I forgot that detail it's so this is actually the upper left-hand point let's go back to there let's let's confirm that so the whoever developed OpenCV decided to implement it this way probably because it was smarter in the long term instead of having the top left point and bottom right point they actually have the top left point and then the width and height of the rectangle so that you can just add this to these points to get the bottom right point so I guess it's equally effective it's the same data just in a different way so actually what this would be would be 307 plus 336 and that would make sense why it's 3 336 because it's a square it's the same number because the width and height of a square is the same so now now let's try it now it should run right correctly back to the terminal oops yeah and I'm much better nice okay so that bug was fixed and there so this is it solution is it still true that the top left point is is 307 and one 1/5 is that still true and the bottom bottom right point is 336 comma 336 no the bottom right point this is the width and height of the square because it's a square so width and height is the same so what so what's the bottom right point then that would have to be this point to get the I mean it's just so some coordinate math so 311 is the X is the x location of the top left point so this is 307 this way okay and then the bottom right bottom right is just one one five yeah and then this is this is one one five no no no you have to come on Kazi you got to add the you got to add the 336 to the width and the height to this point and once you add three through six it'll come out here and go down so that that's how you get the rectangular got it I'm gonna offset so from there we have Robert Downey jr. space okay the problem with this though is of course we hard-coded it in so we do not want to do that okay what we do want to do is actually just get the points dynamically and then print those out so like we just did it's going to be like this nice the thing is we are going to want to make sure we are getting the coordinates from the face coordinates correctly so X Y width and height like we said in the terminal okay so this is X Y width and height here and we can do that using a four tuple and just set this equal to face coordinates okay and this will automatically assign each of those four numbers 1 2 3 4 to these four variables 1 2 3 4 and now that we hope means nice in the two pool then we can just pull those X Y and the X Y WH out just like this and this should work unless I got my math wrong again which could be very awesome we just broke a thousand likes I heard on the video that is amazing thank you guys thank you one more value to unpack what is the last coordinate what was it complaining about face in line equals face coordinates value Air need more than you never do oh yeah you actually did define bogey maybe wait hold on isn't it face coordinates of 0 or something because it's a array with a list within a list so you might have to like is the double brackets there right yeah I know what that is but yeah that'll probably work so let's give this a shot mm-hmm there we go yeah oh that I guess that makes sense because it's a list of here that would make sense cuz if there's multiple faces then you want a list of list yeah yeah yeah have a second race okay that's why that's like that makes sense yeah but there you go there you go guys so now we have it dynamically getting the the face with the coordinates here that is so cool that is so cool can you turn it red real quick just like in just real quick like that's awesome so there we go sunny was telling me you have like scary amount of knowledge color oh yeah I worked at I worked at a I had a data science gig for eight months doing open CV stuff like exclusively like four years oh wow no oops I killed the terminal command T command T oh my god no I'm merging it cause II know that's I'm sorry brutally it's dead damn this guy's this is the right person keep opening it up and once you open it up do let me know but guys this is a right person and learn open CV stuff from like he just was doing this for almost a whole year yeah edge detection motion detection background like you can you can do a lot of cool stuff but right now we're just doing face detection so desktop and again let's go Python face detector you know Aaron maybe like upcoming few weeks just like go crazy edge detection like bring all of that back so yeah you guys look at that we just turned it red super fast super easy yeah so awesome the color I mean minor detail but yeah yeah I'll change it back to green cuz I like green better and alcohol of course this is the thickness so let's let's just do a quick yeah changing this real thick like 5 or 10 that would be awesome kaboom nice that looks that actually looks great I like that level of thickness like it alright let's keep it there yeah so keep that there that is so cool like we're detecting a face in real-time and drawing the circle dynamically around it and this code already works on any image you give it that is mind-blowing you can even you can even have a circle instead of a rectangle but it's only gonna but right now it's only gonna make draw it on the first person it sees you guys so if there are two people it'll only draw it on one person's face but once you write a loop it's so cool we're looping through humans like that like you literally you're looping through human beings that is insane it kind of blows my mind I almost wanted to take a picture I almost want to take a picture of us right now and send it can I do that can I just take a picture of both of us split side by side and then you open that image up and run it on it or is that designed for it no I should work okay cool yeah this this algorithm is in 100 sin accurate they the algorithm the HAR cascade algorithm is more concerned with speed than accuracy so it might know how to percent work but like ninety percent it work so let's alright just make sure you have good lighting and then it should work fine yeah I got good lighting let's detect it right now so I pulled it from our live stream and I'm sending it over to you in slack so what you can do in the meantime is just go ahead and pop it in to your like it's so cool to me so just go and slack and then pop it into your image see what I'm saying mm-hmm okay cool but like guys this is so mind-blowing to me the fact that you can actually like detect images so like what I'm talking about looping through human faces here right if I can just draw it out for you guys so let's go over here and I'm gonna draw it out but let's say that we had I'm gonna clean this up but let's say the image I gave Erin has two people in it so if you have actually two human beings here like this right what we're about to do right now which is mind blowing is for right now our code all its gonna do is draw a circle around like one person's face it's gonna decide who whether it's Erin or me and that's it but then once you write a for loop it's actually gonna make a square around Erin so that's person one and then it's gonna loop through find me right this is cause II it's gonna find me and then it's gonna make a circle around my face like a for loop that loops through human beings that is just cool like that's the power of you know being able to actually do like face detection and AI with Python or really any language like it's so freaking cool to me you know if you guys think it's cool go ahead and smash that like button but to me it's kind of mind-blowing and then whenever you're ready in just let me know got it yeah so I got the picture of me and kasi here on my desktop and instead of reading the Robert Downey jr. photo I just want to read in this photo so I just named it cause the Aaron okay and I'm gonna comment is it in the same directory it's on my desktop it's kind of slow come coming today that's fine that's why I just keep it there just to get it up and running she can you show the image first can you show the image first by itself so they don't think I sent it to you but green squares around it there we go so this is the image okay mm-hmm I wonder now I think it'll detect my face there yeah maybe we'll see so that is the image now we're using that instead yeah and from there we want to detect the Aldi faces so right now we only gonna detect one so let's run it and see who it detects first errand or Kazi you know just give it a shot or if it breaks we'll see we'll find out Hey whoa that is so cool and for that reason it probably goes I think it gets me first because it scans from top can you yeah can you can you do me a favor and change that in debt like dude this is an array of us to look in one array like I'm sorry guys excuse the language but like in one list it's two human beings can you index that array can you index it list with the one so then it puts me maybe let's take a look yeah so cuz you're second in the list so if you did yes then it would oh dude this is about to blow in frickin mind right now I'm so cool it's secret like this shirt oh if it come oh my god you did yeah maybe maybe maybe it detects my face this is so cool this is so cool I can't get over it I cannot get over it I don't care if people think of school or not I think it's so freaky it's what it's doing is it's taking the train data and it's checking for all sizes so it's starting big or starting small and then it's iterating tangled it's called a sliding window so it'll like slide the window all the way across tiny did nothing nothing nothing nothing Bing found a match then go a little bit bigger nothing there yeah that is sick it's an array with its array of three items of three human beings in a little list three human beings in a list does it make you feel yo that is so cool like my I'm blowing like Logan is like damn Paula's like insane I know all we did is copy and paste like six lines of code it's amazing it's so crazy it's six lines of code that's right people are like whoa we got somebody's like that's actually epic dude can you can you now can we loop through it and draw the squares around all of us I don't know about that man oh if my skills are up to par for that but I mean I mean I mean you know we had the loop you wrote the loop earlier yeah we got you guys so now we're just gonna have this speed loop so we have a list you know unless the creak of three phases that we detected in that image and now we just want to loop over them instead of just doing it one by one of course we could probably do this this is a sloppy way to do it well why don't we do it just for sake so this is a few like running a loop three times but just manually yeah it's just listing about you got it run it and kaboom oh man that is nice so this is just manually doing it yeah and let's make this nice and big hurl nice nice thumbnail you know Wow oh that should have been the thumbnail of the video yeah I mean Robert Downey jr. is too sexy mine are you Justin Timberlake that's true that's true that's true you're right yeah six lines of code man like that is so cool to me I even Logan is going he's like bro this is literally input in the six lines of code what the hell you'd be cool if you can change the color you know I'll get even like like label them and stuff but yeah anyways let's go back and make this actually the proper way so the way we're gonna do that is oh yeah change also yeah once you write the loop let's also like change the colors between zero and 255 it just picks randomly so then it'll be different colors oh that's fun all right yeah okay do it on the on the webcam when we're doing a live webcam it's gonna be like rainbow it's gonna be like going crazy it's gonna be pretty cool yeah yeah yeah but all right so let me just go in here so I'm gonna type it out here's the loop all right and we're just going to reply this top line this one we're gonna replace it with this okay so instead of manually getting it here and getting the first the first face in this list of faces and then assigning it we're just actually going to loop through all of them so because this is a list we can loop over it using the word in so for this tuple in this list which means it's going to iterate over everything then it's gonna have the same functionality okay so do this make sure you tab correctly so for this tuple X Y width height in face coordinates then this is unchanged okay because we had the variables exactly the same so this is actually going to work - the randomization of the colors but let's just give this a run and see what happens and I might even pick up extra stuff if it if it failed there might be even be like a fourth face somewhere no there is none but there we go you guys so this is functioning so far that loop worked yeah it did we're looping through three human beings right now or two human beings beings in one cartoon man this is so crazy to me Wow Wow same glasses this is awesome yo okay I got you with the I got you with the different colors so Aaron go all the way at the top of the file and and just do do hold on import so do from random import ran range although at the top like that yep I am so you misspelled import yep and then where you have that 255 you can you see where you have 0 comma 255 so just do ranch no no no don't do that don't do that don't do that don't do that just the 255 just wrap the 255 in the function Rand range got it well now right you could do 250 yeah 256 because it's gonna go up to but not including yeah so now it should be randomly like make the colors so now if you hit save and run it everybody should have like a different color hopefully it would be really unlucky if all three of them were regenerated it's just well this is just the green this is just the green Channel so it's gonna be different scales of green but not different colors oh you can actually just do ran ran for all of them like for all of the coordinates you could do also go back mm-hm you could do for like four zero you could do that and all three of them you could do the same thing there we go and yeah now let's run it and now it should be random colors nah guys that is so cool green damn okay awesome that would be useful for identification like if you ever actually trained if you got enough of your images enough of my images and nothing biss little shirt we could uh we could actually like train it and be like okay cause these red Aaron's blue I just can't get over though we're in a list we are in a list right now in a pipe like that okay I can't get over that all right let's keep going we know one thing I do want to try though is I feel like those colors a little bit dark but if we keep the the range in the upper half of the 256 then they won't be so dull all the time so let's actually go 128 to 256 okay oh I tried this and then I think the colors the colors will always be brighter color science look haha I mean it kind of looks it's fine okay excellent but that's fine you can remove the one yeah just so it's less confusing for them but yeah ran range is awesome right guys he's pretty cool cause II Logan goes cause II 2020 I'm in a list getting excited coronavirus got us all screwed up you know like we're getting excited about a list like oscillating alright let's thank you guys we just broke a thousand likes thank you guys so much really appreciate you guys we are team just notified us Frankie just notify he's like thousand likes on this video Frankie's pumped awesome you guys thanks for all the support we appreciate it I don't know how long we've been going for like almost an hour already right yeah longer this one this is so fun this is so fun I think everybody's pumped I think everybody's totally like what do you guys think but in my I honestly think that everybody's freaking pumped out of their minds because I think this is the coolest freaking tutorial that's out there on face detection everything else is like so boring it every time I try to learn it I go to my ball asleep wait wait till the wait till I explain actually how it works like mathematically like it's not complicated but like that'll blow your mind cuz right here we're just calling a piece of code anybody can copy and paste this but if you actually understand how it works that's the mind-blowing part so stay tuned to the end for that ok guys we're almost done so we're gonna move on to video now because actually our our image is done so we are have already gotten the image and we're displaying it here and then we did I would display here so we display the image here play the image with the faces mm-hmm and then we have the wait key of course which is just waiting until we press a key to close and once it closes then code this code completed and we are done so ok we are detecting faces on a single image but what if we wanted to text faces in a video well all the video is is a bunch of images one after another so we can actually just run this code on every single frame of an image of a video I mean whether it be a video file or our webcam stream we're gonna be using the webcam stream and that will basically give us real-time face detection so it's pretty much the same exact code with just a loop added on the outside so let's do that now ok I just got Amada I'm just gonna continue modifying this code so let's go down here and the first thing we're gonna have to do is it actually instead of capturing an image we're gonna want to want to capture the webcam so let's start from here let's comment out the image and instead let's paste in this okay so instead of calling image bead on an image file to wait to get image like this we instead are going to call CD to video capture so all video capture means is you're gonna capture a video if you put a zero in here it's gonna get your default webcam so for the person asking about your iPhone if you can make your iPhone the default cam I think the using 0 would actually work you set to like specify on your computer's instantly I went unfortunately I went and checked and OpenCV does not work with iOS but I honestly think that if you figure out how to make this work you can easily make something with JavaScript and then host it online and then people can go and use it but the most important thing is to get experience using it learn how this technology works and then it you can go and implement it in any language you want on any system absolutely so I mean I I think I might be possible like look into it maybe it won't work natively on the iPhone but you can use your iPhone camera hook it up your computer and then use your iPhone and look around and have that be in this app but the point I was saying was if you have 0 it automatically goes to your default web cam but if you put a file name in here then you could have like you know video dot mp4 or something if that was on my desktop doesn't exist well actually I do have a video here or is it was a fun game Detective face that's actually cool lol memes so let's do this oh nice I didn't know you could give it a video that is so nice and you can I was actually not gonna work as Gavin coded it all out so let's actually do this later but first step is calling video capture which allows you to get the webcam or a video file with the same function which is nice so thank you open so you need for being smart and efficient so Anna asks where's the training part the training part is on line 6 that's where the rain did the that's where we trained the data but keep it going here yeah we actually didn't train OpenCV because training takes a lot of time like we would be sitting here for anywhere from probably like half an hour to multiple days depending on what kind of data you're training so that's what we didn't do that here we used we stole the training the Train data that OpenCV already trained for us like years ago and then they just supplied supplied this machine learning network for us to use and we just downloaded that which is right here so this is actually the train data so all these numbers the negative for the negative two and all these crazy numbers if you pump an image through all these crazy numbers it just filters through and it finds the faces magically you know the same way your I can find a face when you look at a picture this is doing something remotely similar that your brain is doing but in a different like in a computer computerised way so that's what that's what this is so yes the training is happening in here in essence hope that's clear Kazi I cannot hear anybody no you're good you're good to go Keep Calm got it so let's just get this going and then actually display the webcam okay I believe we need wait key as well pretty sure we need wait key and web camera leaves let's see if this works and this is awesome we still have 685 people life this just tells us we got to be doing a lot more of this and because seems like everybody loves this stuff because most of the times by now we drop to like hundred live people watching this is awesome yeah people are loving it right yeah oh if Dunn says yeah well I said it's not gonna work cuz we need a loop so this is a little bit confusing what I so if I was if I were to do this in display what this gave us it would only give us the first frame so we're gonna have to kind of do a big chunk of logic together guys we're gonna have to cook the whole loop and get it going as one so just bear with me and and we'll get back away so once you get the the web cam here what you're gonna want to do is get a a while loop okay so this loop is going to go right here and what this does is we just want it to loop over all the frames in the video forever until the video ends or until we killed a web cam so for the web cam we just want to run forever because it's just in the there's a real time until we tell it to stop so that's why you want a wire loop running forever and then from here now we can actually go and fetch the current frame and display the current frame so let's go down and get this so this is the next call so this is actually how you would get the image out from the video you could technically run this outside the loop but then like I said it would only get the first frame and it's essentially just an image again which is kind of the same as what we did before so I'm just jumping into the loop so there is this web cam that we made okay from the video capture we call it webcam and web cam allows you to read from it so it is called a read function here and what this returns is two things to pool so the first thing returns is if the reading the frame was successful or not this is just a bully and it'll be true or false and then this is the actual image so the frame that is being read currently from the from the webcam this we actually don't need but we need we need to be there just to like for placeholder but we're never gonna use this this should always be true okay but this is all we want we just want the frame so once we have the frame now we can actually take the frame and instead of frame use the frame instead of image so before we're using image with Robert Downey jr. and everything and being cozy and all that but now we just want to actually change the image to frame because this is the image reason it's the current frame so why don't we actually do that and let's comment this out and just show the webcam in grayscale okay so we're not gonna do any detecting it let's take this paste that in here and show this okay remembered yeah so um the one thing about weight key is actually let me demonstrate it first and I think that'll be better so let me just run this and see what pops up you know there we go so as you can see this is just a single frame it's not moving because the weight key is is waiting on something and I got a little bit screwed up so actually what we need to do if I had an image though you can see it skips to the next frame so I can move around and then when I hit my spacebar and then it automatically goes to that frame because it's waiting for the key so one way to get out of this I got a force quit this because there's no way out of it before it's quit force quit Python there we go we are going to put a delay in here so weight key here if there's nothing in here it'll wait infinitely until you hit a key for it to quit or to go to the next frame like we did what if you put a number in here it'll wait this amount of milliseconds before it automatically hits a key for yourself so we can wait one millisecond then it'll auto hit a key which means each frame will be um happening every millisecond so let's try this all right and this should actually be live webcam footage in grayscale from our webcam so as you can see it's just grayscale there's no face detection yet or anything I'm just doing this and then the weak heat every millisecond is because he just keeps pressing a key every Melissa it already weight it so hold on one second so that's so sick that already has you in great skill in real-time like from Python I did not know you could do that that is so cool and what are you saying about this one millisecond thing I'm not understanding it like what is it what is it doing it's a little no no show you show you show your screen show your camera so like okay so what's happening like without it would it close with the window closed No so wait key what wait he doesn't open see via the ways it's coded the way it's implemented is it's going to wait for a key press to execute code so the code will just wait at wait key that's why it's called wait key it's waiting for a key to be press I'm just I'm just trying to understand in English like what happens if it's not there or you don't do it the right way like it just a code break like does the video stop playing does the next frame not load like just in pure English like what is it that it actually does wait key it it's tough if it's not there then it just won't work that's all okay what's great is it's like like you need you need wait key to actually display something like you cannot dis an open CV the way the way it's designed is you can don't display something if you don't call wait key okay that's it that's all that's the most important thing I care about like if I just want to use this like oh that's so the for me that's really important okay so if I don't have wait it won't isn't likely god is display yeah you're but it's not actually it is but that's the behavior that okay beautiful beautiful got it okay so yeah because if it's not here then it'll just it won't work so let's quit this yeah you've got a force quiz we'll fix that in a bit you guys will be able to like hit the cue button and it'll go quit instead of having the force quit but here if I run this then I don't think it's gonna work it'll like pop up for a second or something and then disappear yes yeah it just freezes up and there's nothing we're here yes so it gets there and then it gets stuck in an infinite loop so that's not a good idea nothing pops up you need wait key because you wanted to wait here and you want to show a frame and then wait at that frame that's what you want wanted on it okay so let's close see yeah Python not responding because if we're stuck in this infinite loop with no way to get out so it just not just bugged out all right continuing on now so let's put this back you can think of it as display but what I was saying was if it's if there's nothing in here it'll wait forever and wait for you to actually press a button for it to continue to the next iteration of the loop so if you go back to the code this is a while loop right and it's gonna run over and over and over again and each time it runs it's getting the current frame and then displaying it in grayscale to the screen okay it's good it's turning at the grayscale and then displaying it with image show right here the current frame then it waits here forever until a key is pressed so that's why the bit behavior is weird it's frozen it's the current frame and then when I hit a key then it goes to the next iteration of the loop the next one the next one and if I span it then you can see that I'm kind of like moving but then if I stop then it breaks so what wait key does is it waits for a key infinitely but if you put a number in here then it'll only wait for that many milliseconds before it automatically just continues on and goes here so that's why we're saying then I don't have to press any buttons it'll wait for one bill a second then automatically go to the next iteration aka the next frame and so we're getting one frame every millisecond I mean I mean the camera rate will be lower but that that's that's what it does so we will quit out of this again one last time force quit this and run this to get the correct behavior and there we go so we're real time every millisecond the loop is automatically restarting with the wait key because we put a 1 here you could even put like a thousand in here and then every 10 seconds it would go automatically so but we'll leave it at 1 because we want to be real time moving on from here we have the grayscale but now we want to plug in the face detection into this so just the same as before we're going to want to take the deck detect faces and of course we want to apply it to the skilled frame so instead of just displaying the grayscale image we want to find the coordinates of that and then display it display on display over it so the grayscale image this is all the same code the face coordinates are going to be using the trained face data and then a kit of course the detect multi scale and then we're giving it the grayscale image from right here just the same as the image then we need to draw the rectangle so this is the same thing again let's just steal this loop that we wrote earlier and pop it in right there and this should work as well so let's just double check image actually this won't work because image was the image before this actually needs to be frame instead okay and I think that is it it's just the frame has to change so what we're doing is we're detecting oh I see it's pulling the frame just to be clear because I got confused at this I'm sure people are gonna get sometimes confused he's pulling the frame from line 18 when webcam dot read unpacks okay the the current frame of this loop every millisecond the current frame is called frame so you want to we want to detect the face in this frame grayscale and then we want to draw the rectangle on the frame not on the Robert Downey image that we had before but on the current frame of the webcam because we have webcam now so that's how that works now let's just run this and see how it goes I think I think it's everything so of course you're going to need to force quit this again and go to terminal and why is it not working ah because I am displaying the grayscale image not the colored colored one one second I must got a book somewhere too - so yeah so the image show it's showing the grace-filled image what we want to show is actually frame okay because that's where that's the colored frame going going back to color and then we colored we put the rectangle on top of it so we want to display that instead of great scale so now it should work but knowing my luck it probably won't huh there we go and there is the ran range oh nice and the color is changing every second look at that that is so nice you can so you can see that you can see the one minute the one millisecond refresh rate that's actually pretty cool the color you can see like in real time every time the little cool every time the loop iterates it's getting my face it's not perfect though because this is actually a pretty lean algorithm there's there's more powerful algorithms but of course there's always a trade-off of speed and accuracy whenever you doing algorithms for everything in lay computer science and data science there's always multiple algorithms you can use to solve a problem this one we're using hard cascade specifically because this is the most commonly used one like this like on your little cameras digital cameras your iPhone is probably using this that's so cool that were you like real looping through on your face and like drawing like looping through and making new colors like that is just a coolest thing and we're gonna have to force quit this Wow okay and let's continue on so we can actually get rid of this stuff and keep code completed so oh I forgot one more thing so I kept having the force quit which is pretty pretty dang annoying to be honest so I threw in this thing earlier today of giving us the ability to only gotta clean up after two but being able to actually quit using a key specific key so I did I copy the right one yeah this one so if I pop this in here all this means is well first of all I screwed this up so actually when you're getting the weight you can actually capture which key was pressed on the keyboard so it's weighting freaky and then you can actually get it so if if if I if I don't press the key then this is probably like zero or null or something or none in in Python but if you press the key then it'll go into this so what I decided was I um if you press the Q key then Q for quit then it'll quit out so I looked up the ASCII characters for Q very simple you can just type in ASCII if you didn't know every every keyboard character has its own ASCII code ask you to just ASCI I don't know what it actually stands for but every every letter has its own number so if you go to Q you can see Q is 113 lowercase Q is 113 uppercase Q is 81 so 113 and 81 that's where I got these numbers from 113 to 81 so uppercase or lowercase Q will actually break out of this infinite loop and that allows us to exit the real time face detection without needing the force quit the way I was doing it all this time okay so let's give this a shot and see what happens run it and it's going good and let me hit the Q key Q connected Q let's add a bug let me let me go double check it is what is wrong oh yeah I forgot the webcam release I always forgot these things good things like especially when you're doing um memory allocation or any any kind of like streams of data you should always clean up your code afterwards so in this case you're gonna have to release so I think this might be the case let me see the yeah let's try let's try again there we go now let's hit Q I think it's lagging I think it was actually working but it's just taking a long time so actually there we go okay so what's working I think I think the color may be the random color was doing some stuff I don't know why it's just being laggy but pretty much it has the the working functionality I'm actually curious let me just try to change this and see if this is actually well what is holding it up and let's run this again go here so it's just green back to what I had before and it hit Q nope my computer's just slow I think it's just bogged down from the live streaming and everything at once I think that's why it's slow really slow anyways it'll it'll close after a few seconds not important well let's change this back to the multi colors because that was cool and that there guys is pretty much the completed real-time face detector app okay we have the ability to detect faces in images to detect faces and webcam and even video so actually let's I have a video here called lol memes that you guys have probably seen before and let's pop this in so instead of reading from the webcam we're gonna be reading a video file on by so if you give it if you give it an argument of zero it reads it from your webcam but if you give it all and if you give it a name it actually looks for that file path it's just capturing video from somewhere you just gotta tell it where to cap your video from Austin is always good so now let's try to run this and we can see it work whoa what is going on whoa that is crazy it's getting that guy a little bit you can see us not perfect I mean it's really difficult like this is a crazy video and it's already pulling it like that's insane Wow I pulled this in this video and you can see it's getting his face at the end there people are like wow it's amazing very nice awesome and then of course if you hit cube it will quit and that one happened a lot fast can you oh I see okay interesting I'm trying to think okay cool is there any other videos you have on your computer that it can pull from yeah I have this funny baby like okay let's pop see you - yep right there that's testing these earlier nice and there's no audio but here's a no my god that is so cute that is so cute I love that I love that the dad is basically like tormenting the baby by making them say stuff but yeah Logan says super impressive Erin Logan says super impressive Aaron thank you for the content absolutely we have even gotten to the interesting part yet this is the fun part but the interesting part is about to happen so this is working here again I'm gonna try capital Q this time and it should quit Wow our loop is still working and going through the random colors yeah I think I think we're giving the baby epilepsy that's what's happening here he's gone crazy you know what you can even do is like it like right next to it draw random captions like draw like random you know so like how have that people have that like speech thinking bubble next to them so we could even like exactly so you can detect faces and automatically put like speech bubbles in real-time next to it that little writes out text or more I think we could superimpose yeah we're thinking doing that we're gonna superimpose on Rick and Morty's faces over our faces but it was too hard to do that with the stream at the same time we couldn't technically figure it out the stream I mean I mean we could probably do that you guys you really you would really have to just let us know if that's something you're gonna like I mean I mean you you can go ahead I mean on geez just go ahead and smash that like button that'll be so awesome yeah Morty head on your shoulders morning boy donut our to you today people are loving it they're like this is amazing they're like I wish I could code like this I still hate you morning kiss shape log into the laughing another game already man I love Rick and Morty yeah awesome anyways that is that and that completes the app but now I mean this is a lot of hand wavy woohoo stuff we just called this detect multi scale and downloaded this file and it just kind of magically started taking faces like cool like a five-year-old could have done this which is true but which is also awesome because I mean you got this up and running with like minimal work but I do want to give you guys understanding of how the algorithm is actually doing it like how the heck is a computer looking at an image and how does it know what a face looks like and actually understand it so that's where I want to actually go to here okay so let's just look at this for now I put together a little presentation to actually transition people over into thinking like a data scientist because most data scientists are coders but not all coders are data scientists so you can be a coder you can be a front-end developer which is amazing web developer and stuff but like if if you don't actually understand like some of the underlying algorithms and stuff you you can't actually pursue anything in data science if that's what you want to do and that's what Python is known for so that's all I want to touch on it so instead of being a coder and just like copy pasting what I just I taught you now you need to actually understand what's happening behind the scenes so that in all the little tricks that are happening so that you can actually innovate on that and continue forward and actually come up with like new and faster and better face detection down the road so but you got to understand the basics before so let's continue on okay face detection so what the hell is har cascade that's the big question because if we go back to the code then all this stuff was like our cascade our cascade what like what the heck is har cascade why does it say har cascade and we just downloaded this thing well this is just an algorithm ok so har is the name of a dude who invented the algorithm and cascade is just the word cascade so cascade is like a chain of events pretty much and you're just going down like going down a funnel so what it's doing is it's like it's going down a funnel until it finds a face at the bottom of the funnel and then it's like here's one face and it keeps doing it over and over again for every face you're cascading down that's the one paragraph explanation but let's actually go to one second and real quick if you want to cue me to show your face just you know just be like hey you know camera 1 or something like that and I'll switch over to your face or you can just tell me so right now I'm I have the camera on both of us so now when you're drawing like moving your hands physical eels show and now I'll hit too and we'll go back to the screen got it yeah we'll just show me really quickly I'm explaining it so like I was saying the heart cascade it's just like a chain of machine learning things that that the image is being passed through and then it's gonna funnel it down to until it finds the exact square where there where there's a face it kind of looks it like every square of every size on the image and it passes each of those squares through this machine learning thing and then I cascades down eventually it's like okay if you pass all of the cascades and you get to the bottom it's like okay this is close enough to a face to actually be considered a face and that's why it's called cascade how it's just a name you can kind of ignore that but hopefully this makes it seem a little less scary you kind of understand but now it's time to reveal the magic trick of face detection okay it's pretty mind-blowing like honestly I wouldn't have been able to figure this out on my own like these geniuses did it but I learned it in my in my schooling at Georgia Tech when I took computer vision and I'm just gonna share it with you okay so yep time to reveal the magic trick don't be this guy I have this gif here so it's actually it's actually pretty simple how it works and I want you to understand instead of being like this guy like how the hell does faces section work I want you to just know like how it works because it actually is this simple and you can see it from the third one like once you understand it's that simple I love that that is let's continue on yes as you know the algorithm is called hard cascade so let's start with heart so what heart cascade does you can also by the way you can fullscreen this you can just hit command enter and it'll fullscreen it so everybody can see it in a better way yeah and and you can actually use a pointer so at the bottom left click pointer and then when you actually move your mouse around like people oh yeah so it's easier to know what we're what you're talking about okay see I'm just uh just a coder I don't know all this fancy Google Google slide stuff but anyways so these are our features so this is the first part of the HAR cascade algorithm it's a very very simple thing what you can think of are features as as rudimentary building blocks so this is a one hard feature this is another hard feature this isn't the heart feature this is one and this is one all this really is is a little block so what we're gonna be doing is we're gonna be placing this over a portion of an image that has a face in it or doesn't have a face in it and all it's doing is it's kind of showing the relationship from the white portion to the black portion and then what this allows us to do is it kind of allows us to approximate the relationship of these pixels within this box so if we overlay this over somebody's face then we could be like okay down here in relation to the pixels up here there's some kind of relationship and then or this way you could be like okay that pixels the left have some kind of relationship to the pixels on the right or like this you can do the three thing like okay there's there's a relationship a three-way here or a diagonal and this is like the most elementary ones that you not to go any bigger because you can just kind of chain them together but these starting blocks is what you start with and if you chain these together over and over again and you layer enough you can actually some combination of these five things will actually give you a template for a face it'll look like a face and then that's how they actually find faces by layering this over and over again and cascading all the way down these different layers and then actually finding one that matches the face and though the way it does that is you give it faces so it finds out the combination of these things to know what it faces and then you can just filter through it again so that's kind of the the idea of hard features so far so these are hard features and let's continue on to the next slide I think I'll make more sense when I show this one so this is how a hard feature is applied to an image so like these let's say these are blank images or background images that have no faces in them one hard feature is here and here's another one different a different type where it has three three columns and other one is here and as you can see this is how you would overlaid on an image this is the raw image but then if you have the heart the heart feature here what we're looking for here is oh the relationship of this black box to this white box and what's cool is if you look on this face here you can kind of tell that this rectangle here of his eyes is a little bit darker than his cheeks and his nose and you see how that corresponds to the black and the white here so we can actually say that this heart feature is closer to a face then something else like placing this light all the way on the bottom left where it's kind of like not because there's kind of like a black bar here but we're here so white what so the white line means that that is more like a face and in the black the area that's black is like that's less like a face the black is like you can think of it as like it being darker and the white is it being lighter so you're saying there's some kind of like going from light to dark in this direction or dark to light the dark and this direction so oh if you like very true also like bar going to a light bar you see right here is exactly what okay so one second so I'm so basically the face detection it's literally doesn't look for facial features is just looking for like how dark and light it usually is around faces and that's why you only need great crap that is this is only one algorithm there's like there's a bunch of different ones but this is the most well-known and most widely used like literally just looking at so for this one specifically it's just looking at like you cure a hundred different faces your Frank you bring your face in real quick so like it's like it'll like have like a certain thing for like dark and light for his face versus dark and like our thighs yeah his trains up on cheeks yeah so then it's just looking for white and black like okay okay cool like if you if we went back like this would even be a better match because you have forehead then eyes then cheeks you see it I mean if we had not ears but also have a white bar up here no I kind of see it's like white then dark then white so we're basically saying there's some kind of three layered relationship of this block of pixels and this is only one layer of horror cascades basically we're gonna layer a thousand of these little matches together and eventually those added up will add add up to a face Wow you can see the second one here you can kind of see like okay the eyes are darker and the bridge of the nose is lighter see the IM dark and the bridge of the nose is darker so then our future Plus this heart feature is even closer to what the heck of faces yeah this is what this is what Logan is saying he's like ah he goes this is why we used grayscale right Aaron and he goes because color is irrelevant we're actually just looking for brightness yes exactly well you can you can start using color when you want to get like really like if you want to like profile like race or a skin color or if somebody has freckles or not you could even start doing that if but it would of course you would need more data and longer time to Train but this is the most generic case but I want to teach you guys from the ground up so that you understand how this works because this is the prerequisite to everything else okay yeah so this is how a heart heart feature is applied to an image and of course you would just take this box and you just drag it all the way around the image you would even resize it like because again you could have like this similar thing might be like it might match on the mouth a little bit but let's move on okay so these are our features these are the five rudimentary heart heart heart features and this is how our features are applied to an image if it's like this then there's no match you know there's like it's just the white but like flat background or if it was like a landscape or like your wall or something then there's gonna probably be nothing there but let's go on there's a little bit more nuances but that's the general idea so far so now actually how did open CV train that face detector that we downloaded that XML file because that's where the magic is we just call that we just downloaded it and just like called a few lines of code which is kind of like okay cool but how do they actually do it because from here if you understand it then you can actually start thinking correctly and do some real data science stuff like you're not a data scientist unless you actually understand that algorithms so step one like we bench at the very beginning of the call you want to start out with your training data so in machine learning the the way it works is you have positive images and negative images and all these are are just labels so this would be labeled as face label as face labeled as face this would be label does not face not face not face that's all it is you just need faces labeled as faces and so just to contrast just to just a contrast that guys so here is a face right but here is like this would be a negative image right a phone would be a non face so like it's not a face so again non faces and yep cup of coffee this is a non face when it sees it this is a face when it sees it right and this was through supervised learning so it fed the data to the heart acade cascade right and it told it yo teacher all faces and these are all non faces yeah so this is yeah yep so this is called supervised learning weren't probably not gonna go too much into unsupervised supervised but like at a high level it's like a human beings telling it what is a face and what isn't and then it feeding it like lots and loss of data yeah another way it may be another way to explain if somebody didn't catch that is there's two fields of machine learning there's supervised machine learning and unsupervised machine learning which is what Kazi just said supervised means you know these are all faces and you know these are all non faces it's supervised you're supervising the actual data and you can tell it like these are faces okay that's what it faces these are not phases okay that's not what a face not is but if you're doing unsupervised you're literally just given a bunch of stuff and you have no labels and then you can just at the end be like oh was it right or wrong so you have even less data but that also is a lot better because when you're gathering photos from the real world or data from the real world usually it's not labeled you'd have to go in there and manually label it by human because how else would you know these are things so there's kind of two fields I thought supervised is better to learn first but unsupervised gets very very messy and does not like the cutting edge right yeah yeah that's where I like you really get into AI and all that like that deep nor deep neural Nets and all that stuff deep deep neural Nets awesome this is just ya machine learning so star one start out with training your data so you have a bunch of labeled images and from here you'll go to the next thing so after you have all of your images so you have face images and then also non face images you want to find the winning horror feature so like I said this heart feature matches nicely with the eyes and the cheek and then even if we had like a the other hard few adult with a lighter bar on the forehead that would even be a better match but for this one is we're just using this this is a fairly good match and this one is also a good match okay so basically what want to do is you just want to chain together all the hard features that kind of match and add them together sum them up until we get a face template these are like little Lego blocks to like doing it and this is why it works for nothing because they're rudimentary building blocks this can work for a coca-cola can you can distinguish a coca-cola can from a Pepsi can eventually because it looks different and like the the brightness and the brightness relationships within these boxes and sizes and boxes is all different that's why there's you just start with like plain old boxes and you add them up I've been saying that you can tell the difference of everything based on brightness like this is where algorithms are so freaking cool because I mean it's what your brain is doing right like what is your brain doing it's doing that that's true thing it recognizes the face versus not a face and we're just mention the computer how to do the same thing dang damn it blows you're like self-driving cars this is this is absolutely like essential like Tesla you know Tesla stock is going to off like crazy Elon Musk just pass Warren Buffett and it's net worth as of like three days ago like go are you serious protect deal oh yeah he's 70 billion Warren Buffett is 69 billion whoa he's now Elon Musk is now number seven in the world he's exploding bro but but yeah the Tesla self-driving cars they use this heavily it like with other things but there's a lot of computer vision so like you could like you could detect pedestrians whoa I am showing that on my screen Ilan's using the same heart function that is insane I just I have an ad blocker on so I have to turn that off real quick but um I'm just showing them on the screen that Elon Musk actually passed oh my god is this website annoying holy crap Jesus Christ okay I'm out of here they can they can keep their we don't need to look at that cuz this insider yeah Elon Musk just passed him officially that is insane yeah Elon Musk use my guy man like Daniel works at Blue Origin right that's like the competitor of Elon mustn't stay six yeah oh we have a guy here included programmer who works at a the competitor yeah Jeff yeah he worked for Jeff Bezos company and he works on actual rocket ship at clever he's gonna actually be here next week and he's gonna be teaching everybody algorithms and maybe even some stuff with Python yeah he makes me look like a baby awesome so keep going but yes so after we have all of the images the face images and the non face images we want to find the winning car feature so which horror features align with a face and then on the reverse for non feet on for non non hard features we want to see which hard features don't match it's like you're doing the reverse you want to make you want to find hard figures that like do the anti match and then eventually if you do enough reps then you can kind of understand the hard feature that determines a face versus not a face so then this is how it works so we have if we go back to this you have a bunch of faces and a bunch of non faces so the way the algorithm actually works is I'm actually gonna come out of here for now and can i zoom in no this is big enough though so we have to test every hard feature okay that's pretty much it there's only five but of course we can scale the size so what we want to do is you literally want to start here and check check check check check check and then go down check check check check check of course and do this across the entire thing and then again do with a smaller one check check check check check blah blah and then go to the second heart feature and the third and the fourth and fifth and you keep doing this on every single image until you can find which ones actually match your face so after um yeah every like I said every type of heart feature every size of heart feature and every location cause II there's a lot of background noise I bet there's a in every location of every image okay and each heart feature after it goes through the whole thing it'll give you a number and this number tells you whether it is a face or not a face like what this har feature will say so after this har feature goes through this entire image okay or let's make a little bit bigger like there so this is this is the good matching one so after this goes through the entire image each of these spots it's gonna get a number so the way it does this number is it takes all the pixels in the black and adds them up to one number then takes all the pixels in the white this box adds them up into one number again grayscale is useful because we don't have to deal with one number instead of RGB and from there um you have a white number and a black number then you just - them to get the difference like how far away are these numbers from each other like if this was if this was zero and this was a hundred then the difference would be 100 or this was twenty this was 70 the difference would be fifty there 50 apart from each other so this would be a fifty and we could say okay this match is fifty so this this horror feature in this location on this image has the number 50 and then we give it a threshold of saying okay if it's 50 or higher say it matches good for a face if it's fifty or lower then say it's not good for a face and so that's what we do so you say okay this hard feature goes all the way through and here and then at the end it gets a it's a it's a final number for itself and you and then you can say at the end of this you'd like okay this hard feature in this location was really good let's keep this okay this is level one this was the best performing our feature let's keep this because it was the highest like maybe it was like like closer to a hundred or something and then after you do that that was just one hard feature on one image you'd have to do this on every single image to find it because it would also find the same pattern on other people's faces kids you know their eyes are gonna be dark and their cheeks are gonna be light - because that's what a face is and then after that you could be like okay this this is the winner that's cascade one that's level one so whichever hard feature it matches the training images closest is our first winner so if you go back here it matched this one like her eyes are darker than her cheeks then you could say okay that's a match that's the first winner then we would go to the second one and go here and and then see that this is another good match because the light nose plus the dark eyes and we would do that too but of course you would have to iterate through the whole thing and then of course all the different sizes - you'd even go like tiny on every single image and all the background images - but that's how that works and eventually at the end because if you take this black segment add it to this black segment and then subtract it from this white segment you get a number at the end again and then if it's above 50 or closer to 100 that's the idea then you can say okay this is a pretty good match versus here where it would be like not a very good match and you just do this over and over again and you want to get like a thousand hearts harsh on the features that match right now we're only looking at two but you want to do this over and over again that's why computers so powerful and that's how you actually train the Cascade object detector or an our case the face detector but you can detect any object depending on this on the on the training data so that's how that works you have to test every card on our feature and then after that after you've gotten a thousand winner hard features you can take that and then put it all together into a big machine learning algorithm for face detection so that's what this diagram here is so step three this is the cascade which is just a chain of haar features a chain of heart features this was the first best match there's some second best third best fourth best all the up to a thousandth best and you would go through in order and if a picture passes all the thousand then you can definitively say hey this is a face if it only passes nine hundred it doesn't get to a thousand but like it's not good enough to be a face so that's the kind of the mark there that's how that would work so you take all the images that are faces all the images that are not faces then you train it with these Hart features you find the one thousand once oh I can be any number is you can kind of decide like when it's close enough but you shoes let's just say a thousand winter heart features in order and then we can chain them together into our face detector which is called a cascade classifier because you cascade from stage one to stage 2 to stage three all it down to stage 1000 and then that is stored as an XML file which is exactly the data that is stored in here so in here it probably has a thousand of these little things so this here is one our feature could be one heart picture like from this tag to this tag is one single heart feature that's the idea okay and then all these numbers are actually the heart feature we are looking at has the brightness and the color and the location and the size of it and you would go through and then of course there's like a thousand so you just fish the the image through here and then it looks for every single thing in the image until it finds faces so that's why it's called a cascade classifier you or a heart cascade because using hard figures and then your cascading down all the matches hard figures until you find a face ok so that's the explanation of how it actually works one final thing I want to do is the way it actually will actually continue on in the slides can you full screen it please yeah so the nice thing about that is OpenCV does the hard work for us ok OpenCV provides all the pre trained classifier data that has the chain of heart features that best match the frontal face which is exactly what we downloaded remember when we were at the github I said download har cascade frontal face default they have all of that data for us they trained it already and they have that data and then from there then after it's classified we can just pass a sliding window so a sliding window is just a window over the image or the current frame or whatever into the and pump it into this classifier and run it through all 1,000 hard Cascades and if it makes it past all 1000 it's a face if it only gets a 999 it's not a face that's the threshold and so yeah if it gets all the way to the end it's a face but if it doesn't then it's not so that's why it's called a cascade because you're like funneling down into what a face actually is one last thing let me just explain how that actually works so what you what we would be doing is you would have a have like a sliding window so you would have our let me at this full screen or actually I can actually just add a square can't i shape nice so let's just say we have a square here good question by evil name can't he said is it possible to use face detection into a flask application absolutely yeah I've never done it personally but I mean yeah you can you can tie it you can tie this into web development you take this and then you can somehow call the opencv code in your flask application I mean you would like you can use this flask like how something or how's a web page but you would need some way to get the opencv code running on your flash but it's definitely possible jingle won't be better but flats might be cleaner but look into it but it's definitely possible you just got it theater like it up that's homework it for you but the way this works is you would take this little square pass it through all a thousand horrors it's like oh no this is not a face not a face not a face not a face not a face not a face and this is so tiny this would be not a face the entire time and eventually then you would check a little bit bigger not a face not if it's not if it's not a face but eventually when you got this big you'd be like okay not a face not a face match is $900 blah matches nine 950 not quite a thousand it matches 950 but I had your fifty boom and then boom it matches a thousand and then that's when you get the face so that's how it would work and then it says ding-ding-ding that's a face and if there's multiple faces then it'll find all of them in the current frame and that's kind of the full algorithm of how the face detection actually works in practice and actually like three three with the math in numbers okay so now I want to show you the fun Romano one interesting thing Romano says you can adjust the sensitivity of face detection and he basically says that that face classifier detect multi scale thing you have the last argument is actually for sensitivity so you can actually yeah change the sensitivity are looking for mmhmm yeah you can go here detect multi scale so you will have all of these parameters you can do mid neighbors scale factor so you can say like okay how many faces can be close to each other like do the faces have to be far away you how we were getting random little schools here in there you can say oh if there's a little square next to a big square prioritize that big score and just omit those little squares to kind of clean it up so you can kind of fine tune it that's the nature of algorithms you want to fine tune things until they're working nicely but you just want to get it really close to working effectively and of course there's always a trade-off of speed and accuracy that's always the case that's just how the universe works there's always gonna be that trade-off so but yes you are correct go check it out you can play with all of these things like scale factor is 1 by 1 by 1 point 1 by default mid neighbors is 3 and it explains what all these things do and there's even other things like set image that I didn't go over there's a bunch I mean it would take forever to go over everything but I definitely look at the documentation for OpenCV but good catch that that is correct now let's go to this video so this is actually hard code visualization happening live so everything I just explained and everything we just coded this is what's happening after the hard code is trained this is actually how the algorithm goes forward ok so let's start here we have a lady here and as you can see like I said though the window will go across right and as you can see whenever the read the current read Red Square will run a bunch it through a bunch of horror features so you can see it's spamming a bunch and it's saying okay all those a thousand hard features within this Red Square did this Red Square pass through all those hard features yes or no and it's saying no no no because it's not a face obviously the face is down here so it's saying okay this box's face no is this face note is this a face no is this a face no by running through all of these crazy hard features for each of these red boxes that's why it's going so freaking fast and it'll keep doing this um at for every for every face at every size until it finds the face so let's just speed this up and we're getting close so as you can see the stage look see it says 100% match here features 16 of 135 it's matching there's 135 instead of a thousand in this case and you can see that this is what makes up a face you can see the match is pretty close only two stages so see how it seeing seeing these red squares or a match and at the end it probably averages all those red squares and the red squares to find the actual face and that's where the whole thing as well as tweaking those tweaking numbers come into play because you can be like okay if all these squares are so close together you can kind of approximate and just make it one square instead of six faces that's the same face you split math this is just one face with a bunch of the square matches Dan so this is cool it happening in real time and of course if it's multiple faces then it'll catch all of us people want to go people want to see the video they're like can you share the link of the video maybe you can just show the description yes stop gesturing no but it just hit escape for now so you can show the URL and hold on give me one second to show the URL and so you guys can cause search this up anchored Zulfiqar har cascade visualization yeah there you go and you guys can also just type in the URL at the top there you go yeah and that's that's really it you guys so that's the entire algorithm you guys coded it up you guys followed along with how it actually works mathematically why it works and how it effective it works in the efficiency and all that and the in the trade-offs and then also the extensions of it like you could do actually start like recognizing people like we could distinguish like Jeff Bezos from Elon Musk or you could distinguish most man from from coke cans and stuff like that if you if you do it enough depends how much you want to train the data Jemmy train the algorithm how much data you want to do it but as you can see this is getting pretty see how it got bigger the Red Square is getting bigger because it's a sliding window algorithm which is how most of like self-driving cars same stuff it's very very similar kind of ideal and that's really it you guys so it's getting close to the end here and boom there at the end they got all the boxes and it approximated that that is the face in the huge I love that that all of that is happening in literally milliseconds like dad Danny is insane can you open the webcam again like I want to just see it happening in real time that is crazy yeah I did omit one optimization so I didn't get into the nitty-gritty math I'm just explaining the general because the math is really simple was you like if you know one plus one you understand the math I just didn't bother because it would it gets really in-depth if you want to read it you can read the paper I'll link down the description too but let's go back to the to the code and then you can actually see it happening with the do camp for this one so you might have to go at the top and change the mp4 ah yeah yeah you're right you're right 2-0 save that and run this and get that going and Kazi text me a picture text me a picture there we go to my phone to my phone because I'm gonna pull up on my phone I'm gonna hold it up send you to pieces know what the webcam oh I see okay I'll send it to your slack right now got it okay cool I'm working a real time every single frame it's doing the sliding window thing all the way across the entire thing and it's doing it's doing the entire haar cascade of all the hard features that it was trained to within each of these squares until it finds this square at this size and finds my face and it's doing that in real time just because the computer is so dang fast so alright a bottle take on you I'm having trouble actually I got different ones so we can actually so real time we can oh wow but see there we go examples it got me I went to widen closes where's yeah it is looping through in real time that is insane it is literary sizing throw in real time every frame every size square across the entire image with all the thousands of horror functions and it's just like number crunching you know fast enough that's all it is wow it's really it's really cool and you get an appreciation once you understand how it works Aaron this is how we got a demo it next time we got to just have like you holding like three phones one with your foot and then just like showing the three phones in your face at the same time that's like mind-blowing one issue is going sideways doesn't work so it's not super robust but you can do that was also side profile like there is different like this looks different than a frontal but you could get Seattle you could combine a frontal machine learning algorithm with a with a side you can be like oh if it matches either of them then detect it you know then you can be like okay this or even a back hit you can even be like okay if it matches any like any anything and of course like different hairstyles mind might be different than a girl or a bald guy but I mean eventually you could you could really detect anything and damn fast for the it you guys face detection with open CV and Python with the entire theory and math behind it so you guys understand it through and through hope hope you guys had your curiosity spikes and maybe you're inspired to go learn how to do self-driving cars and work at Tesla because that's what I want to do no that is so exciting that was awesome dude thank you so much for that demonstration guys if you enjoyed this so far you know first of all drop it in the comments and like let us know if you enjoyed this and also go ahead and smash that like button because if you enjoyed this chances are somebody else will get value out of it somebody else will also enjoy it all you gotta do is smash that like button to get this video out to as many people as possible and if you haven't already subscribed to the channel yeah and one last thing I just want to mention again that if you do like coding and you enjoyed this and you are interested in doing this kind of stuff for a living we do have courses here at clever programmer that's what we do that's why we exist we help people make a living with coding whether it be freelance or lining full-time jobs or tutoring or coaching coding to like high schoolers and other people whatever whatever it is you want we are offering that so the link is in the description to check that out if you're interested in pursuing that and being serious about it and as a bonus as a bonus for anybody who's watching this live stream that course is taught primarily by Kazi and jacob the other Python instructors but I also have my own entire Python course the clever programmer that's worth $197 where I teach the same style my same bad jokes but basically what I do is I go to up work or freelancing platform find jobs that are actually paying more money than the course is worth then I code the project or a portion of the project that's it I just coat it and I teach you step by step the same way the same thoroughness as I did here like everything there's like no loop there's no room for like misunderstanding everything's clear and I teach you all of that in my Arin style fashions and we were gonna throw that in 197 course my course cuz we actually don't I don't think we've watched in a while I don't watch it again but we're gonna give it as a bonus to anybody who joins the Prophet if they join it if if you join it today we're gonna give that to you as a bonus yeah if you join you'll e enroll you so you if you purchase today will manually go in and give you Y my 197 course for free but today only because today's the live stream this yep and if you have you needed live that's your bonus if you guys join right now you also get one call with Aaron one-on-one okay so if you actually join within the next like 15 minutes so I just want to like because sometimes what happens is people just need a little push and a little sense of urgency and so like let this be that push because look if you're serious you want to turn this into a career and not just a hobby something where you learn these skills and then actually also can use to go out and make an income in the next 15 minutes if you join profit with Python which is right over here on this screen if you join this program you are going to also get automate with Python which is a course with Aaron so teach web scraping teach how to send text message some so basically how yeah like literally he find jobs from up work each job is worth over $500 to $3,000 each and then he codes it and he shows you exactly how to do it so not only do you know how to get those jobs but how to get paid for them and then build them so that's huge so you'll get that as a bonus and if you join in the next 15 minutes you'll also get a one on one call with Aaron alright so if you're here and you're serious about this join it all right join properly I thought yeah I even I even caught up an app I totally forgot about this but there's an app in the in my course where we caught up app that literally tracks International Space Station so like we use an API to get its coordinates and everything and we have a map when we superimpose some little spaceship thing on the map and we can track it in real time so instead of face tracking where ISS tracking on a globe in real time so that project is set yeah it's sick it's not even it's it's justice do you wanna do you wanna do you want to errand you want to take some time and pull up like some of the projects either if we have a sales page or we have the actual course and like maybe find other course the course would work because I don't know what the sales pages and yeah the projects are not downloaded I'm like I got a new laptop so that's all good just go to the course and let's see like what type of projects were actually building so we can show them the inside of the course would you guys be would you guys want to take a look at inside of the course that aaron has built its called automate with Python let us know in the chat but he's gonna be pulling it up right now yeah and then you can join just that one for 197 if you want to but I would encourage you jump in the proper word Python yeah because you get this one free and then also you get live calls it Jakob I'm not actually hosting live calls you know I prefer just to teach like this I guess I don't do like weekly live calls except for anybody wants if we can get on a one-on-one call but all right I'm gonna keep answering the questions well and engaging with people while you pull it up so summer it says you guys are doing amazing your tutorials are better than some paid ones love and respect thank you so much really appreciate you yeah seriously like our free stuff we try to make it as value-packed as possible and in our paid programs the reason why they're always premium and price so high is because so that we can give you that one-on-one and then the group coaching and help you actually land clients because if we just had everybody in there it's really hard to help everybody in a personalized setting right we can't get on a call with like the 300 people that are on this livestream even though we would love to but we really want to help you in your career and so if you guys join then you guys get on group coaching calls and if you join in the next 15 minutes you get a call with of course Aaron as well so yeah so this is the course this is profit by thon let me just give you a quick run through of this really quick with Jacob and Kazi they teach this they teach a lot of web development and like how to actually freelance and actually make an income from coding like cause II was able to scale the six figures within one or two years of him learning the code which is phenomenal I never personally reached that level I ended up doing this before reaching that but so this is here there's 50 modules in this course you can you can check it out in the link in the description nice oh there's also slacking slacking Facebook communities for it so there's always other students to help you and you can collaborate and talk what's in there and then let me go show you the automate with Python so this is my course and some cool projects in here so right here how to automate stuff with Python so here same same layout we have everything here and it's a little bit shorter a shorter course but it's it's just as cool just as fun and let me just jump to the the spacestation one because why not they're like each module is the station got it and also just make sure you go through the modules and like just highlight like each project and maybe how much it's worth you know so like the module name and I know the prices but I know they're all worth the price of the course so 200 or more so I don't know the exact but this is a web scripting one so somebody just went into web scream some yeah and this is probably worth like 300 dollars or something along those lines from up work and then you have another web scraping one right underneath it which is the PDF one yeah so there's a couple I think this was teaching one and then this was actually an upward one so it's just like scraping PDFs from a list of websites so it's like hey go to twitter.com and Google calm and give me a list of all the PDF files hosted on these sites and that's really all it is you're just scraping all of it I teach you everything there regex allows you to search for things in text email automation so this is one because a lot of small businesses want to have be able to like broadcast emails to potential customers and whatnot so a lot of cool little projects there same thing with text automation a lot of small businesses when people be able to like text people for information or customers system administration so assist system admin stuff like auto auto copying files auto editing files bulk log stuff like that very important for big enterprise stuff these are the fundamentals you can want to learn and it's all fun like I have a lot of cool projects in here and it's not boring like a lot of other of course is this that's against my against my beliefs and then this one there's some Django a little bit jingo stuff in api's and this is where we build the space station so I'll just demo that really quick and all the modules here on the left and we'll go here and let's just show it I don't know if you can hear it but all right where's though but we can definitely see your screen there we go so if you can see right there nice and what is this doing it's tracking the International Space Station in live guys literally NASA right yeah like it scrapes the NASA API guys it's using lastly I'm finding all the space stations and actually like showing it to you that is insane there we go see is blinking for a bit so that's where it was currently when I was like crying yo and then it goes in like a sine wave across the globe that's Steve gon get a sneak peak of stuff get this bonus with profitable Python yeah yeah just check it out if you want to do this stuff for a living or the guys you want to be talking to but yep I'm so awesome so you guys got what it's gonna be in there it's super value packed I'm gonna show you how to make an income again if you join in the next 15 minutes you get the call with Aaron if you guys joined by today you're gonna get automate with Python as a bonus that comes with it and outside of that you should just join profit with Python anyway because we're gonna teach you Python and we're gonna teach you how to make an income with it with that said guys anything else you want to add at the end Aaron nope only about 10 minutes left though because I can't handle too many students so only 10 minutes left for the one call with one on one call with me going going now the next ten minutes and then five minutes later other than that is there any questions in the comments because I actually haven't even been looking at the live stream talking to my cam so Elijah Elijah says Elijah says I bought the pwp course the other day can I get the automate course so because Maya yeah sure here he's on this live stream right he wants the course the other day so let's actually just enroll him in to automate even though it's a hundred and ninety seven dollar course what we're gonna do for you Elijah's we're gonna as a thank you to joining and being on this live stream we're actually gonna enroll you manually into our automated Python so you can have access to that entire course yeah man just haven't posts well how can you contact us because my support and also slack and slacked slightly and I also support he's in the course though yeah got it did you just give us just contact us and we'll get that to you today any other questions cause II in the chat so actually I found Elijah he's actually in profit with JavaScript I think and I can actually see him in slack as one of the people we have lots of members you guys so we have tons of members we have over like 1300 members just in our JavaScript course and have tons we have over 2500 members in our Python and here's Aaron right over here just wanna check out the JavaScript course that's yeah it's - if you're in JavaScript really move this cool so right there is Aaron and then you guys saw it like Jo so yeah just hit us up from slack and then we'll be able to help you no problem okay and Logan also says so Logan says let's see Logan let me say let me go justices could I also get that course I'd really appreciate if those topics are the same ones that I'm going to be making proposals for yeah Logan absolutely your urine profitable shortcut so for sure we're gonna add that on as a bonus for you yep yeah Logan is under most premium program so he's working directly with me and we're getting on one group coaching calls so yeah for sure we're gonna add that in for you all right so probably like six minutes left if anybody I don't know how many came through but I'll bet on 101 calls with anybody who joins yeah awesome I think that's it guys thank you hopefully this gave you value and taught you guys something new this was a mind-blowing experience for me hopefully it was also a mind-blowing experience for you with that said guys we love your face this is cozy and this is Aaron what's up you see me yup and that's it with that said we'll see you guys in the next video peace out guys bye
Info
Channel: Clever Programmer
Views: 217,346
Rating: undefined out of 5
Keywords:
Id: R7B8sSByZGQ
Channel Id: undefined
Length: 136min 35sec (8195 seconds)
Published: Fri Jul 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.