Detecting & Counting coins on images with Python using OpenCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to the channel imagine having this image with some coins on it and I was counting how many coins there are in it as a human we can visually count the coins one by one to get the result by counting them we would conclude that there are nine coins on the image but I have another question how would a computer detect and count the coins nowadays there are many tools that we can use to do this but one easy solution could be be to get a simple AI model or try it in a straightforward Way by building a simple Python program to count the coins for us you might say okay this image has nine coins no need to develop a program to count them however I have more images to show you take a look at these images like the first one you can easily count them and come to the conclusion that there are uh 10 coins what about this one like the others it is easy to count and the number is six let's increase the challenge how about this image or this one you can sit down and count them but it would take you some time and you would probably make some erors so what is the number of coins in these images lucky you for this challenge in this video we will build a program to count the number of coins for us in these types of image using Python and open CV without further Ado let's build this program let's start by creating our main file which will have our python script and then let's bring bring our images uh each image has the number of coin attached to name as you can see it there we will need to import the package that we will we are going we are going to use including the open CV package what if we are going to detect coins we need a function to detect the coins in which we will pass the the image pet also we need something to to to know the error in terms of P percentage uh regarding the true value and the estimated value uh from our predict coin function and this is the way we are going to calculate it and since we have the image inside uh of folder we can build a dictionary with all the information from the image itself inside the folder let's add validation to run the script in the implementation of the on the detective coins function we need the a number of coins variable to store the number of coins we need to read our image pet uh regarding the pet we also need to apply a a graay uh gray scale filter on our image and then also I'm going to apply a blower effect in the image to help it to help us get uh more pictures from the image also I'm going to apply an Adaptive threshold to adapt for each image so it can give us uh really good results okay and then also to detect the circles the coin circles I will I'm going to use the open CV function uh root root circles function let me just pass the arguments here I'm just validating if we have circles there and also I'm uh rounding the values to integer there I'm getting the number of uh coins based on the length of the circles in the position zero also I'm just connecting the connecting the component States in order to show the image and the centroids I'm going to receive them uh receive them as parameters if I want to show the image or if I want to show the centroids also to show the the images Al to show the centroids uh I mean I will just have to to to to run through the centroids value to the croid values and then show it to the user the centroids are basically the the circles the circle positions or the the coin Circle positions in the image and also I need to to show the the image if I want to show the image and by but if I'm going to show the image I'm going to draw circles uh hting the the coins that the open CV or the function above did find and after that I will return the number of coins Let's test the function that we already created with the image with six coins let me bring my my turn terminal let's let's test it okay okay it's saying three coins okay something must have gone wrong let me just check and uh okay let's change the parameters on the ho circles let's change the parameter 2 to 30 instead of 50 okay and then okay it's working now it's showing six coins and let's Tex test for 10 coins and let's see it's working just fine too okay sounds good okay okay okay let's test to show the images okay we can see the imagees on the images that we have the the circles the red circles draw inside in the coins border and let's see the centroids okay we have 10 centroids or 10 coins position inside the image okay that looks good for me then we can proceed uh now let's build the image list okay we will start with an empty array and then we will get the current position and then we will join the position with the the folder pad that we are that we are passing with the images and then for each file that we encounter on the image folder we will we will search for the number of coins using uh reject code which the number of coins it's uh alongside with the image name and then we will get it then we need to check if the image is uh J uh an is in an image format I'm using gpg J PG but you can use another format image if you want and afterwards we'll only to append a dictionary with the image information inside the the image list which I'm appending the image the true coins number and also I'm appending the pet and then afterwards we need to return our image image information list then we can test the function that we already built for building the the image list let let's see how does it okay it looks okay for me and then let's work with that I'm going to use python AR parser library to to receive arguments while I'm executing the python code uh hting these arguments I will receive the folder pet uh the image folder pet which is a required uh argument then also I receive I want also to receive the if the user wants to show the images from the coins imagees and then also I think I would put uh the user to choose if he wants to show the image centroids uh so let me add that I will show the image centroids and then that's all from the arc sper that I will show to the that I will allow the user to to choose then we can we need to get the arcs and Par them then let's test it for me let's test okay it will say that we need to pass them let's press the images okay it looks so it works for it works then let's press the show images and then it also works okay since we have everything done uh now let's organize things and to in order to do that I will create a new function called Main and then in this main function we will uh build the image list with the build image list function uh also we need the total number of images we need the total relative error which we will initiate it alongside with the square error and then we will increment them also we need to run through all the images um running through them getting the index and then uh I will use the detect coins uh function to to detect the number of coins in each image image we will pass the pad and then the arcs to show if it's to show the images or if or not or if it's to show the caliz or not and then also for the relative error uh we have the function that we implemented back there and then we need just to pass the True Value and then the estimated value from the detect coins and also we need to uh increment the total relative error pressing the uh the relative error and then the square error and then after that uh in order to calculate the average relative error we just need to calculate the total relative error uh by the image number and then the average square ero the total square ER by the average number and we need to show this information to to the console I'm just making it pretty to show to the user okay almost done okay there I'm passing in the formatted string the the the two points do 2f to just add me to decimal uh case and I'm adding this print here just to make it more uh beautiful on the console it there's anything else now everything is implemented and let's test things okay for all the images okay we can see here that the CID are perfectly uh design in the images we have the circles surrounding the coins then we have the number of coins and then we are displaying the information through the console so the way I see it we have some small errors in the big uh the the image with most coins but it's okay and also we have the centroids okay so I guess it's it's pretty much done we already have we are already detecting the coins in the images and then we are building uh the centroids for each coins then we can see it here it pretty much detects us for us most of the in the images with uh less coins it detects all of them for us but in the other image with a lot of coins it has an small error uh till 10% of error which is okay and there you have guys we have successfully built a program to count the number of coins in an image using Python and open CV I hope you find this tutorial informative and enjoyable if you like this video and found it helpful don't forget to give it a thumbs up and hit the Subscribe button below for more content like this if you have any questions suggestions or just want to share your thoughts feel free to leave a comment down below I will do my best to answer all of your queries and engage with your feedback thank you so much for watching and I will see you in the next video
Info
Channel: Cyroobe
Views: 354
Rating: undefined out of 5
Keywords:
Id: VrgI1nPbV88
Channel Id: undefined
Length: 12min 47sec (767 seconds)
Published: Sat Mar 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.