'hand tracking system using opencv error:TypeError: handDetector.findHands() missing 1 required positional argument: 'img'

I'm following an online advanced CV course but the code wouldn't run properly for me. These are the error messages I've gotten:

#Traceback (most recent call last):
  #File "C:\Users\lihua\PycharmProjects\AdvancedComputerVision\HandTrackingModule.py", line 60, 
#in <module>
    #main ()

  #File "C:\Users\lihua\PycharmProjects\AdvancedComputerVision\HandTrackingModule.py", line 48, 
#in main
    #img = detector.findHands(img)
#TypeError: handDetector.findHands() missing 1 required positional argument: 'img'

#[ WARN:[email protected]] global D:\a\opencv-python\opencv- 
#python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous- 
#namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

I don't understand what I did wrong as I followed the online tutorial exactly and it seems that it worked out fine with the person in the tutorial

import cv2
import mediapipe as mp
import time

class handDetector ():
    #initialization
    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.mpDraw = mp.solutions.drawing_utils

    def findHands(self,img,draw =True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.hands.process(imgRGB)
        # print (results.multi_hand_landmarks)

        if results.multi_hand_landmarks:
            for handLms in results.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
        return img
def main ():
    pTime = 0
    cTime = 0
    cap = cv2.VideoCapture(0)
    detector = handDetector
    while True:
        success, img = cap.read()
        img = detector.findHands(img)

    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime

    cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (225, 0, 225), 3)

    cv2.imshow('Image', img)
    cv2.waitKey(1)

if __name__ == '__main__':
    main ()


Solution 1:[1]

You forgot to instantiate the class handDetector in the main function line 31, you have to add () otherwise you pass him the complete object without instantiating it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1