In this tutorial, we will be implementing a program that will help to detect poses in images and mark them as landmarks on the image. Let us first understand what pose detection is.
Pose estimation is a computer vision approach for tracking a person’s or object’s motions. This is normally accomplished by locating key spots for the provided items. We will be using the
mediapipe library which is a cross-platform open-source tool for creating multimodel machine learning pipelines. It is capable of implementing cutting-edge models such as human face identification, multi-hand tracking, hair segmentation, object detection and tracking, and so on.
Implementing an Image Post Detector in Python
Let’s start off by importing all the necessary libraries that we need into the program. The libraries include the OpenCV, mediapipe, and matplotlib library.
import cv2 import mediapipe as mp import matplotlib.pyplot as plt
The very first step is to initialize our pose detection model. The same is initialized using the
Next, we will use the Pose method which is
mp_pose.Pose to store the pose_image. The function will take a number of parameters. The parameters define that we are working on images and it also sets the confidence score. We will set a similar Pose method for videos and pass the necessary parameter values.
Lastly, we will use the
drawing_utils function to draw all the pose estimated points on the image.
Look at the code below.
mp_pose = mp.solutions.pose pose_image = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5) pose_video = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7) mp_drawing = mp.solutions.drawing_utils
The function shown below is the main function which will estimate the pose after taking a few parameters to the function.
We will start off by creating a copy of the original image given by the user just to have a safe backup. Next, we will convert the image to RGB form to make the processing easier.
The next step will be to process the pose detection on the RGB converted image with the help of the
process function. Now we check the validation of the landmarks and the decision is made whether we need to draw the line and landmark points or not.
def detectPose(image_pose, pose, draw=False, display=False): original_image = image_pose.copy() image_in_RGB = cv2.cvtColor(image_pose, cv2.COLOR_BGR2RGB) resultant = pose.process(image_in_RGB) if resultant.pose_landmarks and draw: mp_drawing.draw_landmarks(image=original_image, landmark_list=resultant.pose_landmarks, connections=mp_pose.POSE_CONNECTIONS, landmark_drawing_spec=mp_drawing.DrawingSpec(color=(255,255,255), thickness=2, circle_radius=2), connection_drawing_spec=mp_drawing.DrawingSpec(color=(49,125,237), thickness=2, circle_radius=2)) if display: plt.figure(figsize=[22,22]) plt.subplot(121);plt.imshow(image_pose[:,:,::-1]) plt.title("Input Image",size=14) plt.axis('off'); plt.subplot(122);plt.imshow(original_image[:,:,::-1]) plt.title("Pose detected Image",size=14) plt.axis('off'); else: return original_image, resultant
Lastly, we will test the function in the previous code snippet above for a number of images. The outputs for some of them are shown below.
You can see that the results are soo pleasing and we can say for sure that Mediapipe has done a great job to detect poses in the images.
Thank you for reading!