Today, one of our customer Krishnam Goyal Worked on and shared a nice OpenCV based code to detect hand (fingers) using simple camera module and python code.

This is not a tutorial but just a shared post to congratulate Krishnam for nice work and to encourage to create more such projects.

Its the photo of Krishnam doing the experiment and code

Python Code

import cv2 as cv
import mediapipe as mp

import time
cap = cv.VideoCapture(0)
class handDetector():
    def __init__(self, mode = False,maxHands = 2, detectionCon = 0.5, trackcon = 0.5):
        self.mode = mode
        self.maxHands = maxHands
        self.detectionCon = detectionCon
        self.trackcon = trackcon
        self.mpHands = mp.solutions.hands
        self.hands = self.mpHands.Hands(self.mode,self.maxHands,self.detectionCon,self.trackcon)
        self.mpDarw = mp.solutions.drawing_utils

    def findHands(self,img,draw = True):
        imgRgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
        self.results = self.hands.process(imgRgb)

        if self.results.multi_hand_landmarks:
            for handLms in self.results.multi_hand_landmarks:
                if draw:
                    self.mpDarw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
        return img
    def findposition(self,img,handNo = 0, draw=True):
                lmlist = []
                myHand = self.results.multi_hand_landmarks[handNo]
                if self.results.multi_hand_landmarks:
                    for id, lm in enumerate(myHand.landmark):

                        # print(id,lm)
                        h, w, c = img.shape
                        cx, cy = int(lm.x * w), int(lm.y * h)
                        lmlist.append(id, c, cy)
                        # print(id, cx, cy)
                        # if id == 4:
                            # cv.circle(img, (cx, cy), 25, (255, 0, 255), cv.FILLED)'
                return lmlist

def main():
    Pt = 0
    cT = 0
    #cap = cv.imread(0)
    cap = cv.VideoCapture(0)
    detector = handDetector()
    while True:
        success, img = cap.read()
        img = detector.findHands(img)
        detector,
        cT = time.time()
        fps = 1/(cT-Pt)
        Pt = cT

        cv.putText(img,str(int(fps)),(18, 78), cv.FONT_HERSHEY_COMPLEX,3,(255,0,255))
    # if fps >= 20:
    #     cv.putText(img, str(int(20)), (180, 78), cv.FONT_HERSHEY_COMPLEX, 3, (255, 0, 255))
        cv.imshow("Video", img)
        cv.waitKey(1)
    
if (__name__ == "__main__"):
    main()

The modules needed to run this code

Feel free to experiment with this code.