使用OpenCV和MediaPipe制作手识别文件时出错

发布于 2025-02-01 09:12:16 字数 3673 浏览 4 评论 0原文

我正在尝试使用MediaPipe和OpenCV制作一个手识别模块,我已经完成了一半,并试图查看代码是否有效。它没有,我不知道该如何解决。

这是我写的代码 -

import mediapipe as mp
import cv2
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)
        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

                
                #for id, lm in enumerate(handLms.landmark):
                    #print(id, lm)
                #    h, w, c = img.shape
                #    cx, cy = int(lm.x * w), int(lm.y * h)
                #    print(id, cx, cy)
                    #if id == 4:
                #    cv2.circle(img, (cx,cy), 15, (255,0,255), cv2.FILLED)
                
    

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_SIMPLEX,2,(0,255,0),3)

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

if __name__ == "__main__":
    main()

这是我发现的错误 -

Traceback (most recent call last):
  File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 60, in <module>
    main()
  File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 45, in main
    detector = handDetector()
  File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 14, in __init__
    self.hands = self.mpHands.Hands(self.mode,self.maxHands,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solutions\hands.py", 
line 114, in __init__
    super().__init__(
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 274, in __init__
    self._input_side_packets = {
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 275, in <dictcomp>
    name: self._make_packet(self._side_input_type_info[name], data)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 533, 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
[ 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

我正在使用Python版本3.8.3

我需要在代码中进行哪些更改才能使其正常工作?

I am trying to make a hand recognition module using mediapipe and opencv and I was half done and tried to see if the code worked. It didn't and I don't know how to fix it.

This is the code I wrote-

import mediapipe as mp
import cv2
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)
        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

                
                #for id, lm in enumerate(handLms.landmark):
                    #print(id, lm)
                #    h, w, c = img.shape
                #    cx, cy = int(lm.x * w), int(lm.y * h)
                #    print(id, cx, cy)
                    #if id == 4:
                #    cv2.circle(img, (cx,cy), 15, (255,0,255), cv2.FILLED)
                
    

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_SIMPLEX,2,(0,255,0),3)

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

if __name__ == "__main__":
    main()

And this is the error which I got-

Traceback (most recent call last):
  File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 60, in <module>
    main()
  File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 45, in main
    detector = handDetector()
  File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 14, in __init__
    self.hands = self.mpHands.Hands(self.mode,self.maxHands,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solutions\hands.py", 
line 114, in __init__
    super().__init__(
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 274, in __init__
    self._input_side_packets = {
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 275, in <dictcomp>
    name: self._make_packet(self._side_input_type_info[name], data)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 533, 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
[ 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 am using python version 3.8.3

What changes do I need to make in the code to get it to work ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2025-02-08 09:12:16

参数model_complexity缺少。它需要是整数。您正在传递检测con。您可以添加默认值1:

        self.hands = self.mpHands.Hands(self.mode, self.maxHands, 1,
                                        self.detectionCon, self.trackCon)

...或使用名为参数:

        self.hands = self.mpHands.Hands(static_image_mode=self.mode,
                                        max_num_hands=self.maxHands,
                                        min_detection_confidence=self.detectionCon,
                                        min_tracking_confidence=self.trackCon)

The parameter model_complexity is missing. It needs to be integer. You are passing detectionCon instead. You can add the default 1:

        self.hands = self.mpHands.Hands(self.mode, self.maxHands, 1,
                                        self.detectionCon, self.trackCon)

... or use named parameters:

        self.hands = self.mpHands.Hands(static_image_mode=self.mode,
                                        max_num_hands=self.maxHands,
                                        min_detection_confidence=self.detectionCon,
                                        min_tracking_confidence=self.trackCon)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文