【问题标题】:TypeError: create_int(): incompatible function argumentsTypeError: create_int(): 不兼容的函数参数
【发布时间】:2021-12-09 15:52:30
【问题描述】:

我最近一直在用python学习计算机视觉,在做手部检测项目的时候遇到了这个错误:-

Traceback (most recent call last):
File "c:\Users\idhant\OneDrive - 007lakshya\Idhant\Programming\Projects\MY MACHINE 
LEARNING PROJECTS\Hand Tracking Module.py", line 64, in <module>
main()
File "c:\Users\idhant\OneDrive - 007lakshya\Idhant\Programming\Projects\MY MACHINE 
LEARNING PROJECTS\Hand Tracking Module.py", line 41, in main
detector = handDetector()
File "c:\Users\idhant\OneDrive - 007lakshya\Idhant\Programming\Projects\MY MACHINE 
LEARNING PROJECTS\Hand Tracking Module.py", line 13, in __init__
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.detectionCon, 
self.trackCon)
File "C:\Users\idhant\AppData\Roaming\Python\Python39\site- 
packages\mediapipe\python\solutions\hands.py", line 114, in __init__
super().__init__(
File "C:\Users\idhant\AppData\Roaming\Python\Python39\site- 
packages\mediapipe\python\solution_base.py", line 258, in __init__
self._input_side_packets = {
File "C:\Users\idhant\AppData\Roaming\Python\Python39\site- 
packages\mediapipe\python\solution_base.py", line 259, in <dictcomp>
name: self._make_packet(self._side_input_type_info[name], data)
File "C:\Users\idhant\AppData\Roaming\Python\Python39\site- 
packages\mediapipe\python\solution_base.py", line 513, 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:0] global D:\a\opencv-python\opencv- 
python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous- 
 namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

我尝试了很多调试它,但没有成功:(,所以请帮助我,这是我编写的代码:-

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

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








if __name__ == "__main__":
    main()

我已经尝试过制作一个手部检测器类,它可以做同样的事情来检测手,但我们也可以在我们的其他文件中使用它,这就是我编写这段代码的原因,并遇到了这个问题!

【问题讨论】:

    标签: python opencv mediapipe


    【解决方案1】:

    def __init__(),处代码:

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

    尝试为Hands() 中的第三个参数添加模型复杂度,如下所示:

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

    所以self.mpHands.Hands()中一共有五个参数

    这是我的完整代码:

    class handDetector():
        def __init__(self, mode=False, maxHands=1, modelComplexity=1, detectionCon=0.5, trackCon=0.5):
            self.mode = mode
            self.maxHands = maxHands
            self.modelComplex = modelComplexity
            self.detectionCon = detectionCon
            self.trackCon = trackCon
    
            self.mpHands = mp.solutions.hands
            self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComplex, 
                                            self.detectionCon, self.trackCon)
    

    【讨论】:

    • 我以为modelComplexity=1 self.modelComplex = modelComplexity 会默认工作,但手动定义后它工作得很好。
    • 非常感谢,检查了互联网上的每个地方,但找不到解决问题的方法,谢谢。
    【解决方案2】:

    您需要在handDetector() 类的__init__ 方法中再分配一个参数。

    您的完整代码可能如下所示:

    import cv2
    import mediapipe as mp
    import time
    
    # class creation
    class handDetector():
        def __init__(self, mode=False, maxHands=2, detectionCon=0.5,modelComplexity=1,trackCon=0.5):
            self.mode = mode
            self.maxHands = maxHands
            self.detectionCon = detectionCon
            self.modelComplex = modelComplexity
            self.trackCon = trackCon
            self.mpHands = mp.solutions.hands
            self.hands = self.mpHands.Hands(self.mode, self.maxHands,self.modelComplex,
                                            self.detectionCon, self.trackCon)
            self.mpDraw = mp.solutions.drawing_utils # it gives small dots onhands total 20 landmark points
    
        def findHands(self,img,draw=True):
            # Send rgb image to hands
            imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
            self.results = self.hands.process(imgRGB) # process the frame
        #     print(results.multi_hand_landmarks)
    
            if self.results.multi_hand_landmarks:
                for handLms in self.results.multi_hand_landmarks:
    
                    if draw:
                        #Draw dots and connect them
                        self.mpDraw.draw_landmarks(img,handLms,
                                                    self.mpHands.HAND_CONNECTIONS)
    
               return img
    
        def findPosition(self,img, handNo=0, draw=True):
            """Lists the position/type of landmarks
            we give in the list and in the list ww have stored
            type and position of the landmarks.
            List has all the lm position"""
    
            lmlist = []
    
            # check wether any landmark was detected
            if self.results.multi_hand_landmarks:
                #Which hand are we talking about
                myHand = self.results.multi_hand_landmarks[handNo]
                # Get id number and landmark information
                for id, lm in enumerate(myHand.landmark):
                    # id will give id of landmark in exact index number
                    # height width and channel
                    h,w,c = img.shape
                    #find the position
                    cx,cy = int(lm.x*w), int(lm.y*h) #center
                    # print(id,cx,cy)
                    lmlist.append([id,cx,cy])
    
                    # Draw circle for 0th landmark
                    if draw:
                        cv2.circle(img,(cx,cy), 15 , (255,0,255), cv2.FILLED)
    
            return lmlist
    
    def main():
        #Frame rates
        pTime = 0
        cTime = 0
        cap = cv2.VideoCapture(0)
        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("Video",img)
            if cv2.waitKey(1) == ord('q'):
                break
    
        cap.release()
        cv2.destroyAllWindows()
    
    if __name__ == "__main__":
        main()
    

    【讨论】:

      【解决方案3】:

      我不知道它现在是否有帮助,或者可能供将来参考。 这是最后一个版本的 mediapipe 的已知问题。 恢复到 0.8.8 版本即可解决问题。

      【讨论】:

        猜你喜欢
        • 2022-07-09
        • 1970-01-01
        • 1970-01-01
        • 2013-08-09
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-01
        相关资源
        最近更新 更多