Automatic number plate recognition with Python, Yolov8 Tesseract OCR | computer vision

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends and welcome to YouTube channel Freedom te and in this session what we are going to learn in this session we are going to create our own custom object detection model with the help of YOLO and Google collab which detect a license number plate and then with the help of tasa OCR we are going to read a number plate which we detect and we are going to save the number plate in simple text file and and with current date and current time so before we move to our practical friends if you learn something from our videos please consider to subscribe our Channel okay friends thank you so much and let's get started so friends as I mentioned today we are going to create our own custom object detection model which detect a number plate a license plate and then with the help of T tasak OCR we are going to read our number plate and then we are going to save a number plate inside text file with current date and current time when we detect a number plate in selected region of Interest area so for that first we are going to install our basic packages so just open Tony python IDE go to the tools manage packages and search for open CV hyphen python hit enter and first package click on install button it will install open CV hyphen python then of course we need ultral litics package for YOLO V8 so search for Ultra litics hit enter first package click on install button and then it will install ultral litics package then we need CV Zone package so search for CV Zone package first package click on install button that's it it will install CV Zone package so this is how we need to install open CV hyphen python then ultral for YOLO and then CV Zone done so we have done with our basic installation so now first as I mentioned we are going to create our own custom model so we need our Google collab file also we need some project file so I have create here new repository I will mention the link inside description box you need to Simply copy paste the link inside the browser so the GitHub repository name is car number plate go to the code click on download zip and it will download our ZIP format repository inside that repository we have our all the files which we want for this project so car number plate just open downloads and this is what our repository right click vinar extract as a folder name and then as you can see this is what our repository car number plate hyphen men just open it and first let's just watch watch our video file so my carplate mp4 this is our video file so as you can see this is what our video file and here it is as you can see we can clearly see here the number plate then we have next car we have number plate license plate then we have next car then we have a license plate next X car license plate is there so our goal is to detect a license plate with the help of yate for that we are going to create our own custom model which detect a license plate a license plate simple so first we need images so for images we are going to use here our video file simple so inside inside a repos I have already mentioned IMG dopy so this IMG dopy basically capture images from our video file so first we want to create here our images folder so inside repository as you can see right now I am inside repository so just right click new folder and create the folder name images then now just right click to the images folder and copy as a path because we want to mention the this images folder path inside our IMG dopy code so just open IMG dop and maximum frames we are going to mention for this project 100 100 images is enough for this project so maximum frames 100 and then the video file name which I have already mentioned as you can see my carplate mp4 this is what our video file and here we want to change the folder path so from images just remove all the the things just remove it and press control+ V it will copy paste our new folder path then remove double Cod from images and also over here as you can see there is a two double code so we only want single one so just remove it that's it so we have done and of course we are going to save images the name of images is basically number plate and the extension is jpg so we are going to save number plate. jpg inside our images folder and how many number of images we are going to save 100 100 images inside our images folder and name of images is basically number plate. jpg is extension that's it and we are going to use our video file so just save it and run the code now it will capture all the images and it will save images inside our images folder so if you open our repository and inside images as you can see now we have all 99 images over here 99 as you can see here it is so we have all our images frame by frame so now we have done with images now we need our label IMG software for training purpose so so for label IMG just open Microsoft store because label IMG is only work with python 3.8 version so just open Microsoft Store search for python hit enter and then you will see python 3.8 version just click on install button it will install python 3.8 on Windows machine that's it so I have already done with python 3.8 installation just click on install button done it will automatically install python 3.8 now we want to select our version python version for our Tony python ID so just go to the Run config interpreter and then here as you can see this is what our by default python version setup.exe just select it and then you will see we have python 3.8 exe as you can see this one line python 3.8 exe so just select it click on okay and inside shell as you can see the version is now changed so now we are on python 3.8.1 version done now go to the tools manage packages and simply we are going to search for label IMG so label IMG hit enter label IMG first package click on install button that's it it will install label IMG so we have done with label IMG installation so now simply go to the tools again open system shell and here run the command label IMG label IMG just hit enter it will open label IMG software like this way open DI downloads our repository same report again images select folder change save di downloads repository again same repository select images folder select folder done now friends here what we are going to do because our goal is to detect only the license plate or we can say only the number plate so if there is no number plate in frame we are going to Simply skip all those images because we have IMG delete. Pi if I open our repository here it is as you can see IMG delete yellow. Pi so this code will delete all those images which we are going to skip so no problem if there is no license plate in frame or you can say in image simply skip those images so so this is the first image and here it is as you can see we cannot see a li plate so simply we are going to skip first image so just go for next image and now as you can see we can clearly see a li plate so just select here YOLO because we want to train our images for YOLO so just select it yolo and then click on create rectangle box and draw the rectangle box on lense plate like this way from here try and label IMG mention the label number plate number plate or you can simply mention license plate so number plate I have mentioned the number plate so this is what my class number plate click on Okay click on save button and then if you open our repository again and images and inside that as you can see now we have number plate 1. TX so this is what first image and they they are basically rectangle points which we draw with the help of label IMG software simple so we have done with first image go for next image same way create rectangle box draw the rectangle box on the plate number plate is label is already there just click on okay button click on Save go for next image create rectangle box same way draw the rectangle box click on okay save same way we want to draw a rectangle box on each image so now here it is as you can see now we cannot clearly see the number plate so skip this image just skip it there is no need to draw a rectangle box on which the image which you skip there is no need to draw a rectangle just skip it next image again next image as you can see now there is no car there is no license plate just go for next next next next next next next next next next and here it is as you can see now we can clearly see a license plate so just create rectangle box and draw the rectangle box and just click on okay and save it so this is how you need to draw a rectangle box each image where you see the license plate if there is no license plate just click on next image just click on next image and Skip images because we are going to delete all those images so let me draw a rectangle on each image where we have a license plate and then I will start video again so friends I have done with drawing rectangle on each license plate as you can see I have skipped those frames where there is no license plate in image so this image I have skip if I go back previous image as you can see there is no rectangle because there is no license plate so I have just skip all those images so here it is as you can see there is no license plate go previous previous image and here it is as you can see we clearly see the license plate so I have draw a rectangle previous image there is still we can see a lense plate so there is a rectangle previous image previous image previous image and and then here it is as you can see there is no rectangle on this image if I go back back as you can see now there is a rectangle so as I mentioned you only want to draw a rectangle where you clearly see the liyson plate and the numbers that's it so we have done with drawing rectangle on lyson plate with the help of label IMG software so now just close it close all the things and here now again we want to go with our by default python versions so just go to the Run config interpreter and here select the first one python.exe select it click on okay and now again we are back with python 3.10 version now here simply we want to open our IMG delete dopy so go to the file click on open downloads car number plate Main same repository and IMG delete yellow. piy and here as you can see see we have our images folder to so it will delete all those images which we skip so just click on it and then done it's automatic delete all those images it automatic deleted that's it so we have done with label mg software now simply we want to create our freedom Tech folder and then we are going to start our training so open again downloads then our repository same folder and here we are going to create a new folder so folder freedom take hit enter and inside that we are going to create again new folder which is images and labels images and labels folder and then go inside images folder again create new folder which is validation and training so training training just a minute there is a type of mistake training and then we are going to create our new folder which is validation training and validation go back again inside labels create new folder which is same way training and validation training and validation validation now simply go back go back and go inside images folder and just press control+ a copy it and go back again and go to the freedom Tech folder images training just paste it paste all the data and then validation paste it paste all data go back go to the label same way validation paste all data go back training paste all data so this is how we need to copy paste all data from images folder to labels images training validation done now simply we are going to convert our freedom Tech folder as a zip file because we want to move our file on our Google Drive so right click vrar at archive. Z click on okay and it will convert our folder IN Zip format file now just open Google Drive uh this is what Google Drive you need to Simply open your Google Drive click on new file upload Freedom tech. zip click on open that's it it will upload our file on Google Drive meanwhile we are going to Simply upload our collab file so click on upload browse and inside that repository I have mention collap file for training purpose click on open and it will upload our collap file on Google collab so friends we have successfully upload our collab file go to run time then change runtime type Python 3 T4 GPU click on Save and click on connect and friends as you can see we have successfully connected now simply we are going to run our sell one by one so first sale import o just click on play button done now we are going to install our packages ultral DI package so just click on play button button on second cell done now just scroll down and we are going to import our packages done scroll out now we have done with package installation so we are going to cross check our installation means we are going to cross check our detection with the help of yate for that we are going to use do. jpeg image from Robo flow it will detect object from this image then and we have here one person one car and one do now simply we are going to mount our Google Drive so we have upload our file successfully now we are ready and just click on the next cell play button connect to Google Drive select your Gmail ID continue scroll down click on continue button done now it will Mount over Google Drive done now simply run our next Cale it will create a new data set folder and inside that it will unzip all our data as you can see we have our images now just open just click on first this simple folder logo or we can say simple folder image then go to the data set folder and here we have freedom Tech folder as you can see inside that we have images and labels folder now here we want to create data. yl file so just click on here click on new file mention the name data. yl data. yml then just hit enter double click to the file it will open the file like this way it is now completely empty so we want to mention our code so open the repository and data.txt this is simple text file inside that we have our code so just copy it open data. yl and just press control+ V and here names means the class name of course our class name is number plate so remove suring and mention number plate and number of classes of course we are going to train only one class so NC is one that's it done only change the class name number plate then press c control+ s save it and then we are ready and we are going to start our training so just click on next sell play button so friends as you can see our trading process is started and it will started our aox 1 out of 100 so it will take some time meanwhile I will pause video so friends as you can see we have done with training process and our model is ready and it's save inside run runs detect and train folder so if you go here runs detect and train then we have WS folder and then we have p. PT just click on it click on download and it will download our model so friends as you can see we have download our model now if you open downloads folder and this is what our train model right click cut and move the model inside our repository and then just paste it now we have done now simply we want to open our main file but first we want to create our Coco 1.txt file which is our custom label file so right click new and then text document and mention the name Coco 1 txt the txt is already there extension so Coco One open it and mention the class name which is number plate number plate done file save it close it now we are ready and I have mentioned the full code inside main one. Pi this is what the python full code where we have detection and then we are going to as I mentioned we are going to use a p aser act and with the help of p as act we are going to detect a text a number plate text simple so we are ready but first uh we want to install here our P taser act so for p t act inside repository I have mentioned a file a normal text file which is web. txt as you can see web. txt and the inside that I have mention the link so just copy the link and open the browser and the paste the link inside the browser and then it will open this repository as you can see home taser at UB so this is the repository where we have our treasure act installer for Windows as you can see treasure installer for Windows so just open the text file web. txt form repository copy and paste the link inside the browser that's it now this is what my 64bit version uh window 11 so here it is as you can see Tas OCR w64 setup simple for 64bit version so just click on it and it will download our setup.exe file as you can see it's now downloading so friends as you can see we have successfully download our setup.exe file so just open downloads and this is what our setup.exe file so just click on it and now as you can see it's Opening Our setup.exe file so install our language of course English click on Okay click on next click on I agree install for anyone using this computer click on next next and then friends the destination of folder means where it will install so this is the path this is the path so we want to copy this path because we need this path so just copy as it is just copy it copy it and open new tab and go to the downloads and open our repository because we need this path so we want to save save the path so just create a new text document uh text document you can mention any name I have mentioned 1.txt and just save the path save the path over here because we need this path so click on file click on save it done we save our path now just open setup uh file and just click on next click on install and it will install our P taser act it will install taser act so we have done click on next finish setup so we have done installation now for taser rack we need to mention the path we need to mention for that we copy paste the path so how to mention the path in system so for that simply go here in search bar and here we want to search for advance advance and then as you can see we basically want to search for view advance system setting so just search for Advance view Advan system setting and just then just click on it it will open our window like this way this is the window system properties and then here we want to go inside environment variables environment variables as you can see this one tab environment variables just click on it and then again you will see the new window environmental variables or you can say environment meant variables and here we want to go inside system variables in system variables we have path as you can see we have path so in system variables just select the path this one path and then you will see this when we select there is a blue color as you can see because if if I select OS then there is a blue color so now we want to select a path so just select it path and click on edit and then you will see this kind of new window again edit environment variables so here we want to add our path this path which we copy paste click on new and then here just copy the path which we paste inside our new txt new txt file basically so just copy the path open edit environment variable and then click on new and then just press here as you can see here press cr+ V it will paste our path and then click on Okay click on okay then click on okay and just close it done now we mention our software path in in basically system variabl that's it done now we are ready and simply we are going to open our main. PI so as you can see I already open main 1. pi and here I have mentioned p.p CMD the path where we have a setup.exe file here it is as you can see in C program files we have taser folder and inside that we have taser .exe so just copy paste the path no worry about the path because I have already mentioned the path inside the code but for cross checking let's just open program files taser OCR and we are going to cross check if there is a taser x.exe file so so just open our file manager go to the C and then we want to go inside program files so go to the program files and here we have taser OCR as you can see taser OCR and here we want to search for the setup.exe file so just if I scroll down here and then our taser act. exe our file name is task. EXE so let me cross check taser dot as you can see this one taser .exe so this is the path this is the path which I have mentioned here inside the code if you not mention the path the code will not work means the P taser act software will not work so no worry because I have already mention all these things inside main 1. Pi only you have to download the setup. x file which I have already show you download the setup.exe file copy paste the path go to the system variables environmental variables and mention the path which we copy paste simple that's it done now we are ready I have mentioned the model best. PT also I have mentioned our video name video file name and then we have our Coco one.txt only because I have already mentioned all the code so I have comment some lines because we are going to start with basic code only we are going to start with detection then we are going to draw a region of Interest area then we are going to use our bite as a rag and further things but let's just start with only detection so for that I have mentioned here detection code we have rectangle when we detect our number plate it uh it will basically uh highlight with the rectangle as you always do and this is the basic code if you don't know how to use YOLO V8 Python and open CV I have already create a playlist Watch Playlist you will get the idea how to use YOLO V8 with python and open CV so basic code is there now we are ready and what we are going to do we are going to Simply freeze our frame let's just mention Zero Save the code and if I run the code and as you can see this is what our first car so because we uh mentioned here zero means we want to skip the frame manually so just press space button press it and as you can see there is a green color rectangle let me make a two uh thickness rectangle thickness two save the code run the code and now as you can see if I skip the frame there is a green color rectangle on our number plate just skip the frame it's still detecting still detecting this is how we create our model this is how we create our model we only want to detect a number plate a license plate there is a next car when there is a license plate in frame it will detect with the help of our custom object detection model and there is a green color rectangle as you can see there is a green color rectangle simple so we successfully detecting a license plate with the help of our own customer object detection model done now as I mentioned we only want a rectangle object means we only want here a crop object so for that we are going to crop our rectangle so here here it is as you can see this line is for cropping only rectangle detection frame detected frame we can can see so just mention like this way I have already mentioned as I mentioned crop is just a variable and we are going to crop our rectangle coordinates so how do we crop y1 colon Y2 y1 colon Y2 X1 colon X2 and it will only uh crop the rectangle size image means it will crop what a rectangle detect means here the rectangle is detecting basically our license plate so it will only crop the license plate so crop is equal to frame and as I mention y1 colon Y2 and X1 colon X2 as simple as now simply we want to show our crop image so for that here it is as you can see I have mentioned C2 do IMO so just uncomment now if I save the code if I run the code now we have two frame which is first one is is main frame and another one is basically our crop frame as you can see this is what our crop frame and this is what our main frame so if I skip the code as you can see in crop frame we only see the number plate and the text from the number plate or we can say for uh the license plate so this is what our license plate simple done this is what our first license plate then we are going to now detect our second lense plate so as you can see the number is changed because we have now new lense plate and because we mention a crop function so it will crop our lysm plate means we crop the rectangle detected object where is the rectangle we have rectangle on our lyson plate so that's why it show here only a lysen plate done so we have done with crop section now what we want we want here a region of Interest area why because we don't want to detect our a lense plate all the all other area we only want to detect a lense plate in specific area for that we need to mention region of Interest area so how do we mention region of Interest area I have already mentioned the regon of Interest area with the help of C2 poly lines so let me UNC commmand the poly line function and area I have mentioned over here also here it is as you can see this is what area so how do we mention the area points as I explain in several previous video we have here mouse event function we have here mouse event event Mouse move function or we can say mouse move event function CV2 do event Mouse move this is what the function and with the help of this function when we move our Curson on the frame we will get the XY coordinate so if I say the code if I run the code because I have already mentioned the area and I already mentioned the C2 do poly lines so this is what my area as you can see in blue color we have here area so how do I mention the points as I mentioned we have C2 do event uh mouse event move function as you can see event Mouse move function so if I move my cursor over here on the frame inside shell we have x y coordinate so if I want to start from here just move cursor over here and then inside sh we have 37 and 376 this is the XY coordinates so this is what my first point for drawing poly line so this is you this is how you will get all the coordinates go here you will get for uh second point you will get XY coordinate then just go over here next Point XY coordinates then go over here next Point XY coordinates and then you need to mention the coordinates like this way as a area variable square bracket and all other coordinates in like like a simple bracket like this way which I have already mentioned so I have already mentioned all the things inside the code simple and then now as you can see we have here our area so now what we are going to say here we are going to create a condition so if the rectangle center point because rectangle is basically our object so if the rectangle Center Point if it is inside our area then only we have going to detect a Lys plate so for Center Point CX is equal to int + X1 + X2 divided by 2 as we always do and Cy is equal to int y1 + Y2 ided 2 this is how we will get the center point of our rectangle the rectangle which basically detecting our license number plate that's it so now we have s2. Point polygon test method as I explained in several previous video we have our Point polygon test method which basically a detect object if it is inside the poly line inside the poly line how it's it's use the area area and it's use the center point if the detected rectangle Center Point if it is inside our area then only we have one whether we have minus one so one so for that we are going to mention the condition if the result if it is greater than or is equal to zero then only we are going to uh draw here a crop then only we are going to draw a rectangle and then only we are going to show our crop image so I just want to cross check if it is in line so it's not in line basically just go back um one more one more more and now it's in line so this one is also we want to make a little bit back now what will happen as I mentioned because we have a s2. point polygon test method so it will detect our Lys plant only the Lys plant uh rectangle Center Point if it is inside our area then only we have rectangle so if I save the code if I run the code so now as you can see lense plate it is inside our area we have detection we have crop image we have detection there is a rectangle also we have crop image now if I skip the frame now there is no detection as you can see now there is no detection because our lysm plate is not inside our region of Interest area which we draw with the help of C2 mouse event function event Mouse move function you will get the XY coordinates and with the help of XY coordinates we mention our area and with the help of C2 do poly lines we draw our area then we mention CV2 do Point polygon test method inside that method we mention our area and then we mention our area and inside area if we have our CX and Cy which is our rectangle Center Point if the Center Point if it is inside our area then only we have detection so for that we create a condition if the result is if it is greater than or is equal to zero only then we have detection only then we are going to crop that's it now for reading the text from detected crop image we are going to convert our image in Gray format so here it is as you can see gray is equal to C2 CVT color crop which is our image and CV2 do color BGR to gray it is really simple this is how we convert image in BGR to gray format simple so then we are going to use here a filter so for filtering we we need our gray image so here it is as you can see I have mentioned the gray image then the coordinates simple now we are ready and we are going to Simply call our taser function so P taser do image to string and inside that we want to mention our image which is the gray image because we want to read a text from our gray image and then do strip this is the method that's it and when we read a text if there is comma or if there is a uh double quote all things if there if there we want to replace all these things so for that I have mentioned text. replace method it will replace comma code and all other things that's it and then then simply if you go here and let me let me print here text for our crosschecking before we move our further process let's just print our text the text is basically uh detect license plate number simple so if I save the code if I run the code now as you can see there is no detection there is nothing because the license plate is not inside our area so if I skip the frame and now the license plate is inside our area there is a crop image and here it is the numbers here it is the detected license plate number inside shell DL 7c dl7 C D50 017 it means we are successfully detecting our license plate we are cropping image with the help of taser OCR or we are we read the numbers from our number plate or we can simply say we are basically reading the text from a number plate image to string image to string this is the method which we are using over here image to string and if there is a uh let's just say if there is a comma or uh any any symbol like double quote we want to Simply replace all these things so for that I have mentioned text. replace and I have replaced these things this one and this one also simple so replace and then we are going to Simply print a final text which is our number plate text that's it now what is the next thing friends because we are detecting here a number plate and the number plate it will take some time to cross our area cross our area for that what we are going to do we are going to save this number plate inside proceed numbers as a set format here it is as you can see I have mentioned here proceed numbers and set so it will basically save all the numbers as a set in this proceed numbers in this proceed numbers and then we are going to create our condition if if we have text if text not in proceed numbers if text not in proceed numbers then proceed numbers. add and add the text means it is basically adding the text the number text inside our set the proceed numbers this is the set which we create over here which we create over here that's it and now as I mentioned because we want to uh save the number in text format means in text file not in text format basically we want to save the number plate in text file with current date and current time so we have here our date time function as you can see from date time import date time from date time import date time this is how we mention our date time simple basic code basic things and then here it is as you can see if text not in proceed numbers means if there is no text just add the text using this text variable here it is as you can see then what we are going to do we are going to create a list one list one we are going to create and insert that list one we are going to append our text we are going to append our text and then for current date time this is the function this is how we call our current date time current date time is equal to date time do now then Str strf time this is what our format simple this is what our format and then we are going to create our file the carplate data txt this is what our file and then we are going to write our file with this format a text current dat time text means our number license number plate and current date time that's it so this is how our final code will work simple now if I save the code and if I run the code just skip the frame we detect our first license plate and if I sto the code and if you open the Repository open it and inside that as you can see it's created our text file open the text file and inside that text file as you can see we have here we have here basically the number plate the current date and the current time current date and current time so just close and now I will delete the text file I will delete the text file and here all is good and now what we are going to do we are going to Simply start our while loop save the code and run the code detected first license plate we have crop image next license plate we have crop image next one Det detected when the license plate inside mentioning area we have detection and it's basically saving inside our text file with current date and current time we have done close the code and if I open downloads and our repository carplate data.txt and as you can see the number plate this is all the number plates and here it is a as you can see we have basically a square bracket here it is so as I mentioned for replacing all these things we simply want to mention over here let's just say uh replace here again dot replace and what we want to replace we want to replace a square bracket this one and then instead of this we are going to mention nothing like this way done now if I save the code and if I start code again now it will save again all those numbers inside our text file but in real scenario of course we have more cars we have new number so it will save all the numbers inside our text file with their current date and current time done stop go over here downloads and then we have number plate text file and here it is as you can this this is what our first detection and we have your square bracket but because we mention a replace so now in New detection there is no square bracket we have only the number plate a text we have current date and current time also also because we create here a set uh in code because we create here a proceed numbers dot uh do add this is what our set so it will cross check if the a particular time stamp if the same number plate if it is there it will not detect some same number plate in same time stamp again and again okay so this is how friends the code will work it is really easy we detect we create our own custom object detection model it's detecting our lense plate then we crop our image because we only want detected lense plate so we have rectangle on our lense plate so we crop our rectangle coordinates with the help of crop method then we have P act so we have P OCR so with the help of part OCR we read the text now we draw our area with the help of poly lines we draw our area now we detect the license plant only when the car number LIC plate if it is inside our area so we detect the license plate inside our area then we save the license plate T inside the set and then we we call all the license plate inside the list format so we mention here list one and then we want to save our detection license plate inside our text file so for that we mention here current date and current time and then we create a file and inside that file we are saving a data simple I hope you will learn something from this tutorial I have mentioned the full code we will meet our next video till then thank you take care and bye-bye
Info
Channel: FREEDOM TECH
Views: 4,879
Rating: undefined out of 5
Keywords:
Id: gCNEJg0I7OQ
Channel Id: undefined
Length: 49min 47sec (2987 seconds)
Published: Mon Mar 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.