Real-time Pose Estimation in webcam using OpenPose : Python 2/3 & OpenCV

Nisha Gandhi
Pixel-wise
Published in
3 min readAug 24, 2018
Human Pose Estimation Demo

Welcome to pixel-wise.

OpenPose is a popular Human Pose Estimation (open-source) library in C++. There have been several PyTorch, Keras, Tensorflow implementations of the same. But, the thing we all have been waiting for, Python API for OpenPose (!!) is finally here, released in June 2018 by OpenPose authors.

So what does that mean? Now we can import OpenPose and pass images (or frames) as NumPy matrices. Then, we get the key-points (pose-positions) for that image as a NumPy matrix.

NOTE: Python- OpenPose currently works for body pose, not face and hands.

So, lets get started (The installation instructions below are for Ubuntu, the instructions for the rest can be found here) :

Step 1: Install OpenPose from source

  1. Clone the repository in your local computer:
git clone https://github.com/CMU-Perceptual-Computing-Lab/openpose

2. Install CMake GUI:

sudo apt-get install cmake-qt-gui

NOTE: The steps here assume you have OpenCV, Caffe, CUDA and cuDNN installed. If not, please check the installation link for more details.

3. Open CMake GUI:

cmake-gui

4. Put the source code location & build directory location as /path/to/openpose and /path/to/openpose/build respectively:

This might prompt a window asking if build directory should be created, press yes.

5. Tick the BUILD_PYTHON checkbox.

6. Press Configure. You should now get a message saying Configuring Done . Press Generate and then close the GUI.

7. Go to the build directory and make:

cd build
make -j`nproc`

8. Installing OpenPose Python:

cd build/python
sudo make install

Add the build path to bash:

vim ~/.bashrc#(Add this line to the end of the file)
export PYTHONPATH=/path/to/openpose/build/python:

9. We are done installing OpenPose! Lets check if it works. If everything went well, you should be able to import openpose in python 2 or 3:

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpose
>>>
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpose
>>>

Step 2: Estimating Pose from web-cam using Python OpenCV

Now, lets write a simple code in Python for live-streaming with the help of the example provided by OpenPose authors:

In the program above, we first set some parameters pose model, number of GPU’s etc. And then we set up our OpenCV webcam stream. Click here for the full code on Github.

So that is it! Running this code should now give you a similar output as the one above. Pose Estimation has many exciting and fun applications such as Gesture Recognition, Gaming applications, Pose Matching and many more.

LinkedIn Profile : Know more about me!

GitHub Profile : Check out my other projects!

Stay tuned for further posts. Thank you :)

References

[1] https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/doc/installation.md

[2] https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/examples/tutorial_python/1_extract_pose.py

--

--