1

i'm trying to create a pose detection module but for some reason i get this error every time. it has something to do with my variable declerations but i don't understand what. When i try to convert it to a module it gives me this type error, and this is my code:

import cv2
import mediapipe as mp
import time


class poseDetector():

    def __init__(self, mode=False, upBody=False, smooth=True, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.upBody = upBody
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackCon = trackCon

        self.mpPose = mp.solutions.pose
        self.mpDraw = mp.solutions.drawing_utils
        self.pose = self.mpPose.Pose(self.mode, self.upBody, self.smooth, self.detectionCon, self.trackCon)

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.pose.process(imgRGB)
        if results.pose_landmarks:
            if draw:
                self.mpDraw.draw_landmarks(img, results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)
        return img
    # for id, lm in enumerate(results.pose_landmarks.landmark):
    #     h, w, c = img.shape
    #     cx, cy = int(lm.x * w), int(lm.y * h)
    #     cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)


def main():
    cap = cv2.VideoCapture("squats.mp4")
    ptime = 0
    detector = poseDetector()

    while True:
        success, img = cap.read()
        img = detector.findPose(img)

        ctime = time.time()
        fps = 1 / (ctime - ptime)
        ptime = ctime

        cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
        cv2.imshow("Image", img)
        cv2.waitKey(1)


if __name__ == "__main__":
    main()

This is the full error:

"C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\Scripts\python.exe" "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py" 
Traceback (most recent call last):
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py", line 52, in <module>
    main()
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py", line 36, in main
    detector = poseDetector()
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\PoseModule.py", line 18, in __init__
    self.pose = self.mpPose.Pose(self.mode, self.upBody, self.smooth, self.detectionCon, self.trackCon, self.modelComplex)
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\lib\site-packages\mediapipe\python\solutions\pose.py", line 146, in __init__
    super().__init__(
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\venv\lib\site-packages\mediapipe\python\solution_base.py", line 290, in __init__
    self._input_side_packets = {
  File "C:\Users\213353\Desktop\Ambaka\Coding\Computer Vision\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\213353\Desktop\Ambaka\Coding\Computer Vision\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_bool(): incompatible function arguments. The following argument types are supported:
    1. (arg0: bool) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.5
CristiFati
  • 38,250
  • 9
  • 50
  • 87

1 Answers1

0

According to [GitHub]: google/mediapipe - (v0.8.11) mediapipe/mediapipe/python/solutions/pose.py (latest at answer time, same on master branch), Poses initializer (around line #114) is:

 def __init__(self,
               static_image_mode=False,
               model_complexity=1,
               smooth_landmarks=True,
               enable_segmentation=False,
               smooth_segmentation=True,
               min_detection_confidence=0.5,
               min_tracking_confidence=0.5):

When you instantiate self.mpPose.Pose, you're passing fewer (5) arguments that the initializer expects (7), so it matches them by order yielding invalid values (2nd argument (model_complexity) is an int, but you're passing a bool (maybe you missed it?), same thing about the last 2 ones).
You must pass arguments, either as:

  • Positional (less typing required, but I wouldn't recommend it since this is the very reason that got you in trouble). Note that you still need to pass another bool argument (the True value following 1)

    self.pose = self.mpPose.Pose(self.mode, 1, True, self.upBody, self.smooth, self.detectionCon, self.trackCon)
    
  • Keyword (more typing required, but the recommended one):

    self.pose = self.mpPose.Pose(
        static_image_mode=self.mode,
        enable_segmentation=self.upBody,  # @TODO:
        smooth_segmentation=self.smooth, 
        min_detection_confidence=self.detectionCon, 
        min_tracking_confidence=self.trackCon)
    

    Since I don't know what those arguments do semantically, I passed self.upBody as enable_segmentation (based on default values), but it could also be passed as smooth_landmarks (the @TODO line).

Related:

CristiFati
  • 38,250
  • 9
  • 50
  • 87