Harbola DataScience

Answer, Read and Post Coding related Questions!

Live Finger Counter OpenCv Python

Sometimes we can’t speak. We can only communicate using sign and hand gesture. But computers can’t do that.

Comedy GIF  WE tv - Find & Share on GIPHY

Computers CAN!

In this project we will make a python program that will detect hands and then count then number of fingres.

This technique of recognising gestures can help in many ways. You can make a lot of fun project. You can make a stone, paper, scissor game and fun with that.

Demonstration of Finger Counting project

Hope you enjoyed it. I tried many different thing with this. I recommend you too implementing this project on your system. The steps that you will require will be explained me.

Logic or intuition behind this project

This is the representation of coordinates marked on fingers. Our task is to identify which finger is opened and which is closed.

It can be achieved if we do something and define some rules like if the tip(for index finger i.e 8) is below middle finger point i.e 6 then the finger is closed and visa versa. And same for all fingers. If the coordinate or tip in below the middle finger point the finger is closed else it is opened.

We will see in further part how we can do this using python.

The basic requirement for computer vision project

I have done this project using python opencv library. So you need to install python on your system. Remember adding it to path. Then open cmd and using pip install opencv library.

Once done with that. You can use python idle or install any text editor. I am using vs code.

You will also require a web cam to capture the video. You can also use pre recorded video and load it to your code. It will need some tweeks in code.

Implementation of Finger counting project.

In a python program libraries play a crucial role in building a project and making job easier. It has all the mind boggling things done. You just need to use one or two word and pass the attributes.

Importing Libraries

Opencv and mediapipe are two libraries that we will be using for this project. In other project too I have used mediapipe library and opencv library is used in every computer vision project. You need to install both of them using pip command.

Initialisation of coordinates and mediapipe object

VideoCapture is a opencv method to capture web cam. 0 parameter is for default camera. mp.solutions.hands is targeting to hands and detecting it. using drawing utils we will draw points on hand joints. And declaring finger and thumb cordinates.

You can face some errors. Try solving them with help of this blog.

Converting image to RGB

When the image file is read with the OpenCV function imread() , the order of colors is BGR (blue, green, red). On the other hand, in Pillow, the order of colors is assumed to be RGB (red, green, blue). Therefore, if you want to use both the Pillow function and the OpenCV function, you need to convert BGR and RGB.

cvtColor() helps to convert image.

Drawing lines/landmarks on hand

the landmarks that you see on hands are drawn using just these lines of code.

Converting hand point into pixels

Working with the actual coordinates is challenging. Therefore, we need to change them into pixels.

We use the image.shape function to get the height, width, and color channel of the image. We will then get the x and y coordinates of each hand point in the form of pixels.

We will then save these hand points in the list we previously created. You can locate any point on image if you their pixel.

Circling the hand points

We will now circle each hand point we have identified. This is to ensure that we are getting the correct hand points.

We use the code below to achieve this:

Main part in counting fingers

This is the main part of this project because here we are doing the calculations required in counting, incrementing or decrementing as the user open or close their fingers. We have discussed the logic of opening and closing of fingers.

Displaying our output

The final step involves us displaying the output. We will display the number of open fingers using the value of the upcount. This is because it is only incremented when a finger is open.

We will also output a real-time video that shows the user opening and closing their fingers. Use the code below to achieve this:

That’s it for today. This was a mini project in which we simply detected fingers and counted them. If you interested in more such computer vision project checkout our blog. And share this with your friends. Post your suggestions and reviews on the comments down below.

Chitranshu Harbola

Self taught programmer, Web Developer and an aspiring Machine learning engineer cum Data Science student

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top