OpenPose is an open-source library written in C++ and CUDA that enables real-time multi-person keypoint detection.
It is primarily used for human pose estimation, which involves detecting and tracking key points on a person’s body, such as their elbows, knees, and shoulders, to understand their pose and movements. There are a total of 135 key points.
OpenPose was developed by researchers at Carnegie Mellon University and first released in 2016. It has since gained popularity due to its accuracy, speed, and versatility. The library uses a deep learning-based approach, leveraging convolutional neural networks (CNNs) for human body keypoint detection.
It supports various input sources such as images, videos, and webcams, making it ideal for both offline and real-time applications.
OpenPose provides pretrained models for keypoint detection and also allows users to train custom models on their own datasets for specific applications. The library offers APIs for popular programming languages like Python and C++, making it accessible to many developers and researchers.
Overall, OpenPose is a powerful tool for understanding human body movement and poses in real-time scenarios. Its use cases range from academic research to commercial applications across multiple industries. As an open-source project, it can be freely downloaded from GitHub.
OpenPose has many variants available, most of which are open source and hosted on GitHub. Since training from scratch requires significant computational resources, we will use an existing
Let's start by cloning the repository:
!git clone https://github.com/Hzzone/pytorch-openpose.git%cd /content/pytorch-openpose
Next, let's install the dependencies which are provided in the requirements.txt
file:
!pip install -q kaggle!pip3 install Cython!pip install torch!pip install numpy!pip install matplotlib!pip install opencv-python!pip install scipy!pip install scikit-image!pip install tqdm
Once the dependencies are installed, we need to download a pretrained OpenPose model from Kaggle to test on our image.
Let’s link our Kaggle API with the notebook by running the following commands:
from google.colab import filesfiles.upload()!cp kaggle.json ~/.kaggle/!chmod 600 ~/.kaggle/kaggle.json
We are supposed to upload the kaggle.json file that we got when we created a token on our Kaggle account. After your Kaggle account is linked to the notebook, we can install the following model:
! kaggle datasets download -d rkuo2000/openpose-pretrained-models! unzip /content/pytorch-openpose/openpose-pretrained-models.zip%cd /content/pytorch-openpose
Line 1: We are using the kaggle
command-line tool (installed in the Colab environment) to download a dataset named openpose-pretrained-models
from the Kaggle platform. The -d
flag specifies the dataset to download, and rkuo2000/openpose-pretrained-models
is the dataset identifier.
Line 2: We are using this shell command to unzip the downloaded ZIP file (openpose-pretrained-models.zip
) located at /content/pytorch-openpose/
.
Line 3: We are changing the current directory we are into the specified directory.
Now, we will import the necessary libraries:
import matplotlib.pyplot as pltimport numpy as npimport cv2import copyfrom src import modelfrom src import utilfrom src.body import Bodyfrom src.hand import Hand
In this code, we will be doing hand and body estimation. We have these provided in the GitHub repository, which is why we imported Body
and Hand
from src.body
and src.hand
. Let's specify the path to the hand and body estimation models:
body_pose_model = Body('/content/pytorch-openpose/body_pose_model.pth')hand_pose_model = Hand('/content/pytorch-openpose/hand_pose_model.pth')
Now we have a path to the pretrained hand and body estimation models that were available to us at the repository. Next, let's perform body key points estimation on the test image:
To apply pose estimation on the above image, write the following code:
test_image = 'images/demo.jpg'oriImg = cv2.imread(test_image)candidate, subset = body_pose_model(oriImg)canvas = copy.deepcopy(oriImg)canvas = util.draw_bodypose(canvas, candidate, subset)hands_list = util.handDetect(candidate, subset, oriImg)
The explanation of the code is as follows:
Line 1: This line assigns the file path 'images/demo.jpg'
to the variable test_image
, which now contains the path to an image file that we want to process.
Line 2: Here, cv2.imread()
function from the OpenCV library is reading the image located at the path specified by test_image
. The read image data is stored in the variable oriImg
. oriImg
is now a NumPy array representing the image.
Line 3: This line calls a function named body_pose_model()
with oriImg
as its argument. The function performs body pose estimation on the input image (oriImg
). It returns two variables candidate
and subset
. These variables contain information about detected body key points and the corresponding body pose subsets.
Lines 4–5: First, we create a deep copy of the original image (oriImg
). The deep copy ensures that any modifications made to canvas
do not affect the original image oriImg
. Then, we call a function named draw_bodypose()
from util
. It draws the detected body pose key points and skeleton on the canvas
image using the information stored in candidate
and subset
. The modified image is assigned back to the variable canvas
.
Line 6: Lastly, another function named handDetect()
from the util
module or package is called. It takes candidate
, subset
, and oriImg
as arguments and detects and extracts information about hands from the input image.
Now that we have extracted the key points, let's visualize the key points on our image by doing the following:
all_hand_peaks = []for x, y, w, is_left in hands_list:peaks = hand_estimation(oriImg[y:y+w, x:x+w, :])peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], peaks[:, 0]+x)peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y)all_hand_peaks.append(peaks)canvas = util.draw_handpose(canvas, all_hand_peaks)plt.imshow(canvas[:, :, [2, 1, 0]])plt.axis('off')plt.show()
The above code gives us the following output:
Our code visualized a bunch of key points, and that is the beauty of OpenPose. We even got the key point for the hidden arm, and they are joined, too. This shows the versatility of OpenPose.
OpenPose is a versatile and powerful library for real-time multi-person keypoint detection and pose estimation. With robust features such as multi-threading support, cross-platform compatibility, pre-trained models, and integration with popular programming languages like Python, OpenPose has become a widely used tool in fields such as sports analysis, healthcare, augmented reality, and human-computer interaction.
Its ability to accurately detect and track human body keypoints in real time, along with its customizable and extensible architecture, makes OpenPose a valuable asset for researchers, developers, and practitioners in computer vision and human pose analysis.
To run the above code in real time, press the Run button below!
Free Resources