在Python 3.7.9中使用opencv和mediapipe制作手部跟踪模块时出现错误



每当我尝试运行我的代码时,我都会得到以下错误,我正在遵循手动跟踪教程,我正确地遵循了步骤,但我仍然有某种错误。

视频链接:https://www.youtube.com/watch?v=01sAkU_NvOY&t=2100s

Traceback (most recent call last):
File "C:/Users/aryan/Desktop/user/Study/Com_vis/Hand_T_M.py", line 56, in <module>
main()
File "C:/Users/aryan/Desktop/user/Study/Com_vis/Hand_T_M.py", line 41, in main
detector = handDetector()
File "C:/Users/aryan/Desktop/user/Study/Com_vis/Hand_T_M.py", line 12, in __init__
self.detectionCon, self.trackCon)
File "C:UsersaryanDesktopuserStudyCom_visvenvlibsite-packagesmediapipepythonsolutionshands.py", line 127, in __init__
outputs=['multi_hand_landmarks', 'multi_handedness'])
File "C:UsersaryanDesktopuserStudyCom_visvenvlibsite-packagesmediapipepythonsolution_base.py", line 260, in __init__
for name, data in (side_inputs or {}).items()
File "C:UsersaryanDesktopuserStudyCom_visvenvlibsite-packagesmediapipepythonsolution_base.py", line 260, in <dictcomp>
for name, data in (side_inputs or {}).items()
File "C:UsersaryanDesktopuserStudyCom_visvenvlibsite-packagesmediapipepythonsolution_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
Process finished with exit code 1

我无法找出代码的问题。我正在运行python 3.7.9,下面是我的代码。

文件名:Hand_T_M.py

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(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("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()

使用新的mediapipe框架,您必须这样写:

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

你必须将你的'init'方法替换为:

class handDetector():
def __init__(self, mode=False, maxHands=3, detectionCon=False, trackCon = 0.5):
self.mode = mode
self.maxHands = maxHands
self.complexity = complexity
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,self.complexity, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils

谢谢。

也许你可以试试这段代码。

class handDetector():
def __init__(self, mode=False, maxHands=3,complexity=1, detectionCon=0.5, trackCon = 0.5):
self.mode = mode
self.maxHands = maxHands
self.complexity = complexity
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,self.complexity, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils

1。PIP uninstall mediapipe

2。

安装mediapipe==0.8.3.1这可能会解决你的问题

您需要使用正确的mediapipe,还可以参阅此GitHub源代码:

from mediapipe.python._framework_bindings import packet
  1. 进入PyCharm File->Settings->Project: YOUR-PROJECT ->点击Python解释器
  2. 卸载当前mediapipe
  3. 安装mediapipe以前的版本(在我的情况下是0.8.8))

或者,如果您可以使用pip命令:

  1. pip uninstall mediapipe
  2. pip install mediapipe==0.8.8

相关内容

  • 没有找到相关文章

最新更新