这是我使用 mediapipe 绘制地标的代码
import cv2
import time
import mediapipe as mp
mp_holistic = mp.solutions.holistic
holistic_model = mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
# Initializing the drawing utils for drawing the facial landmarks on image
mp_drawing = mp.solutions.drawing_utils
# (0) in VideoCapture is used to connect to your computer's default camera
capture = cv2.VideoCapture(0)
# Initializing current time and precious time for calculating the FPS
previousTime = 0
currentTime = 0
while capture.isOpened():
# capture frame by frame
ret, frame = capture.read()
# resizing the frame for better view
frame = cv2.resize(frame, (800, 600))
# Converting the from BGR to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Making predictions using holistic model
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
results = holistic_model.process(image)
image.flags.writeable = True
# Converting back the RGB image to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Drawing the Facial Landmarks
# mp_drawing.draw_landmarks(
# image,
# results.face_landmarks,
# mp_holistic.FACEMESH_CONTOURS,
# mp_drawing.DrawingSpec(
# color=(255, 0, 255),
# thickness=1,
# circle_radius=1
# ),
# mp_drawing.DrawingSpec(
# color=(0, 255, 255),
# thickness=1,
# circle_radius=1
# )
# )
# Drawing Right hand Land Marks
mp_drawing.draw_landmarks(
image,
results.right_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
)
for landmark in mp_holistic.HandLandmark:
print(landmark,landmark.value)
# Drawing Left hand Land Marks
mp_drawing.draw_landmarks(
image,
results.left_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
)
# Calculating the FPS
currentTime = time.time()
fps = 1 / (currentTime - previousTime)
previousTime = currentTime
# Displaying FPS on the image
cv2.putText(image, str(int(fps)) + " FPS", (10, 70), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
# Display the resulting image
cv2.imshow("Facial and Hand Landmarks", image)
# Enter key 'q' to break the loop
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# When all the process is done
# Release the capture and destroy all windows
capture.release()
cv2.destroyAllWindows()
适用于左手和右手,但我的目标是:在手之间画直线,为了理解我的问题,让我们讨论以下图像:
从图中我们可以说,例如,双手之间的线应该是完全水平的(因此角度应该为零,就好像我们考虑以下方程:
很容易说,为了成为完美的水平线,y 坐标应该彼此相等,所以请帮我检测如何确定坐标以及如何在两点之间画线?由于 cv2.line 存在,绘制很容易,但是坐标呢?
您正在使用内置的 Mediapipe 函数
draw_landmarks
来处理所有绘图。此函数将图像、规范化的地标列表和连接作为输入。但是,NormalizedLandmarkList
Mediapipe 中的类型不支持合并多个地标列表,因此很难将双手的地标传递给该函数并找到连接。另一种方法是从手部标志中提取坐标,然后使用
cv2.line
您提到的方法绘制一条线。以下是实现此方法的代码:如果您想选择除之外的其他地标
wrist
,请点击此链接查看地标索引。