'TypeError: create_bool(): incompatible function arguments
I made a code detector that can detect hand, face, pose, etc. But when I run this in Visual Studio, I get the following error message:
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
This is my code:
import cv2
import mediapipe as mp
import time
class findhand():
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)
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
class findFace():
def __init__(self, mode=False, maxFaces=1, detectionCon=0.5
,trackCon=0.5):
self.mode = mode
self.maxFaces = maxFaces
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpFaceMesh = mp.solutions.face_mesh
self.face_mesh = self.mpFaceMesh.FaceMesh(self.mode
,self.maxFaces
,self.detectionCon
,self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
def findFace(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.face_mesh.process(imgRGB)
if self.results.multi_face_landmarks:
for faceLms in self.results.multi_face_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, faceLms,
self.mpFaceMesh.FACE_CONNECTIONS)
return img
class findPose():
def __init__(self, mode=False, complexity=1,detectionCon=0.5,landmarks=True
,trackCon=0.5):
self.mode = mode
self.maxFace = 1
self.detectionCon = 0.5
self.trackCon = 0.5
self.landmarks = landmarks
self.complexity = complexity
self.mpPose = mp.solutions.pose
self.pose = self.mpPose.Pose(self.mode, self.complexity
,self.detectionCon
,self.trackCon
,self.landmarks)
self.mpDraw = mp.solutions.drawing_utils
def findPose(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.pose.process(imgRGB)
if self.result.pose_landmarks:
PoseLms = self.results.pose_landmarks
mpDraw.draw_landmarks(img, PoseLms, self.mpPose.POSE_CONNECTIONS,
drawSpec, drawSpec)
return img
def main():
cTime, pTime = 0, 0
hand_detector = findhand()
face_detector = findFace()
pose_detector = findPose()
cap = cv2.VideoCapture(1, cv2.CAP_DSHOW)
while True:
success, img = cap.read()
img = face_detector.findFace(img)
if not success:
print("GPU Didn't Success At Loading Video")
continue
handlmList = hand_detector.findHands(img)
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("Detector", img)
if cv2.waitKey(1) != -1:
cv2.destroyAllWindows()
break
if __name__ == "__main__":
main()
The full error message is:
Traceback (most recent call last):
File "c:\Users\tmdgh\Desktop\DetectionModule2.py", line 135, in <module>
main()
File "c:\Users\tmdgh\Desktop\DetectionModule2.py", line 107, in main
pose_detector = findPose()
File "c:\Users\tmdgh\Desktop\DetectionModule2.py", line 76, in __init__
self.pose = self.mpPose.Pose(self.mode
File "C:\Users\tmdgh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mediapipe\python\solutions\pose.py", line 146, in __init__
super().__init__(
File "C:\Users\tmdgh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mediapipe\python\solution_base.py", line 258, in __init__
self._input_side_packets = {
File "C:\Users\tmdgh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\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\tmdgh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\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_bool(): incompatible function arguments. The following argument types are supported:
1. (arg0: bool) -> mediapipe.python._framework_bindings.packet.Packet
Invoked with: 0.5
Solution 1:[1]
Replace this line:
self.hands = self.mpHands.Hands(self.mode, self.maxHands ,self.detectionCon ,self.trackCon)
With this one:
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComplex, self.detectionCon, self.trackCon)
Also add parameter of modelComplex
in __init__
method, you are getting this error because you are missing one parameter that is model complexity.
Solution 2:[2]
try this
def __init__(self, mode=False, maxHands=2, detectionCon=False, trackCon=0.5):
Solution 3:[3]
I solved the problem by passing the keyword arguments instead of positional arguments. This ensured right argument is being passed at right place.
self.mpHands.Hands(
static_image_mode=True/False,
model_complexity=yourNumber,
smooth_landmarks=True/False,
enable_segmentation=True/False,
smooth_segmentation=True/False,
min_detection_confidence=yourConfidence,
min_tracking_confidence=yourConfidence):
Solution 4:[4]
If you look at the Pose(SolutionBase) class of MediaPipe, you can see def __init__
:
class Pose(SolutionBase):
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):
You should have the same parameters in the class findPose() __init__
. Of course you can name them whatever you want, but the sequence and types must be the same.
More information: https://google.github.io/mediapipe/solutions/pose#model_complexity
https://www.youtube.com/watch?v=01sAkU_NvOY watch from 1:11
Solution 5:[5]
This seems to be related to the version of mediapipe or opencv you are using. Please look out to the appropriate combinaison of version and you should be good
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tomerikoo |
Solution 2 | Suraj Rao |
Solution 3 | Ahmad Anis |
Solution 4 | ddejohn |
Solution 5 | Manifest Man |