Building A Camera Classifier in Python

NeuralNine · Intermediate ·📐 ML Fundamentals ·5y ago

Key Takeaways

This video demonstrates building a camera classifier in Python, combining computer vision and machine learning principles. The project showcases a fully-functioning classifier using Python.

Full Transcript

[Music] what is going on guys welcome to this video in today's episode we're going to build a camera classifier which is a program that i uploaded on github a while ago already you can check it out on github.com neural nine you can see the camera classifier repository here and it's essentially just a script or an application a python application that allows you to take pictures with your webcam and to classify them into two classes so you can say for example class one is whenever you hold a green marker into the webcam and class um two is whenever you hold a coffee mug into the webcam or whatever and what you do with this tool is you essentially make a bunch of pictures for example you hold a green marker into your webcam and then you click class one class one class one all the time so you provide some sample pictures then you hold a bunch of coffee mugs into your um into your webcam and provide class two examples and then these examples are once once you have enough of them you just click on train you train them all and then the mall predicts um future uh future example so you can take new pictures after that and you can say okay predict what this is and it's going to tell you it's either a coffee mug or it's a green marker for example and i'm going to show you how that looks like what that looks like in a second we're going to run uh the program we're going to play around with it a little bit and after that we're going to talk about how to build this program from scratch so let us get right into it now in order to demonstrate the script to you i need to turn off my camera for a second here because i cannot use my camera for recording and for the script so i'm going to show you how the script works and for this i had to turn off the recording of the camera however we're just going to run the script right now so that you can see what we're going to end up with python main py this is uh the full script you can also get it on github if you don't want to code it yourself and now you just enter the name of the first class and the first class can be whatever you want it's just the first type of thing that you want to classify you can say for example pen and the second one is watch or you can say coffee mug and the second one is i don't know purse or something uh what we're going to do is we're going to have phone so we're going to show a phone to the camera or a book and the uh camera classifier shall be able to distinguish that now here we are again we see the camera here and what you can see down here is we have a bunch of buttons now auto prediction is something that we're going to talk about in a second and then we have the two classes here phone and book and phone and book are essentially the two things that we want to classify so these two buttons allow us to take some screenshots and add some samples so right now the model the whole script here doesn't know what a phone is doesn't know what a book is doesn't have any samples at all we just have this active live camera here but no pictures at all so what we're going to do is we're going to show the phone to the camera a bunch of times and click the phone button at the same time and we're going to show the book which is the magic of thinking big here uh we're going to show this book to the camera from different angles and we're going to click the book button at the same time by doing that uh the script learns what a phone is and what a book is or it essentially prepares the samples and once we click train model it's going to learn it and then we can just show um the book or the phone again and it's going to predict if this is um the book or the phone and we can also reset the whole process so let's just go ahead and do a couple of samples here i'm just going to make pictures here from off my phone from different angles you know all different sites closer farther away by the way so we have all these different positions here and now we're going to do the same thing for the book so we're just going to hold up the book from various different angles we're just providing the training data here for those of you familiar with machine learning the training data it's just the data that the model has already classified we're providing the classes here um let's just take a look at how many of them we already have we have like uh 76 of class 1 and we have 52 of class 2. so let's do a bunch of more from the book side here should be enough all right um and now what we do is we just go ahead and say train model and this might take a while and it's also freezing the camera but once it's done the model is trained we can also see a message in the console here i think no we cannot uh usually we would be able to see that however we just trained them all and now what we can do is we can hold the book into the camera and we can say predict and it's going to say it's a book now i can go ahead and hold the phone into the camera and say predict and it's going to say phone now if i don't do anything it's probably going to say phone because nothing looks more like i'm holding up a phone than a book because the book is white so if i say nothing yeah it's probably going to be phone but every time i hold up the book it's going to recognize this as the book and now we can also turn on auto prediction which is going to constantly predict so if i'm holding up the phone it's going to say phone of course and now the moment i hold up a book it recognizes it as a book right so um i don't i don't need to hold up the phone for it to say it's a phone because you know when you don't ha when you when you do something that doesn't have uh any or that the model has never seen before like holding up nothing or holding up maybe uh i don't know a watch looks probably also like a phone right uh if you're doing something like that the mall is going to be confused but the moment you hold up a book it's going to know hey this is a book right so um this is what we're going to build here and we can build this with all kinds of different objects the good thing about this is that we're not limited to phones and books we're limited to whatever training data we give to this uh to this camera classifier it's not uh limited to a certain category of items like uh 10 sorts of images 10 classes of images you can literally do everything you can say notice when i'm smiling notice when i'm sad notice when i'm angry whatever it is you can just classify these things based on examples so let's go ahead and build this thing up from scratch now i'm going to use the github code that you can find online on github github.com9 i'm going to use it as an orientation we're going to rebuild the whole thing but i'm going to skip the parts or not necessarily skip but copy paste the parts that are quite tedious and not really where there's not really a lot of logic behind that for example building up the graphical user interface designing all these buttons or it's not a lot of design work but you know creating all these buttons and so on uh is something that i'm going to copy because it's not complex it's just repetitive work but whenever it comes to camera processing uh or the classification training testing whatever we're going to build uh the code step by step we're not going to do a lot of copy pasting here we're just going to copy paste uh the the parts of the code that are really not that complex so we're going to start with the camera.py which is obviously the part where we work with the camera so we're going to import for this uh cv2 which is opencv as cv by the way if you don't have cv2 installed you just go ahead and type into your console pip install uh open cv minus python i think that's how we install opencv and then you can just import cv2 so this is what we need for the camera module and then we just create a class camera here and for this we need a constructor a constructor is essentially just the thing that gets called when we create a new instance of the camera and here we're just going to say self.camera is cv dot video capture and then we specify a number if you only have one webcam if you only have one camera like i do you always specify the zero if you have multiple you can specify the one that you're interested in or the one that you want so if you have two webcams you will have one that is zero and one at this one if you have three webcams you're going to have zero one two uh so if you only have one like most people you just use zero and otherwise you just play around with zero one two whatever um that is then essentially the camera the video capture and then you say if not self dot camera is open so essentially if for some reason we initialize this and the camera is not open because there was some error or something uh we're just going to raise the value error so we're going to say value error unable to open the camera unable to open the camera so the next thing is what you want to do when the camera gets destroyed not like destroyed but when you close the camera when you delete the object uh you also want to close off the camera so when we delete an instance of camera here of this class camera you also want to close down the video capture so we're going to say dell which is the function that gets called when we you could say it's the destructor um i'm going to say okay if self.camera is open so if it's still open even though we don't need it anymore we're just going to say self.camera.release which is the function that releases the camera closes the camera stream so to say and then the most important function here is the get frame function which is essentially just getting the next frame uh because what we want to do is we want to create camera instances and then we want to get the frames from the outside because we're building this script on an object-oriented basis we have classes and objects and i cannot directly access this camera here so i'm going to or it's at least not a good practice to access the camera directly we want to have a method that allows us to get the next frame so we're going to say def get a frame myself is already there and then we just say if self dot camera is opened we always need to check for this one otherwise we're going to encounter some errors if this is the case if we can access the camera we're just going to say return value and frame equals camera self.camera sorry cell.camera read which is essentially just getting the next frame so then we check if there is a return value if the return value is active if it is there we're just going to say return and we're going to return the tuple of the return value and we're going to return the frame but we're going to convert the color scheme because when you when you process image data with opencv you get a bgr which is a blue green red image and we want to have an rgb image which is red green blue so we just need to convert that we're going to say cb.cvt so convert color of the frame and we're going to convert it cv uh from bgr to rgb so this is what we're going to do if there is no return value we're just going to return red none and also if the camera is not opened we're just going to return none so this is the whole camera script this is the whole camera class and now we can proceed with the next part of the code so the next step is to open up an app.py file which is the main file that we're going to use this is like the central piece of the whole program we have the graphical user interface here we have the controlling functions that say train them all that say save images how to save images count how many images there already are uh call the camera everything is combined in this main uh app.py file here and for this we need to import a bunch of libraries first of all tk enter which is the library for the graphical user interface i think on this channel we have never used actually tk inter or graphical user interfaces in general so i think this might be new to a couple of people here but it's not too complex we're just going to import tk inter stk we're going to say from tk inter import simple dialog because we're going to need the simple dialogues for the initial two questions of how we want to name the individual classes uh and then we're going to say import cv2s cv then we want to say import os for the file handling you want to say import pillow p i l uh dot image and p i l dot image decay now if you don't have pillow you just go ahead into your command line and say pip install i think it's pillow to be honest i think it's just pillow if not uh you might want to google it but i'm pretty sure it's just pillow um and then you get the pil package with the image in the image dk and then we also import camera now for this you're going to get an error at least in pycharm because it doesn't recognize that there is a module named camera which is our module sometimes this is uh fixable by just you know creating an init dot py file but sometimes it just doesn't work however when you run the script there's not going to be a problem it's just a problem here because it's underlined red but this is not a an actual issue the code still works so what we're going to do here is we're going to create the app class which is the whole program basically and we're just going to create an constructor here so we're going to say def init and here we're going to pass some parameters first of all the window itself that we're going to build or use it's just tk.tk with a capital t just a constructor of a basic window uh this is the default value remember these are default parameters you can also pass your own window but if you don't pass anything you're just going to use tktk and the window title is default camera classifier but of course you can also provide your own window title if you want to and now we're just going to set up the basic things we're going to say self.window equals window we're going to say self.window title equals window title and then we're going to set up counters now counters is just an array of two values both starting at one and this is important because as you noticed when we clicked on the buttons phone or book we created new images new screenshots and they were named something like book one book two book three or uh class one class or class one one class one two and so on essentially just the name of the class followed by a number and in order to keep track of the numbers we have this array of uh two of two counters starting at one so when we take a screenshot for the first class we're going to increase this one otherwise we're going to increase this one just two basic counters so that we know how many images we have taken already then we're going to say self uh dot model equals uh something which we're going to define later on because we haven't done the model yet the machine learning model and then we also set the auto predict auto predict uh variable to false now this is just a simple indicator that tells us do we want to have auto predict on or not so do we want to have live prediction or only when we click the predict button default is only when when we click the predict button but we can of course change this as well then we also say self.camera equals camera.camera with a capital a and then we have self dot some function that we're going to define in a second so i'm going to comment that out as well which is going to initialize the whole gui so everything like buttons and so on will be called will be done in a separate function but after all these elements are there we're just going to update the gui over and over again so we're going to say self.delay equals 15 and then we will have a self.update function which we also have to write we're going to comment it out for now as well so here we're going to have a model here we're going to initialize the gui we can actually go ahead and write the function name already and init gui and then we have self.update which is uh the thing that updates the frame uh or actually the the whole canvas all the time the whole tk interior gui and then last but not least we have self.window dot attributes topmost and this is true which is essentially just um about the position of the window and then we have self.window dot main loop so this is the essential constructor and now a constructor now we can go ahead and add the functions that we need first of all we're going to um add a simple function we haven't used yet we're going to talk to call it uh toggle auto or actually what did i call it in the script toggle predict or auto predict toggle so auto predict toggle that's what we're going to call it it's a very very simple um one liner here we're just going to say self.auto predict is not self.auto predict so essentially negating what it is right now it's just toggling if it's one it's going to be zero if it's true it's going to be false if it's false because it's going to be true and so on just changing the current state um no new line at the end file okay doesn't matter and then we can go ahead and work on the initialize gui function this is where we're going to copy some code we're going to say def init gui and here we're going to copy a bunch of code because that code is very repetitive and simple now i'm still going to explain it so don't get shocked just because i add a lot of code here it's going to be uh some code but it's very simple it's always the same and i'm going to explain it to you and you can also copy it from github so don't get don't worry about that so i'm going to copy this from here right now i'm going to paste it and this is the code it's very it's a lot of code but it's very simple code so what we do in the beginning is we say self.canvas equals tk canvas we'll just create a basic canvas where we're going to have the camera we set the self.window as a parent here as the thing where we want to have this canva canvas and then we have width and height of the camera so these are just the basic attributes of the camera which we actually uh did we do that actually in the camera file no we didn't do that we need to do that as well i forgot that we need to add the self dot width um to be the self dot camera dot get and we're going to get the cv cap prop frame width and we're going to copy that and do the same thing for the height so essentially we're adjusting the height or we just have to uh to um like we have two variables here two attributes that are the height and the weight of the width of the camera sorry and then what we do is we assign them to the width and the height of the canvas we pack the canvas and then we have a bunch of buttons here so what we do here is we say button toggle auto button class one and so on these are all buttons that have certain um certain functions that they call so we have tk button every time we say self.window is where they belong to we have some text for these buttons we have a width for these buttons and then we have a command which is actually what we care about so the command is always what happens when you click the button and when you click toggle auto so the auto predict button you just call the auto predict toggle function uh here some functions here are not defined yet we're going to do this in a second when we're when we're writing the other functions then we always pack them and we align them to the center very simple so then here we get to two simple dialogues we get class name one and class name two are the result of these dialogues so we have simple dialogue ask string we say enter the name for the first class and then once the okay button is clicked we get as a result the string and we save it in class name one classroom two the same thing um and then we have button class one button class two and the text of these buttons of course is the class name so this depends on the input that the user may makes up here and uh these two buttons call a function called save for class that we have not defined yet so the safer class function uh is not written yet we're going to do that in a second but this is actually the function that we're going to call in order to um to process the image to make the screenshot to save it into the right directory and so on to label it in the right way and we use the lambda lambda here because we pass a parameter as well so nothing too complex then we have the train button the train button is just calling the mal.train model method that we also haven't done yet we need to create a third file here model.py where we have the trainmodel function as well so this is also something that we need to add and what we do is we say self.counters so we pass the amount of uh objects that we need to or that we have for training in both categories and then we have the predict button which obviously just calls the predict function we also haven't defined it and then we have the reset button which also resets everything so this is essentially what we what it is we have the label here we have uh the config of the font here and we have the packing at the end so this is just uh why i copied this because it would take a lot of time to just write everything again from scratch because it's always the same it's always some tk.button tk.button text self.window with it equals 50 and so on and just a different function so it's not too complicated the functions are what is complicated or not really complicated but where all the logic is so now we can actually go ahead and uncomment that line because the method already exists and we're going to continue by creating the other functions so we're going to start with the save for class function here save for class and here we just pass a parameter called class num representing the class number because depending on which class we're going to save for we're going to put the current camera frame into a different directory because when we click on the button class 1 or button class 2 we get this function call with either 2 or 1 and we have the directories one or two and when we click those buttons we just take a current snapshot of the camera frame and we write or we save the file and we save it into the directory either one or two depending on the class so we need to to get this class number here and we're going to do is we're just going to say red frame equals self dot camera dot uh get frame and when we do that we're going to call the function that we just wrote a couple of minutes ago this one here we're going to get this return value here the frame in the right color scheme and after that we can check if the two directories that we want to save the files into exist if not os.path dot exists we're going to check for one if this directory does not exist we're just going to create it we're going to say os.mkdir make directory um one and the same thing goes for two so if not os path dot exists to we're going to create this directory there you go now once we have done that mr colon here once we have done that we can go ahead and save the actual file so we're going to say cv.imright so writing the image and we're going to specify an f string here for for the file path so we're going to say um directory that we're interested in so class number class number is the directory name at the same time slash frame and now we want a number so we want to have frame one if it's the first image frame two if it's the second image and so on uh and because of that we have the counter list up here the counters list up here both at uh one in the beginning so we're going to just say counters um but what we're actually interested in is the counters uh for the specific class and we cannot just pass class number because class number is either one or two but we have index zero or one so what we do is we just say class numbers minus one uh what does not work counters counters oh selfhood counters sorry self that counters obviously uh we have the class class number minus one represents the index for class one it's index zero for class two it's index one and we access the counter and then just a simple dot jpeg and the at the end now instead of saving this image into uh with colors you can also go ahead and save it into grayscale which is what we're going to do because if we're not trying to distinguish too much uh the colors but only the structure of what is seen uh we can actually go ahead and ignore the colors because colors means more data and more data means slower training and so on and more complex structure uh so we're just going to save it into grayscale and in order to do this we just say cv cv.cvt color frame so we're going to pass the frame but before we pass it we're going to convert the color from cv color rgb to gray that is what we're doing here and then the image is safe but what we want to do now and this is why we need the pillow module uh is we're going to take this image open it up again resize it so that it's smaller because no one for no one needs hd images high definition high resolution images because those are too difficult to process or it takes too long to process them and it's enough to have a 150 by 150 pixels uh image in order to classify what it is i mean at least if if it's not uh too subtle of a difference okay so what we're going to do is we're going to say image equals pill for pillow um dot image dot open and we're going to open up the exact image that we just saved this one here and oh that's essentially it and then we're going to say image.thumbnail thumbnail we're going to pass a tuple of the resolution 150 150 um and we're going to use anti-aliasing so pill image anti-alias there you go nothing too fancy here and then last but not least we just say image save and we use the same file path there you go that's essentially it and last but not least for this function what we do is we increase the counter so we say self.counters class num minus one whatever we used right now and increase it by one and that's the safer class method that is called when we press the buttons for the individual classes now before we get into the actual machine learning part we're going to talk about two more functions here that are important for the image management and also for the management of the graphical user interface and the first one is the reset function which basically as it already says as the name already says resets everything back to normal to beginning which means we delete all the images we reset all the counters we reset them all that we haven't created yet but we reset them all once it's there uh everything just gets back to the beginning or set back to the beginning so what we do is we say four directory in and then we have a list one or two uh and for each of those directories that we have so if we have five classes if you want to make a script for five classes you would say one two three four five uh for each of those directories we're going to go through all the files so we're going to see for file in os dot list directory current directory because the directory is only a string but once we pass that string into the list directory function of the os module we get a list of all the files in that directory so we can go ahead and say for each file in that directory and then we can go ahead and use the join function so we can say file path equals os dot path dot join and we're going to join the uh first of all the directory name followed by the file name so this is what we're actually going to combine and once we have those two things we have the file path and then we're going to say if os dot path dot uh is file so if we have a file there at this file path if that is the case we're just going to unlink it so we're going to say os dot unlink file path which is just another word for deleting this file and now we can go ahead and reset the counter list so we're going to go ahead and say self counters equals one one because we start counting from one again and then we say self dot model equals model dot model but we're going to comment that out because we don't have the model yet and then last but not least we say self dot class label dot config equals or is text equals class now this is just resetting the bottom label which says book phone or class 1 class 2 to the class the default value that it has when we create it as you can see here default value is class when we click on reset uh provided that auto prediction is not on of course this is going to be set to class again now the second function is the update function the update function is the function that we need in order to get the current camera data live so that we always update the camera data instead of just when we click a button so it's constantly updating and remember we call this function up here so we have to uncomment that and what we're actually doing here is we're just constantly updating and updating and updating so what we're going to do here is if self.auto predict is on we're going to also make the prediction all the time so we're going to say self predict self.predict which is a function that we don't have yet because this is part of the machine learning that we're going to do in a second so for now i'm going to say pass um but let's just ignore the auto predict what we're going to do now is we're going to say red frame so the current camera frame is just going to be self.camera dot get frame and then we're going to process it if there is a return value if there is something we're just going to say okay self dot photo equals pillow dot image tk which is important because we're dealing with tk inter here photo image and then we're going to say it's just an image and this image is the pillow image come on image from an array and this array is what we get as a result from our camera the frame so what we're doing here is we're actually just getting the image from the camera frame and turning it into a tk image so that we can use it in our gui and then we're going to say self.canvas which is the main canvas at the top where the camera data is displayed we're just going to say canvas.createimage and we're going to create the image zero zero this is this is the position and the image is just going to be the photo that we just created so image equals self dot self dot photo and then we also anchor it which means that we orient it to tk dot north west so upper left so to say this is what we do and then last but not least we just have to recursively call this function over and over again given a certain delay so what we do is we say self dot window dot after and after a certain amount of milliseconds in this case the delay in this case 15 as we said in the constructor we're just going to call this function again so we're just recursively calling the update function but we're not calling it like that because that would be actually calling it we're just referring to it so that it is called after the delay amount of milliseconds a milliseconds so this is what we're doing here resetting updating and now we're going to get into the machine learning part so now we get into the actual machine learning part we're going to create a new file here model dot py and here we're going to save from sklearn dot svm import linear svc linear support vector classifier now you can feel free to use whatever model you want to use you can use k nearest neighbors you can use random force classification uh you can go ahead and build a convolutional neural network using tensorflow the process is actually the same we pre-process the data the images and we turn them into numpy arrays we pass them to the mall we fit them all and then we use the model for predictions so you can go ahead if you want to have better results you can also go ahead and use a convolutional neural network it's going to be pretty accurate you can also use color data uh but i use the linear support vector classifier because the results are decent and it doesn't need too long to train now if i was to build an actual project that will be used in production for a real purpose i would never use support vector classifier i would use a convolutional neural network but for this tutorial it doesn't matter really so we're going to use the linear support vector classifier we're also going to import a numpy snp we're going to import cv2 as cv and we're going to import pillow again so pil and then we're going to create a model class here model and we're going to define a constructor the constructor is a simple one-liner because all we need to do is initialize the model so we're going to see self.model equals linear svc constructor that's it and now we have two functions or we will have two functions we'll have the train model function and the predict function and the train model function is actually it's not really complicated but we need to pre-process the data because right now we have two directories after we have the sample we have two directories one and two um which are filled up with images and these images can be processed and uh we need to actually get these images into the script and we need to turn them into arrays into numpy erase it can be processed and can be learned by this support vector machine so we have to find a way to extract them in a good way and to classify and to to turn them into supervised into labeled data so what we're going to do here first is we're going to create an empty image list so image list equals np dot array empty array and class list equals np dot array as well come on there you go and now we're going to say 4i in range and here we're going to deal with the counters um with with the counters uh variable that we have up here now because of this of course we also need to pass it here sorry i forgot that we're going to say for i in range we're going to start at one and go up until counters index zero so for the first class and we're going to go ahead and say image equals cv dot imread i'm going to load the image and we're going to load the image one slash frame i dot jpeg and we're going to load from it everything everything in just the first dimension so just the zeroth dimension and then we're going to say image equals image dot reshape and we're going to specify a format now i'm going to use 16 800 here uh because my images get saved 150 times 112 i think that's what it was uh this is because when we resize it to 150 it wants to maintain the dimensions so this is what my images end up being for you it might be a different uh resolution uh just take a couple of samples right click them go to details and see what resolution they are and just multiply these two numbers and then you get to the number that you want to have in the reshape function and then we go ahead and say image list is just np dot append and we're just going to append the new image to the image list so we're going to put it into a list here and we append this image inside a list to the full image list is what we're going to do then we do the same for the class list so we say class list equals np append the current class list and an additional one so the list is not necessarily ordered it's not like all the samples are first class one then class two but they're mixed around depending on the order you click the buttons in uh and now we're going to do the same thing for two so we're going to copy that we're going to say counters one then two slash frame and here we're going to append two other than that it's actually the same and last but not least what we do after that is we say image list is the image list but we're going to reshape it and we're going to reshape it to counters of dimension 0 -1 plus counters index 1 -1 so the amount of images that we have and then sixteen thousand eight hundred or whatever number you specified in here so that we have a well-structured image list and then what we do is we just say self dot model dot fit and we fit the image quiz image list and as the labels we provide the class list so because we have the image and then the respective class at the same position every time so we can just go ahead pass both of them and fit the model and then you know you can just go ahead and say print model success fully trained you can also go ahead and just output a dialog using tk enter but we are just going to print it to the console that's how you train them all in our project here now the prediction method is actually quite simple we just say predict and what we're going to pass here is the frame because in this model.py file we don't have any access to the camera data we have access to the camera data in app py and also access to the model in app ui so we're going to add a predict method down here which is going to call this predict method and pass the current frame so here we already expect the frame to be given to us and we say frame equals frame one and we're going to say cv dot in right we're going to write the frame into a file we're going to say frame dot jpg and we're going to save the grayscale version of it so we're going to say cv dot rgb to gray so cd color rgb to gray and once have done that we're going to go ahead and load the image again with pill so uh image equals pill dot open actually pill image dot open and frame dot jpg and now actually you can just copy the code that we already used here oh sorry in the safer class method we're just going to copy that oh actually this is another thing uh and now we scale it down again using anti-aliasing we save it into the same file name so this is frame.jpg and uh we now scale it down and what we now need to do again is we need to open it up reshape it into sixteen thousand eight hundred pixels so we're going to say image equals cv dot imread and we're going to open just the frame.jpg file everything everything zeroth dimension or first dimension and then image equals image dot reshape 16800 or whatever the number is for you uh and then we get the prediction just by saying prediction equals self dot model dot predict come on predict and we pass the list of an image because um we have a list of images and these images again are lists of pixels so we have to pass the list of an image here this one image inside of the list here in order to make a prediction and then we just return the prediction the first prediction that we get here so this is the predict method of our model so now we're almost done with the script the last thing we need to do is we need to add the predict method to the app.py file so we have def predict uh and what we do here is we just get the frame frame equals self dot camera dot get frame and then what we do is we get the prediction so we say prediction equals self dot model dot predict by the way we can now go ahead up here and say model equals model dot model and we need to import model as well of course and then we had one more thing here so from all and then predict so i think this is it and we predict the actual frame right now so after that we get the prediction and then we can say okay if the prediction equals equals one if the prediction equals one we can go ahead and say self.class label dot config so we're going to change the text the text is going to be self dot class name one that we got in the beginning and we're going to return also the class name of self.classname1 and then if the prediction is not one but two we're going to do the same thing for class name two so like that that's essentially it and we are now actually done with the script so now we're done with the script the last thing that we need to do is we need to create a main file that executes everything so we're going to say main.py i've also turned off my camera so that we can see that it works we're going to say import app here um and we're going to define a main method this main method essentially just goes ahead and says app dot app and we have a window title and we can say neural nine camera classifier for example and then we say if name equals main that's a professional way in which you start a python script we're just going to call main there you go we can just go ahead run main and if there were no mistakes in the code which i highly doubt because that would be a wander um that would be a miracle but let's see maybe it works there you go the camera works okay so let's see if we can do the same thing right now i can just make a couple of book images here there you go and then we have the phone as well there you go so now let's see what happens when i say train model it actually trains them all models successfully trained okay now i am very curious what happens when i say predict okay we got an we got an error here so let's see what the exception here says uh we had the function called impredict 108 and app dot py line 108. our prediction self-model predictor k then we had 33 in predict here so in mole 33 convert color oh of course we need to pass the frame as well my mistake very stupid mistake however we now need to delete these directories because otherwise it's not going to work let's just rerun the script here run main and do the same thing again now it should work so book and phone there you go let's try again book book book now phone phone phone phone phone there you go and now we're going to train them all again trained and now we're going to predict it's a book it's a phone right and we can also go with auto prediction phone phone phone book book book there you go it works now i'm not sure if reset works oh yeah it works so now we should have empty directories yeah everything works fine and the program is done it works and as i said you can copy and download the code on github or maybe you followed along and wrote the program yourself right now uh whatever you did it works and we're now done with the project so that's it for today's video hope you enjoyed and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below also make sure you subscribe to this channel in order to see more future videos for free now this video today was quite long because it was an extensive project at least a little bit uh larger than the usual projects because we had multiple files and classes and all that but i hope it was not too confusing for you and you could code along uh easily if you have any questions if you have any feedback leave it in the comment section down below and other than that thank you very much for watching see you next video and bye [Music] you

Original Description

In this video we will complete an AI project that is a little big larger than usual. It is a fully-functioning camera classifier. We combine principles of computer vision with principles of machine learning. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🖥️ My Coding Setup 🖥️ ⌨️ Keyboard: http://hyperurl.co/neuralkeyboard 🖱️ Mouse: http://hyperurl.co/neuralmouse 🖥️ Monitor: http://hyperurl.co/neuralmonitor 🎙️ Microphone: http://hyperurl.co/neuralmicrophone ✏️ Drawing Tablet: http://hyperurl.co/neuraldraw 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches you how to build a fully-functioning camera classifier in Python by combining computer vision and machine learning principles. You will learn how to design and implement a machine learning pipeline for image classification. The project demonstrates the application of deep learning techniques for a real-world problem.

Key Takeaways
  1. Import necessary libraries and load the dataset
  2. Preprocess the images and split the data into training and testing sets
  3. Design and implement a convolutional neural network for image classification
  4. Train the model and evaluate its performance
  5. Test the model with new images and refine the classifier as needed
💡 Combining computer vision and machine learning principles can be used to build a fully-functioning camera classifier in Python.

Related AI Lessons

Mastering TypeScript — Understanding the TypeScript Compiler (tsc) from Scratch — Lesson 2
Learn the basics of the TypeScript compiler to write better JavaScript code
Medium · JavaScript
Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting with a simple code tweak and understand the difference between Ridge and Lasso regression
Medium · AI
Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting in machine learning models with a simple code tweak and understand the difference between Ridge and Lasso regression
Medium · Machine Learning
Stop Overfitting With Basically One Line of Code
Prevent overfitting in models with a simple code tweak, understanding the difference between Ridge and Lasso regression
Medium · Data Science
Up next
Learn Deep Learning by Hand (Beginner's Guide - Part 1)
Thu Vu
Watch →