A Code Implementation for Advanced Human Pose Estimation Using MediaPipe, OpenCV and Matplotlib
www.marktechpost.com
Human pose estimation is a cutting-edge computer vision technology that transforms visual data into actionable insights about human movement. By utilizing advanced machine learning models like MediaPipes BlazePose and powerful libraries such as OpenCV, developers can track body key points with unprecedented accuracy. In this tutorial, we explore the seamless integration of these, demonstrating how Python-based frameworks enable sophisticated pose detection across various domains, from sports analytics to healthcare monitoring and interactive applications.First, we install the essential libraries:!pip install mediapipe opencv-python-headless matplotlibThen, we import the important libraries needed for our implementation:import cv2import mediapipe as mpimport matplotlib.pyplot as pltimport numpy as npWe initialize the MediaPipe Pose model in static image mode with segmentation enabled and a minimum detection confidence of 0.5. It also imports utilities for drawing landmarks and applying drawing styles.mp_pose = mp.solutions.posemp_drawing = mp.solutions.drawing_utilsmp_drawing_styles = mp.solutions.drawing_stylespose = mp_pose.Pose( static_image_mode=True, model_complexity=1, enable_segmentation=True, min_detection_confidence=0.5)Here, we define the detect_pose function, which reads an image, processes it to detect human pose landmarks using MediaPipe, and returns the annotated image along with the detected landmarks. If landmarks are found, they are drawn using default styling.def detect_pose(image_path): image = cv2.imread(image_path) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = pose.process(image_rgb) annotated_image = image_rgb.copy() if results.pose_landmarks: mp_drawing.draw_landmarks( annotated_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS, landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style() ) return annotated_image, results.pose_landmarksWe define the visualize_pose function, which displays the original and pose-annotated images side by side using matplotlib. The extract_keypoints function converts detected pose landmarks into a dictionary of named keypoints with their x, y, z coordinates and visibility scores.def visualize_pose(original_image, annotated_image): plt.figure(figsize=(16, 8)) plt.subplot(1, 2, 1) plt.title('Original Image') plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.subplot(1, 2, 2) plt.title('Pose Estimation') plt.imshow(annotated_image) plt.axis('off') plt.tight_layout() plt.show()def extract_keypoints(landmarks): if landmarks: keypoints = {} for idx, landmark in enumerate(landmarks.landmark): keypoints[mp_pose.PoseLandmark(idx).name] = { 'x': landmark.x, 'y': landmark.y, 'z': landmark.z, 'visibility': landmark.visibility } return keypoints return NoneFinally, we load an image from the specified path, detect and visualize human pose landmarks using MediaPipe, and then extract and print the coordinates and visibility of each detected keypoint.image_path = '/content/Screenshot 2025-03-26 at 12.56.05 AM.png'original_image = cv2.imread(image_path)annotated_image, landmarks = detect_pose(image_path)visualize_pose(original_image, annotated_image)keypoints = extract_keypoints(landmarks)if keypoints: print("Detected Keypoints:") for name, details in keypoints.items(): print(f"{name}: {details}")Sample Processed OutputIn this tutorial, we explored human pose estimation using MediaPipe and OpenCV, demonstrating a comprehensive approach to body keypoint detection. We implemented a robust pipeline that transforms images into detailed skeletal maps, covering key steps including library installation, pose detection function creation, visualization techniques, and keypoint extraction. Using advanced machine learning models, we showcased how developers can transform raw visual data into meaningful movement insights across various domains like sports analytics and healthcare monitoring.Here is the Colab Notebook. Also,dont forget to follow us onTwitterand join ourTelegram ChannelandLinkedIn Group. Dont Forget to join our85k+ ML SubReddit. Asif RazzaqWebsite| + postsBioAsif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.Asif Razzaqhttps://www.marktechpost.com/author/6flvq/A Coding Implementation of Extracting Structured Data Using LangSmith, Pydantic, LangChain, and Claude 3.7 SonnetAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Meet LocAgent: Graph-Based AI Agents Transforming Code Localization for Scalable Software MaintenanceAsif Razzaqhttps://www.marktechpost.com/author/6flvq/A Coding Implementation to Build a Conversational Research Assistant with FAISS, Langchain, Pypdf, and TinyLlama-1.1B-Chat-v1.0Asif Razzaqhttps://www.marktechpost.com/author/6flvq/Sea AI Lab Researchers Introduce Dr. GRPO: A Bias-Free Reinforcement Learning Method that Enhances Math Reasoning Accuracy in Large Language Models Without Inflating Responses
0 Comments
·0 Shares
·58 Views