1

I am trying to use mediapipe to track hands. I am using Python 3.7.9 on Windows 10, my code is below:

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)

while (True):
  success, img = cap.read()
  imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  results = mp_hands.Hands.process(imgRGB)
  print(result)
  if cv2.waitKey(10) & 0xFF == ord('q'):
    break

I'm getting this error:

Traceback (most recent call last):
  File "C:/Users/Tomáš/PycharmProjects/pythonProject/hand_detect.py", line 11, in <module>
    results = mp_hands.Hands.process(imgRGB)
TypeError: process() missing 1 required positional argument: 'image'
[ WARN:1] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Error says that I need to pass one more argument 'self' before I pass argument 'image'. I've been browsing a lot and every related code doesnt use first argument in the process() function. Could anyone help me solve this error?

Tom379
  • 11
  • 2
  • It is pretty tough to help you without a fully reproducible example. Maybe [the mediapipe documentation](https://google.github.io/mediapipe/solutions/hands.html) gives you an answer, there is an example for static images. – mnikley Oct 26 '21 at 11:49
  • Can you check documentation of cv2 also what this function returns cvtColor. You can use breakpoint() in between for debugging – Anand Tripathi Oct 26 '21 at 12:22

1 Answers1

1

The problem is that you do not create an object of mp_hands.Hands before you want to process it. The following code solves it and prints some results. By the way, this was well documentated in the documentation link i commented before.. :

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)  # i had problems before reading webcam feeds, so i added cv2.CAP_DSHOW here 

while True:
    success, img = cap.read()
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # you have to create an object of mp_hands.Hands to get results
    # alternatively you could do: results = mp_hands.Hands().process(imgRGB)
    with mp_hands.Hands() as hands:
        results = hands.process(imgRGB)

    # continue loop if no results were found
    if not results.multi_hand_landmarks:
        continue

    # print some results
    for hand_landmarks in results.multi_hand_landmarks:
        print(
            f'Index finger tip coordinates: (',
            f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x}, '
            f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y})'
        )

    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

Edit: This is more or less the same code from here:

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

# initialize webcam
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

with mp_hands.Hands(model_complexity=0,
                    min_detection_confidence=0.5,
                    min_tracking_confidence=0.5) as hands:
    while cap.isOpened():
        success, image = cap.read()
        if not success:
            print("Ignoring empty camera frame.")
            # If loading a video, use 'break' instead of 'continue'.
            continue

        # To improve performance, optionally mark the image as not writeable to
        # pass by reference.
        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = hands.process(image)

        # Draw the hand annotations on the image.
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                mp_drawing.draw_landmarks(
                    image,
                    hand_landmarks,
                    mp_hands.HAND_CONNECTIONS,
                    mp_drawing_styles.get_default_hand_landmarks_style(),
                    mp_drawing_styles.get_default_hand_connections_style())
        # Flip the image horizontally for a selfie-view display.
        cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
        if cv2.waitKey(5) & 0xFF == 27:
            break
cap.release()
mnikley
  • 1,625
  • 1
  • 8
  • 21
  • with mp_hands.Hands() as hands: this line caused another error: FileNotFoundError: The path does not exist. – Tom379 Oct 26 '21 at 14:37
  • I edited my post, try copy pasting the second code example into a new file and run it. – mnikley Oct 27 '21 at 09:19
  • It didn't solve the problem. I'm getting still the same error – Tom379 Oct 27 '21 at 14:51
  • Copy and paste the full error log. Other than that, you could try to un- and re-install the packages with `pip uninstall -y cv2 mediapipe` `pip install cv2 mediapipe` – mnikley Oct 27 '21 at 15:14
  • Traceback : File "C:/Users/Tomáš/PycharmProjects/ruce/main.py", line 13, in min_tracking_confidence=0.5) as hands: File "C:\Users\Tomáš\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solutions\hands.py", line 127, in __init__ outputs=['multi_hand_landmarks', 'multi_handedness']) File "C:\Users\Tomáš\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solution_base.py", line 238, in __init__ binary_graph_path=os.path.join(root_path, binary_graph_path)) FileNotFoundError: The path does not exist. – Tom379 Oct 27 '21 at 15:20
  • This suggests that your package is somehow not correctly installed and cross-dependencies can not be found. Try uninstalling and reinstalling the packages, or start a search for that specific error. – mnikley Oct 27 '21 at 15:23