OpenCV Face Detection with Raspberry Pi - Robotics with Python p.7
Skills:
CV Basics90%
Key Takeaways
Uses OpenCV for face detection with the Raspberry Pi in Python
Full Transcript
what is going on everybody welcome to another Raspberry Pi python Robotics and of course the go P go tutorial video in this video we're going to be talking about open CV with the Raspberry Pi so I imagine some people may have actually found this tutorial just simply searching for a way to combine open CV the Raspberry Pi and the Raspberry Pi camera module because mainly open CV is meant to work just with USB cameras but we can use the Asberry Pi camera so we'll be talking about that uh open CV is used for all sorts of image recognition uh arguably it's mostly used in C but you can also use Python for this also open CV is relatively computationally expensive but we can actually do it on the p and we can do it relatively fast it takes a little bit to detect a face but uh you'll see that it's actually it's not it's not too bad so first of all in order to work with open CV we have to to have open CV now this isn't going to be any sort of crazy in-depth open CV tutorial series I will end up putting out more open CV tutorials because open CV is really cool uh and it's just a fascinating topic to do image recognition with python but this is just a basic open CV kind of introduction as well as how to do it on the Raspberry Pi and all that but eventually we'll probably take it off the Raspberry Pi uh and do open CV with you know your ordinary webcam but also on a little bit more powerful computer so we can do more cool stuff anyway first things first you have to get open CV so you'll need uh to follow some tutorial to get open CV uh we could go through it or I could just point you to a list that lists it out so I'm just going to point you to a list that lists it out warning one of these steps is going to take about well if you have a Raspberry Pi model B or B+ it's going to take about 9 to 10 hours one of these steps I'll I'll show you which one uh if you are on a Rasberry Pi 2 it'll take about 2.5 hours obviously if you're on a really fast computer it's not going to take that much longer but uh that long at all but anyway um I'll put all these links in the description this one's really short though Mitch tech.net Rasberry Pi opencv um and this just you can run through this list of commands basically and the one that's going to take really long is going to be uh here right the this is what's going to take your longest um I usually just ran this overnight uh so if you're going to put it on a pie just run it overnight now uh I'm also going to put uh my image I think it's like I think I'm on a 16 GB SD card but I'll put that up on uh Google Drive and then that'll just be open and anybody can just come in and download it cuz if you mess up any part of this little process and then you're trying to get it to work work with your Raspberry Pi and uh the gopie go and all that kind of stuff together it can be kind of a pain if you want to do it yourself you can do it here or if you just can't figure it out you can always download from that Google Drive uh either way it's probably going to take you a while I don't know what your download speed from Google Drive is for 16 GB but anyway moving along you can either follow this instructional here or you can just download my image and mount it with uh like wind disk you know imager or whatever um or whatever you're using to Mount images so there's that the next thing uh would be uh this is the script I used or I'm going to use uh for what we're about to do and this works with the Raspberry Pi's camera module not this but as you come down it was this script here um mostly I'm just giving attribution here we'll type this out you could just copy and paste it but it's probably best well at least read the uh the comments so you know what's going on um otherwise you're probably just wasting your time copying and pasting uh the other thing is whenever you're doing any sort of object detection we're going to do faces but any sort of object detection the way that this works is you you usually go to when you train some sort of image recognition system you're going to train it or really any machine learning at all but image recognition often is actually has one more step but mainly what you're going to do is you're going to have a bunch of images say of faces like we we want to do facial recognition so we have take a bunch of images with faces and then we say hey here's a bunch of images with faces here's where they start sometimes you might say the width and the uh height but a lot of times it's just where they start um and and and you feed it like 10,000 images or something right that like that and that's how you train it and then later on you can use these uh face Cascades XML files and you can uh load those in and then the facial detection it's going to read this Cascade look for anything that's kind of closely related to anything that might be what we call a face so that's how we train it now a lot of times uh you'll also have one more step and you you'll basically show it like 10,000 pictures and you'll be like these are all faces and then you'll show it 10,000 pictures that have no faces in them uh and and then that's how you like one more step that usually goes in or oftentimes goes in moving along uh this file here this isn't really included here he might have a download here I don't know where it is but uh what you can do is I'll link to this one as well this is a um this one's coming from Intel and it's a massive um in fact let's see if this actually comes from his see if he's got a GitHub or something so I don't know where I found this one either no anyway this is the link to it it's for from Shan shant anyway um but the original source is actually int and this is their entire thing so if you do uh take this file and U you know propagate this file out just make sure you leave the license agreement there otherwise you can do what you want moving along uh the last thing I will say is I just I'll probably forget to mention this later on um but once once you finish this tutorial you should come to a sort of realization how easy it was to make a machine recognize a human face and and what we're going to do is is have our robot take an action when it detects there is a human face and that action is going to be to shoot the missile okay so so at that point you probably started thinking about your life and uh you know what it's going to be like in the next 10 and 20 years well good news is you can full facial uh recognition systems uh doing things like this with makeup and hair and all this kind of stuff so that's kind of an interesting uh offshoot I suppose of of facial recognition anti anti-facial recognition anyway uh just thought point that out so once you have open CV and all that you're ready to rumble I'm trying to think if there's anything else uh once you get that far but I think I think we're all set at this point um but if you have any problems like if you are trying to install it all on your own without downloading the image that I'll just host um post a question below I I went through the entire install installation process like twice or something so so definitely uh hopefully I'll be able to help you out cuz there's like a lot of there's like you know 15 ways that people are doing open CV installations and then some people are saying you don't even have to build and make and all that but I could not figure that one out um and then some people actually hosted the made files so and I couldn't figure out how to make those work either so I just went the oldfashioned way build and make and all that anyway uh once you get that far uh we'll move this aside again for anybody who is just now coming to uh this series um we've started this we've got basically I'm in win SCP here this is just my gooey file viewer if you're not on Windows you can also use filezilla for this purpose and then also I'm Remote Desktop in I did end up upgrading uh my uh raspian here so we're remote remote desktop in this is with xrdp and then once you install xrdp to your Raspberry Pi you can go to your start bar type in remote desktop and connect to the IP address so once you're in here uh you're ready to go ahead and get started so what we're going to do first is uh we're going to go through basically let's just take uh this file here so this is our main file that we want so again this is just coming from uh this this website here this RP RPI home. blogspot.com again link will be in the description uh take this all the way down here we'll copy that we'll just step this aside now and we're going to make a new uh file here and in fact I Pro well we can do it here it doesn't really matter I don't think maybe I can't I don't know there we go it was taking a while it'll be too tedious to make a file here I'll step it over here um so now what we're going to do is new file and I'll just call this uh I'll call this actually open CV x1. okay and then I'll paste all of this in here so what this is doing is you need I/O this is just send data back and forth Pi camera you should already have it with your Raspberry Pi CV2 I believe that would be installed in the tutorial here um if not you would want to make sure you installed that but is yeah I I think it's in this uh thing anyway uh and then numpy if you don't have numpy uh you can do uh an install for numpy like pip install numpy for example stream as the comment says makes memory stream so you wouldn't necessarily have to save the photo to a file but it can be useful if you do so you can go back and read that file and see that it detected your face and not something else otherwise you won't know uh then here this is how uh we're just defining our Raspberry Pi camera as the camera and then in here you can give any specifications you want so uh you can do something like this like camera. H flip equals true that's to flip it horizontally you can do V flip you can change um brightness all kinds of stuff we'll go ahead and leave everything uh the way it is right now though the dimensions you might want to change for the resolution there but that's fine for now uh this converts it to a numpy array that's why we need numpy that's how open CV basically works then we create the open CV image here this uh loads the face Cascade and so yeah let's go ahead and grab that file too so again this will be in the description uh but we mainly are wanting this little file here this is you can call it what you want I'm just going to call it faces. XML so I'm going to right click new file and I'm just going to call it faces. XML it opens up an empty close out of those andr a contrl + C and then crl V for paste that might take a second save it and now we should have a faces. XML in here we sure do bringing back down the file I've been working on uh that's right here we're going to give the full path that is uh just do this that's home Pi desktop and then I'm having this in go Pi Go Local and then the name of that was faces. XML so now for the face Cascade this is these are basically examples of faces okay it's we as you could see this is the file and it's this just this massive XML do document just many many tens of thousands of lines um then uh and and we'll in this tutorial we're just going to we're just trying to show simple open CV example um but definitely if you're interested in open CV all like I said I'll be creating a much more in-depth open CV tutorial series and we'll talk about how we can create our own because you can find uh like let's say you're trying to do um license plate recognition there are tons of things for license plate recognition there are tons of things for facial recognition just data sets and XML files that you can have but like let's say you're looking for I don't know um eo's chapstick okay you're probably going to have to take your own images of these EO EOS chapsticks and create your own data set so it's imperative that you understand how to create your own data set but for now we'll let let that go create uh convert the image to grayscale this is just uh the image recognition way of norm normalizing data we don't really care if you're black white well we care if you're black or white but generally uh image recognitions pretty bad if someone has a very similar facial structure between a black person and say a Hispanic person it a lot of times they will be scaled very similarly and if they have a similar looking face it's going to be identical as far as facial recognition is concerned but that's the best we have right now now there are well actually I I can't say that it's not the best we have but that is pretty much industry standard but there are some image recognition algorithms that do incorporate far greater shading uh than what we'll be doing anyway at least on our Raspberry Pi you got to keep keep in mind all the processing that's required here um so then faces basically we're looking for faces uh in that loaded Cascade file and we're just basically uh trying to detect a face if we have any similar faces we're just printing out to console hey we found this many faces hopefully it'll just be one um but I do have got a picture hanging up there and for some reason it's got like a like uh some lumps in the picture I guess it's like a poster you know and so it's not like perfectly flat for some reason it picked up a face there anyway um unless there's someone there I don't think so but anyway if there is a face the next thing that we do is we take the the X the Y so X and Y is like the top left and then we have to add the width and the height draw you know out so we take um the X the Y the width and the height of the face and we draw a rectangle around it we color it boom done and just for our viewing pleasure we don't have to have done this but just for us we are going to go ahead and save that image to result. jpg that way we can see it right we want to see see this image and see the face so uh that's open CV example one save that and it's up there now let's go ahead and open up our desktop where did I there it is and now we are ready to open up the terminal here and um I don't really think it's necessary for me to make the text huge on this one sorry guys I'm going to CD into desktop maybe oops there we go desktop Goyo local and then uh we're looking to run uh open CV example one so uh pseudo well actually proba don't need pseudo for this one so we'll just do python open CV example one and I'll run that look at my [Music] camera and hopefully it detects a face found a face we'll see if it found my face or not let's open that result d jpg up sure enough it found my face back there it's a little hard to see that uh little box there let's uh go back into the that IO I think I closed the script uh let's open that back up again by the way if you don't have python 2 there you can always go to open with and then go to programming Choose You Know python 2 and that'll that'll make sure it opens up oh we're not writing we're not modifying the script there that's why it wasn't there we can do something like this like 25 there and then come back over here reun and hopefully it detects face again sure enough found a face bring that up and now the uh face is much more obvious U with a nice much more pretty Square Okay so we've gotten that far now what well what we could do is something a little more devious so we could do something like this like we could take this duplicate uh and this will be open cvore ex1 or ex2 Pi uh I'm not sure why it's telling me that but we'll try again interesting duplicate and we'll call this open CV ex2 yes cool hurried up okay now this is basically identical to the first one now what we're looking for is what fired I think tutorial five fires at us right okay so we've got these scripts here and uh I want this one move this over and I'm looking for this one so what we're going to want to do now you have to bear with me here this will be kind of a a sloppy cut and paste uh but we're going to bring in we don't need uh tkinter really here but we do want the go and then just for for distribution purposes I'll take this here paste that up at top just so everyone's getting their attribution and uh then we'll copy all this basically let's just copy let's just copy this whole thing except for the setup down here so copy that we'll come over here paste let just make sure we didn't open import go go twice no okay then what we'll do is we set up USB cool so basically we want all this we'll copy that and we can just [Music] bring uh let's do just put that right here paste we don't need to zero set up you is B that's fine stop it from doing anything cool and then what we're going to do is remember up here this is where we've found how we how all we have to do to fire Rockets is run this command so we can copy that come on down here and then where this is where we're asking about you know were there any faces found so we can say if Len faces greater than zero fire right so it's that easy to uh to end the humans so we can close this and that should be it now I'm going to go ahead this this process can sometimes take a second so I'm just going to say raw input and press enter to continue and that it'll just force us to press the enter key uh so we have control over our demise I suppose and that's that so now we're going to go back to the desktop here um and into our our terminal and now instead of C or E example one we'll run example two hopefully without oh we probably have to do sudo though maybe not looks like oh yeah so pseudo python opencv ex2 dopy we'll run that hopefully we'll make our connection press enter to continue so it was it was nice knowing everybody here found a b oh I've been shot okay uh and let's just make sure it did find us but I can tell with the little thumbnail that it did find us indeed um but here's your proof rip anyway um okay so that was kind of a long tutorial uh 20 minutes but actually for you that was like a 10hour tutorial because you had to download open CV but uh it's really rewarding I think when you finally get it and you draw that square around the face that's really really cool um so hopefully everyone is able to find their way to that spot uh if you have any trouble getting to this point do feel free to let me know Raspberry Pi is not the fastest processor in the world but it's actually very impressive that we can do this with the Raspberry Pi and that camera module but like I said uh if you're interested in more open CV definitely stay tuned um I'm going to be doing an open CV tutorial uh we won't be doing it with the raspberry piie just simply because the Raspberry Pi is not the fastest device ever uh but if you're interested in this kind of thing definitely uh stay tuned and keep your eyes peeled for that one I'll probably annotate this video if and when that series does come out and if I remember anyways uh questions or comments below if you're having trouble getting anywhere to this point let me know um if I forget to put links in the description let me know if I forget to host the image let me know because if you can't figure any of this out you can always just download the image okay so anyway uh questions comments concerns whatever issues leave below otherwise as always thanks for watching thanks for all the support subscriptions and until next time
Original Description
Next, we're going to touch on using OpenCV with the Raspberry Pi's camera, giving our robot the gift of sight. There are many steps involved to this process, so there's a lot that is about to be thrown your way. If at any point you're stuck/lost/whatever, feel free to ask questions on the video and I will try to help where possible. There are a lot of moving parts here. If all else fails, I have hosted my Raspberry Pi image: https://drive.google.com/file/d/0B11p78NlrG-vZzdJLWYxcU5iMXM/view?usp=sharing
OpenCV stands for Open Computer Vision, and it is an open source computer vision and machine learning library. To start, you will need to get OpenCV on to your Raspberry Pi.
http://mitchtech.net/raspberry-pi-opencv/
Keep in mind, the "make" part of this tutorial will take 9-10 hours on a Raspberry Pi Model B+. The Raspberry Pi 2 will do it in more like 2-4 hours. Either way, it will take a while. I just did it overnight one night.
Text-based version and sample code: http://pythonprogramming.net/raspberry-pi-camera-opencv-face-detection-tutorial/
http://pythonprogramming.net
https://twitter.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
AI 3D Object Reconstruction for Crime Scenes
Medium · AI
What Is YOLOv8? An Introduction to the YOLOv8 Model Family
Medium · AI
What Is YOLOv8? An Introduction to the YOLOv8 Model Family
Medium · Data Science
Mistral's 8B Robostral Navigate outperforms multi-sensor robots
Dev.to · ironbyte-rgb
🎓
Tutor Explanation
DeepCamp AI