Image Pose Detection in Python Programming Language

Pose Detection FeaImg

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.

Pose Detection Demo
Pose Detection Demo

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 solutions.pose function.

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 =

pose_image = mp_pose.Pose(static_image_mode=True, 

pose_video = mp_pose.Pose(static_image_mode=False, 

mp_drawing =

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:    
                                                                               thickness=2, circle_radius=2),
                                                                               thickness=2, circle_radius=2))
    if display:
            plt.title("Input Image",size=14)
            plt.title("Pose detected Image",size=14)

        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.

Sample Outputs

Pose Detection Output 1
Pose Detection Output 1
Pose Detection Output 2
Pose Detection Output 2
Pose Detection Output 3
Pose Detection Output 3


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!