在 Python 3.7.9 中使用 opencv 和 mediapipe 制作手部跟踪模块时出错
Posted
技术标签:
【中文标题】在 Python 3.7.9 中使用 opencv 和 mediapipe 制作手部跟踪模块时出错【英文标题】:Getting errors with opencv and mediapipe for making Hand Tracking Module in Python 3.7.9 【发布时间】:2021-12-15 05:29:01 【问题描述】:每当我尝试运行我的代码时,我都会收到以下错误,我正在学习手部追踪教程,我正确地遵循了这些步骤,但我仍然遇到了某种错误。
视频链接: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:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solutions\hands.py", line 127, in __init__
outputs=['multi_hand_landmarks', 'multi_handedness'])
File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solution_base.py", line 260, in __init__
for name, data in (side_inputs or ).items()
File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\site-packages\mediapipe\python\solution_base.py", line 260, in <dictcomp>
for name, data in (side_inputs or ).items()
File "C:\Users\aryan\Desktop\user\Study\Com_vis\venv\lib\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
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()
【问题讨论】:
【参考方案1】:您需要使用正确的媒体管道,另见this GitHub source code:
from mediapipe.python._framework_bindings import packet
【讨论】:
【参考方案2】:-
转到PyCharm文件->设置->项目:您的项目->点击Python解释器
卸载当前 mediapipe 软件包
安装mediapipe以前的版本(在我的例子中是0.8.8)
或者,如果您可以使用 pip 命令:
-
pip 卸载媒体管道
pip install mediapipe==0.8.8
【讨论】:
【参考方案3】:也许你可以试试这个代码。
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
【讨论】:
以上是关于在 Python 3.7.9 中使用 opencv 和 mediapipe 制作手部跟踪模块时出错的主要内容,如果未能解决你的问题,请参考以下文章