Mediapipe process() 第一个“自我”参数
Posted
技术标签:
【中文标题】Mediapipe process() 第一个“自我”参数【英文标题】:Mediapipe process() first 'self' argument 【发布时间】:2021-12-11 18:50:53 【问题描述】:我正在尝试使用 mediapipe 来跟踪手部。我在 Windows 10 上使用 Python 3.7.9,我的代码如下:
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
while (True):
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = mp_hands.Hands.process(imgRGB)
print(result)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
我收到此错误:
Traceback (most recent call last):
File "C:/Users/Tomáš/PycharmProjects/pythonProject/hand_detect.py", line 11, in <module>
results = mp_hands.Hands.process(imgRGB)
TypeError: process() missing 1 required positional argument: 'image'
[ WARN:1] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
错误表示我需要在传递参数“图像”之前再传递一个参数“自我”。我已经浏览了很多,并且每个相关代码都没有在 process() 函数中使用第一个参数。谁能帮我解决这个错误?
【问题讨论】:
如果没有完全可重现的示例,很难帮助您。或许the mediapipe documentation给你答案,有静态图片的例子。 您能否检查 cv2 的文档以及此函数返回的 cvtColor。您可以在两者之间使用 breakpoint() 进行调试 【参考方案1】:问题是您在处理它之前没有创建 mp_hands.Hands 的对象。以下代码解决了它并打印了一些结果。顺便说一句,这在我之前评论过的文档链接中有很好的记录..:
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # i had problems before reading webcam feeds, so i added cv2.CAP_DSHOW here
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# you have to create an object of mp_hands.Hands to get results
# alternatively you could do: results = mp_hands.Hands().process(imgRGB)
with mp_hands.Hands() as hands:
results = hands.process(imgRGB)
# continue loop if no results were found
if not results.multi_hand_landmarks:
continue
# print some results
for hand_landmarks in results.multi_hand_landmarks:
print(
f'Index finger tip coordinates: (',
f'hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x, '
f'hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y)'
)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
编辑: 这或多或少与here 中的代码相同:
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
# initialize webcam
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
with mp_hands.Hands(model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
【讨论】:
with mp_hands.Hands() as hands:这一行导致另一个错误:FileNotFoundError: The path does not exist. 我编辑了我的帖子,尝试将第二个代码示例复制粘贴到一个新文件中并运行它。 它没有解决问题。我仍然遇到同样的错误 复制并粘贴完整的错误日志。除此之外,您可以尝试使用pip uninstall -y cv2 mediapipe
pip install cv2 mediapipe
卸载并重新安装软件包
Traceback : File "C:/Users/Tomáš/PycharmProjects/ruce/main.py", line 13, in 以上是关于Mediapipe process() 第一个“自我”参数的主要内容,如果未能解决你的问题,请参考以下文章
SignAll SDK:基于 MediaPipe 的手语接口现已开放
iOS开发之自定义的framework添加第三方framework,lipo和ar命令看.o文件