research-07-aug-2024 / untitled20.py
ysn-rfd's picture
Upload 20 files
014de59 verified
raw
history blame
1.26 kB
import cv2
import mediapipe as mp
# Initialize Mediapipe Pose and Drawing modules
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
# Set up pose detection
pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5)
# Initialize camera
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Flip the frame horizontally for a later selfie-view display
frame = cv2.flip(frame, 1)
# Convert the BGR image to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame and get pose landmarks
results = pose.process(rgb_frame)
# Draw landmarks on the frame
if results.pose_landmarks:
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Access and print the 3D landmarks
landmarks = results.pose_landmarks.landmark
for i, landmark in enumerate(landmarks):
print(f"Landmark {i}: x={landmark.x}, y={landmark.y}, z={landmark.z}")
# Display the frame
cv2.imshow('Pose Detection', frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()