使用OpenCV和MediaPipe制作手识别文件时出错
我正在尝试使用MediaPipe和OpenCV制作一个手识别模块,我已经完成了一半,并试图查看代码是否有效。它没有,我不知道该如何解决。
这是我写的代码 -
import mediapipe as mp
import cv2
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_SIMPLEX,2,(0,255,0),3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()
这是我发现的错误 -
Traceback (most recent call last):
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 60, in <module>
main()
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 45, in main
detector = handDetector()
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 14, in __init__
self.hands = self.mpHands.Hands(self.mode,self.maxHands,
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solutions\hands.py",
line 114, in __init__
super().__init__(
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 274, in __init__
self._input_side_packets = {
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 275, in <dictcomp>
name: self._make_packet(self._side_input_type_info[name], data)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 533, 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:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
我正在使用Python版本3.8.3
我需要在代码中进行哪些更改才能使其正常工作?
I am trying to make a hand recognition module using mediapipe and opencv and I was half done and tried to see if the code worked. It didn't and I don't know how to fix it.
This is the code I wrote-
import mediapipe as mp
import cv2
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_SIMPLEX,2,(0,255,0),3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()
And this is the error which I got-
Traceback (most recent call last):
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 60, in <module>
main()
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 45, in main
detector = handDetector()
File "f:/Aryav files/J.A.R.V.I.S/Experiments/HandTrackingModule.py", line 14, in __init__
self.hands = self.mpHands.Hands(self.mode,self.maxHands,
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solutions\hands.py",
line 114, in __init__
super().__init__(
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 274, in __init__
self._input_side_packets = {
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 275, in <dictcomp>
name: self._make_packet(self._side_input_type_info[name], data)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\mediapipe\python\solution_base.py", line 533, 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:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
I am using python version 3.8.3
What changes do I need to make in the code to get it to work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参数
model_complexity
缺少。它需要是整数。您正在传递检测con
。您可以添加默认值1:...或使用名为参数:
The parameter
model_complexity
is missing. It needs to be integer. You are passingdetectionCon
instead. You can add the default 1:... or use named parameters: