google-ai-edge / mediapipe

Cross-platform, customizable ML solutions for live and streaming media.
https://ai.google.dev/edge/mediapipe
Apache License 2.0
27.5k stars 5.15k forks source link

HandTrackingModule_with_mediapipe #5225

Closed Iamsakir closed 7 months ago

Iamsakir commented 8 months ago

Have I written custom code (as opposed to using a stock example script provided in MediaPipe)

None

OS Platform and Distribution

windows 11

MediaPipe Tasks SDK version

No response

Task name (e.g. Image classification, Gesture recognition etc.)

give error

Programming Language and version (e.g. C++, Python, Java)

python

Describe the actual behavior

this code is not work in mediapipe version 0.8.11

Describe the expected behaviour

this code is work in mediapipe version 0.8.3.1

Standalone code/steps you may have used to try to get what you need

import cv2
import mediapipe as mp
import time

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.mpDraw = mp.solutions.drawing_utils

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

        if self.results.multi_hand_landmarks:
            for handLms in self.results.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, handLms,
                                               self.mpHands.HAND_CONNECTIONS)
        return img

    def findPosition(self, img, handNo=0, draw=True):

        lmList = []
        if self.results.multi_hand_landmarks:
            myHand = self.results.multi_hand_landmarks[handNo]
            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)
                # print(id, cx, cy)
                lmList.append([id, cx, cy])
                if draw:
                    cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)

        return lmList

def main():
    pTime = 0
    cTime = 0
    cap = cv2.VideoCapture(1)
    detector = handDetector()
    while True:
        success, img = cap.read()
        img = detector.findHands(img)
        lmList = detector.findPosition(img)
        if len(lmList) != 0:
            print(lmList[4])

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

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

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

if __name__ == "__main__":
    main()

Other info / Complete Logs

[ERROR:0@1.530] global obsensor_uvc_stream_channel.cpp:159 cv::obsensor::getStreamChannelGroup Camera index out of range
Traceback (most recent call last):
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\HandTrackingModule.py", line 71, in <module>
    main()
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\HandTrackingModule.py", line 51, in main
    detector = handDetector()
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\HandTrackingModule.py", line 14, in __init__
    self.hands = self.mpHands.Hands(self.mode, self.maxHands,
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\venv\lib\site-packages\mediapipe\python\solutions\hands.py", line 114, in __init__
    super().__init__(
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\venv\lib\site-packages\mediapipe\python\solution_base.py", line 290, in __init__
    self._input_side_packets = {
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\venv\lib\site-packages\mediapipe\python\solution_base.py", line 291, in <dictcomp>
    name: self._make_packet(self._side_input_type_info[name], data)
  File "C:\Users\sakir\OneDrive\Desktop\new_for_image\pythonProject\venv\lib\site-packages\mediapipe\python\solution_base.py", line 593, in _make_packet
    return getattr(packet_creator, 'create_' + packet_data_type.value)(data)
TypeError: create_int(): incompatible function arguments. The following argument types are supported:
    1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.5
kuaashish commented 8 months ago

Hi @Iamsakir,

Based on the code excerpt provided, it appears that you are using an outdated version of MediaPipe. We have recently launched new MediaPipe Tasks APIs, which are more stable and comprehensive than the legacy version. We strongly advise you to switch to these newer APIs for Hand landmarks, as we no longer provide support for legacy solutions. You can find the Hand Landmarker documentation here.

Thank you!!

github-actions[bot] commented 7 months ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 7 months ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 7 months ago

Are you satisfied with the resolution of your issue? Yes No