Human Pose estimation using OpenCV Python

Hey there everyone, today we see how we can locate joints/point of the human body and estimate its position. We will do this using OpenCV in Python. We will use an image(.jpg) file and try to estimate the posture of the person standing in that image.
So let’s get started.

HUMAN POSTURE ESTIMATION

We will use this image and try to estimate the posture of the man standing in this picture.

This will include the following four steps:

  • Loading the image and getting its dimensions.
  • making predictions
  • locating the key points
  • drawing the skeleton

Let’s see each of the above-mentioned points and try to understand them with code.

Importing the required libraries. We will import cv2 and NumPy libraries.

import cv2
import numpy as np

Output format according to the training model. This dataset has different key points corresponding to different joints of the human body.

body_parts = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
    "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
        "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
            "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }

pose_pairs = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
              ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
              ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
              ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
              ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]

Reading the image file and retrieving its height and width.

image_frame = cv2.imread("images.jpg")

height, width, channel = image_frame.shape
image_width = width
image_height = height

Loading our network to memory.

net = cv2.dnn.readNetFromTensorflow("graph_opt.pb")

Converting the image to blob format using the function blobFromImage() and loading it to the network.

net.setInput(cv2.dnn.blobFromImage(image_frame, 1.0, (image_width, image_height), (127.5, 127.5, 127.5), swapRB=True, crop=False))

Receiving the outputs for each frame using net.forward().

image_output = net.forward()
image_output = image_output[:, :19, :, :]

Declaring an empty list to store the detected joints and setting the threshold value.

joints = []
threshold_value = 0.05

Detecting the joints to be plotted.

frame_height = image_output.shape[2]
frame_width = image_output.shape[3]


for i in range(len(body_parts)):
    # generating confidence map of corresponding body's part.
    probMap = image_output[0, i, :, :]
    
    #  Global maxima of the probMap.
    minimum_value, maximum_value, min_loc, max_loc = cv2.minMaxLoc(probMap)
    
    # Scaling the point
    pt_one = (image_width * max_loc[0]) / frame_width
    pt_two = (image_height * max_loc[1]) / frame_height

    if maximum_value > threshold_value :
        joints.append((int(pt_one), int(pt_two)))

    else :
        joints.append(None)

Drawing the skeleton. This part generates the skeleton of the man standing in the picture by connecting the joints of his body with lines.

for pair in pose_pairs:
    first_body_part = pair[0]
    second_body_part = pair[1]
  
    
    part_one = body_parts[first_body_part]
    part_two = body_parts[second_body_part ]
        
    if joints[part_one] and joints[part_two]:
        
        cv2.ellipse(image_frame, joints[part_one], (4, 4), 0, 0, 360, (0, 255, 0), 2)
        cv2.ellipse(image_frame, joints[part_two], (4, 4), 0, 0, 360,(0, 255, 0), 2)
        cv2.line(image_frame, joints[part_one], joints[part_two], (255, 0, 0), 2)

Displaying the output image after complete pose estimation

cv2.imshow('OUTPUT IMAGE', image_frame)
cv2.waitKey(0)

I hope, you enjoyed this tutorial and you’ll try it on some more images.

Leave a Reply

Your email address will not be published.