如何显示左手或右手的手腕?
How to show the wrist of the left or right hand?
我想显示左手手腕的坐标,如果显示右手,则显示右手手腕的坐标。我实现了它,但它只会显示我的网络摄像头是否可以看到双手。我的代码有什么问题?我使用 mediapipe 和 opencv 作为框架。
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
ret, frame = cap.read()
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = holistic.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS )
mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
try:
wrist_left_x = results.left_hand_landmarks.landmark[0].x
wrist_left_y = results.left_hand_landmarks.landmark[0].y
wrist_right_x = results.right_hand_landmarks.landmark[0].x
wrist_right_y = results.right_hand_landmarks.landmark[0].y
if results.left_hand_landmarks:
text = wrist_left_x
text2 = wrist_left_y
if results.right_hand_landmarks:
text = wrist_right_x
text2 = wrist_right_y
cv2.putText(image, str(text), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1,
cv2.LINE_AA)
cv2.putText(image, str(text2), (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1,
cv2.LINE_AA)
except:
pass
您通常应尽可能避免使用裸 except
子句,因为您会无形地捕捉到 try
块中发生的任何错误。相反,您应该只捕获正常执行中可能出现的异常。
我怀疑,基于looking at the MediaPipe source,这里发生的事情是,当只检测到一只手时,未检测到的手的 results.{left|right}_hand_landmarks
的值为 None
.由于 None
是一个错误的值,你的 if results.left_hand_landmarks:
和 if results.right_hand_landmarks:
守卫是很好的做法*,但问题是你试图访问守卫之外的那些对象的属性,即这里:
try:
wrist_left_x = results.left_hand_landmarks.landmark[0].x
wrist_left_y = results.left_hand_landmarks.landmark[0].y
wrist_right_x = results.right_hand_landmarks.landmark[0].x
wrist_right_y = results.right_hand_landmarks.landmark[0].y
在Python中,如果这些对象是None
,这将引发错误:
AttributeError: 'NoneType' object has no attribute 'left_hand_landmarks'
尝试将代码重写为:
try:
if results.left_hand_landmarks:
text = results.left_hand_landmarks.landmark[0].x
text2 = results.left_hand_landmarks.landmark[0].y
if results.right_hand_landmarks:
text = results.right_hand_landmarks.landmark[0].x
text2 = results.right_hand_landmarks.landmark[0].y
*最佳 做法是明确使用 None
检查:if x is not None:
。由于 Python 中的每种类型都有不同的方法来确定其真值,仅依靠 if x:
来确定对象的存在可能是一个陷阱。
我想显示左手手腕的坐标,如果显示右手,则显示右手手腕的坐标。我实现了它,但它只会显示我的网络摄像头是否可以看到双手。我的代码有什么问题?我使用 mediapipe 和 opencv 作为框架。
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
ret, frame = cap.read()
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = holistic.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS )
mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
try:
wrist_left_x = results.left_hand_landmarks.landmark[0].x
wrist_left_y = results.left_hand_landmarks.landmark[0].y
wrist_right_x = results.right_hand_landmarks.landmark[0].x
wrist_right_y = results.right_hand_landmarks.landmark[0].y
if results.left_hand_landmarks:
text = wrist_left_x
text2 = wrist_left_y
if results.right_hand_landmarks:
text = wrist_right_x
text2 = wrist_right_y
cv2.putText(image, str(text), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1,
cv2.LINE_AA)
cv2.putText(image, str(text2), (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1,
cv2.LINE_AA)
except:
pass
您通常应尽可能避免使用裸 except
子句,因为您会无形地捕捉到 try
块中发生的任何错误。相反,您应该只捕获正常执行中可能出现的异常。
我怀疑,基于looking at the MediaPipe source,这里发生的事情是,当只检测到一只手时,未检测到的手的 results.{left|right}_hand_landmarks
的值为 None
.由于 None
是一个错误的值,你的 if results.left_hand_landmarks:
和 if results.right_hand_landmarks:
守卫是很好的做法*,但问题是你试图访问守卫之外的那些对象的属性,即这里:
try:
wrist_left_x = results.left_hand_landmarks.landmark[0].x
wrist_left_y = results.left_hand_landmarks.landmark[0].y
wrist_right_x = results.right_hand_landmarks.landmark[0].x
wrist_right_y = results.right_hand_landmarks.landmark[0].y
在Python中,如果这些对象是None
,这将引发错误:
AttributeError: 'NoneType' object has no attribute 'left_hand_landmarks'
尝试将代码重写为:
try:
if results.left_hand_landmarks:
text = results.left_hand_landmarks.landmark[0].x
text2 = results.left_hand_landmarks.landmark[0].y
if results.right_hand_landmarks:
text = results.right_hand_landmarks.landmark[0].x
text2 = results.right_hand_landmarks.landmark[0].y
*最佳 做法是明确使用 None
检查:if x is not None:
。由于 Python 中的每种类型都有不同的方法来确定其真值,仅依靠 if x:
来确定对象的存在可能是一个陷阱。