Haar Cascade Object Detection Face & Eye - OpenCV with Python for Image and Video Analysis 16

sentdex · Beginner ·👁️ Computer Vision ·10y ago
Skills: CV Basics90%

Key Takeaways

Implements Haar Cascade object detection for face and eye recognition using OpenCV and Python

Full Transcript

what's going on everybody welcome to another open CV with Python tutorial in this tutorial we're going to be talking about object detection like face detection or eye detection with open CV and python using hard Cascades so um to start first we're going to just use some horror Cascades uh in this tutorial and I'll show you how they work and then in the next tutorial will'll be quite a doozy uh we'll talk about how to create hard Cascades I'm going to try to get it all in one video hopefully it won't take too long but anyway uh onto this video I'll put a link to this in the description if I forget I really probably will forget to put a link to this one in the description uh let me know there will be a link to the tutorial in the description as well and then this link would be in that tutorial now this is just a quick um kind of dump of uh various Cascades and the way that these work these are hard Cascades and the way that they work is they are massive XML files with a lot of feature sets and these feature sets correspond to a very specific type of object in our case we might be looking for a full body this is a left eye so if you had like a like a some sort of biometric scanner you could scan for like the you could find the left eye and then begin the scan um anyway you got bodies you got faces youve got lower bodies you've got a profile face like a profile picture probably or something right eye Russian plates there's also probably an American Plate somewhere around here yeah well I don't know anyway um so you you can find most of these and even if you can't find it in this little tiny list you can find horror Cascades for all kinds of things like faces Smiles cars license plates trying to think of some other really common ones but stuff like that you can just find but a lot of times you might have an object you want to track that you can't find and that's what we'll talk about next time till then we'll grab the I and let's get uh the frontal face default so these two files go ahead and and and take these files download them uh just take note these are licensed to Intel so make sure you understand the license before you go using these you know commercially and stuff so uh take these two Cascades download them put them in your um and to download them you can probably you just go to Raw right click save as okay um save them to where you're working both of these and then you're ready to go and here I'm going to show you how how they work so um to start you're going to want to have numpy MP and CV2 imported and now we're going to define the two Cascades we're going to have a face Cascade equals um and this will just be be the loading of that Cascade so it'll be CV2 Cascade classifier classifier and then we load in that Cascade which was har Cascade unor frontal frontal uncore or actually it's not underscore frontal phore default. XML HTML anyway uh face Cascade was that and then now we're going to say the I _ Cascade and that file uh was let's see horror Cascade i. XML so uh those are your two the two um Cascades that you want let me make sure that horor Cascade was actually called yeah just horror Cascade uncore I all right so those are your two Cascades they're loaded in now let's go ahead and uh load in our our webcam so cap equals c t2. video capture and as usual from the rest of this series you'll probably use a zero to bring in your camera feed I'm going to use a one because my first camera is being used so um there's your capture and then now what we're going to do is we're going to say wow true what do we want to do we're going to say R image equals cap. read we're going to convert it to gray immediately so we're going to say gray equals CV2 convert color uh image CV2 do colore bg2 gray now we're going to say the faces here are going to be equal to face cascade. detect multi scale and then we're going to detect on Gray and then we're going to say we're going to use 1.3 and five we can talk about these probably later just you can use use these basically it's going to be um depending on the size of the image you use and the likelihood that you might find it in your image you would you would change these up but we're going to just use these figures for now and we'll in the next tutorial we'll probably get far more in depth into that then so faces will be found and then we're going to say for XY width height in faces we want to just draw rectangle so we're going to say CV2 do rectangle we'll draw it on the image the starting point will be XY the um the ending point of the rectangle will be x + W and then y + H so to draw a rectangle again you've got the starting point then you've got the ending point okay and so this is just how we're calculating those points we're going to make this a blue rectangle so 255 0 and the line width will be two and then um we will we probably get away with just drawing just that but what's going to happen is in faces you probably want to do like you don't want to find eyes outside of a face right that's that's bad news you know but but even even if even thinking about it um an eye will never be out of a face unless it's an eye ball and in that case the features that determine an eye are not going to recognize an eyeball most likely because what determines an eye like your eyebrows will will come into effect your eye lids and your lashes and all that kind of stuff so uh morbidity aside you're not going to find eyes outside of the face so um so what we will find eyes hopefully within the region of the image that the face is in in so we're going to Define Roi gray is going to be equal to Gray and then basically that the location of the face so we're going to say y col y + H and then X colon X+ W so this is just that region it's that starting point and then the ending point and then the starting point again and that ending point and and with region of image it's you know y than an X not XY so uh we've got that and then Roi color for later on when we reimpose it basically it is pretty much this exact thing only image not gray image okay then eyes will be equal to probably don't need that extra space eyes will be equal to the iore cascade. detect um multi scale and we're going to detect against um the ROI gray for our eyes and then we'll just use defaults for now so we could in theory we might even get away with defaults in the faces but we might find too many faces I don't know we'll see um and then four e ex e y e w so iix i y i width and height uh in eyes we're going to now just simply at this point we just have to draw it so um we will do CV2 rectangle we're going to draw it on the ROI color and uh then it starts at ex e and stretches out to ex plus ew and then e y plus a eh don't do a anyway and then the color we'll do is uh 0 255 0 and then the line width will be a two and then outside of this silly for Loop let's do CV2 mow image and image and then we're going to say k equal CV2 we key3 and 0xf oops FF IFK equals 27 break this is just so we can press the Escape key cap. relase when that happens and then CV2 do destroy all windows and let's run this bad boy syntax error ah I did this the last time why do I keep doing that anyway okay so uh we've got this camera running now I have to look look at it but you're probably my my mouth get let me move this over actually dang it uh I can't really move it and then you see it cuz it's hanging over anyway um so like your eyes will be detected and hopefully your face is detected now if I take off my glasses it probably works a whole lot better so uh the glasses are just going to cause problems um just because there like one more thing on your face and then my mouth is apparently also an eye um with this Cascade so anyway uh there's that um but I think that's pretty cool that you can detect a face and as at least for me um it is extremely efficient for object detection uh it does not take much um Computing to do it right it it's going to use like the hor Cascade itself is very small uh let let's see the the Cascade here the face is pretty big because it's hard to find faces but so it's but it's not even that big right it's 99 kilobytes okay so um so yeah uh it doesn't take much memory to really load those into RAM and be like looking for those so that's pretty cool like it's just so they're just so small yet they're so powerful for detecting things so um naturally uh you you would be in blind to want to make your own Cascades cuz chances are maybe you want to track some something else make maybe you want to track um a a phone or glasses or a microphone or just anything like how would you even get started tracking something well it turns out you can make your own hor Cascades but hor Cascade creation can be hard anyway um if you I appreciate it so that's what we're going to be talking about in the next tutorial is how we can go about creating our very own horror Cascades which is actually quite the process but you can get through it it can be done and once you do that you can track any object like that is so cool so that's what we're going to be talking about in the next tutorial if you have questions comment concern whatever oh you know what actually before I kick you guys off let me just try the defaults on this face detection um that's not that bad it finds a face and it's not f faces all over my house um but later on when we do our own cat well I just found some weird faces there when we do our own uh hard Cascade classifier uh you'll see that it's very important that you throw something in there and it's probably because our Cascade is not going to be the greatest because to make really nice hard Cascades you need a lot of ram a lot of CPU and a lot of time um none of which we really going to have in like a short tutorial so uh ours won't be the greatest so we've got to have some some stringent rules on it anyway questions comments leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

In this OpenCV with Python tutorial, we're going to discuss object detection with Haar Cascades. We'll do face and eye detection to start. In order to do object recognition/detection with cascade files, you first need cascade files. For the extremely popular tasks, these already exist. Detecting things like faces, cars, smiles, eyes, and license plates for example are all pretty prevalent. First, I will show you how to use these cascade files, then I will show you how to embark on creating your very own cascades, so that you can detect any object you want, which is pretty darn cool! You can use Google to find various Haar Cascades of things you may want to detect. You shouldn't have too much trouble finding the aforementioned types. We will use a Face cascade and Eye cascade. You can find a few more at the root directory of Haar cascades. Note the license for using/distributing these Haar Cascades. text-based tutorial and sample code: https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ https://pythonprogramming.net https://twitter.com/sentdex https://www.facebook.com/pythonprogramming.net/ https://plus.google.com/+sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related Reads

Up next
9-Phase Computer Vision Roadmap 2026 | AI & Deep Learning | #shorts
SCALER
Watch →