python_api.md 8.0 KB
Newer Older
1 2
OpenPose - Python API
====================================
G
gineshidalgo99 已提交
3 4 5

## Contents
1. [Introduction](#introduction)
6 7
2. [Compatibility](#compatibility)
3. [Installation](#installation)
G
gineshidalgo99 已提交
8
4. [Testing](#testing)
9
5. [Exporting Python OpenPose](#exporting-python-openpose)
G
gineshidalgo99 已提交
10
6. [Common Issues](#common-issues)
11

G
gineshidalgo99 已提交
12 13 14


## Introduction
R
Raaj 已提交
15
This module exposes a Python API for OpenPose. It is effectively a wrapper that replicates most of the functionality of the [op::Wrapper class](https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/include/openpose/wrapper/wrapper.hpp) and allows you to populate and retrieve data from the [op::Datum class](https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/include/openpose/core/datum.hpp) using standard Python and Numpy constructs.
G
gineshidalgo99 已提交
16

17 18 19 20 21 22
The Python API is analagous to the C++ function calls. You may find them in [python/openpose/openpose_python.cpp#L194](https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/python/openpose/openpose_python.cpp#L194).

The Python API is rather simple: `op::Array<float>` and `cv::Mat` objects get casted to numpy arrays automatically. Every other data structure based on the standard library is automatically converted into Python objects. For example, an `std::vector<std::vector<float>>` would become `[[item, item], [item, item]]`, etc. We also provide a casting of `op::Rectangle` and `op::Point` which simply expose setter getter for [x, y, width, height], etc.



G
gineshidalgo99 已提交
23

R
Raaj 已提交
24

25
## Compatibility
G
gineshidalgo99 已提交
26
The OpenPose Python module is compatible with both Python 2 and Python 3 (default and recommended). In addition, it will also run in all OpenPose compatible operating systems. It uses [Pybind11](https://github.com/pybind/pybind11) for mapping between C++ and Python datatypes.
R
Raaj 已提交
27

28
To compile, enable `BUILD_PYTHON` in CMake-gui, or run `cmake -DBUILD_PYTHON=ON ..` from your build directory. In Windows, make sure you compile the whole solution (clicking the green play button does not compile the whole solution!). You can do that by right-click on the OpenPose project solution, and clicking in `Build Solution` (or individually building the PyOpenPose module).
R
Raaj 已提交
29

G
gineshidalgo99 已提交
30
Pybind selects the latest version of Python by default (Python 3). To use Python 2, change `PYTHON_EXECUTABLE` and `PYTHON_LIBRARY` flags in CMake-gui to your desired Python version.
R
Raaj 已提交
31 32

```
G
gineshidalgo99 已提交
33 34
# Ubuntu
PYTHON_EXECUTABLE=/usr/bin/python2.7
R
Raaj 已提交
35 36 37 38
PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7m.so
```

```
G
gineshidalgo99 已提交
39 40 41
# Mac OSX
PYTHON_EXECUTABLE=/usr/local/bin/python2.7
PYTHON_LIBRARY=/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7m.dylib
R
Raaj 已提交
42 43 44
```

```
G
gineshidalgo99 已提交
45
:: Windows
R
Raaj 已提交
46 47 48
PYTHON_EXECUTABLE=C:/Users/user/AppData/Local/Programs/Python/Python27/python.exe
```

G
gineshidalgo99 已提交
49
If run via the command line, you may need to run cmake twice in order for this change to take effect.
50 51 52 53



## Installation
54
Make sure you followed the Python steps in [doc/installation/README.md#cmake-configuration](installation/README.md#cmake-configuration).
G
gineshidalgo99 已提交
55 56 57 58



## Testing
R
Raaj 已提交
59
All the Python examples from the Tutorial API Python module can be found in `build/examples/tutorial_api_python` in your build folder. Navigate directly to this path to run examples.
G
gineshidalgo99 已提交
60 61 62

```
# From command line
G
gineshidalgo99 已提交
63
cd build/examples/tutorial_api_python
64

G
gineshidalgo99 已提交
65
# Python 3 (default version)
G
Gines Hidalgo 已提交
66 67 68
python3 01_body_from_image.py
python3 02_whole_body_from_image.py
# python3 [any_other_python_example.py]
G
gineshidalgo99 已提交
69

70
# Python 2
G
Gines Hidalgo 已提交
71 72 73
python2 01_body_from_image.py
python2 02_whole_body_from_image.py
# python2 [any_other_python_example.py]
G
gineshidalgo99 已提交
74 75
```

R
Raaj 已提交
76 77


78
## Exporting Python OpenPose
G
gineshidalgo99 已提交
79
Note: This step is only required if you are moving the `*.py` files outside their original location, or writting new `*.py` scripts outside `build/examples/tutorial_api_python`.
80

R
Raaj 已提交
81 82
Ubuntu/OSX:

G
Gines Hidalgo 已提交
83 84
- Option a, installing OpenPose: On an Ubuntu or OSX based system, you could install OpenPose by running `sudo make install`, you could then set the OpenPose path in your python scripts to the OpenPose installation path (default: `/usr/local/python`) and start using OpenPose at any location. Take a look at `build/examples/tutorial_api_python/01_body_from_image.py` for an example.
- Option b, not installing OpenPose: To move the OpenPose Python API demos to a different folder, ensure that the line `sys.path.append('{OpenPose_path}/python')` is properly set in your `*.py` files, where `{OpenPose_path}` points to your build folder of OpenPose. Take a look at `build/examples/tutorial_api_python/01_body_from_image.py` for an example.
R
Raaj 已提交
85 86 87 88 89 90 91 92 93 94 95

Windows:

- Ensure that the folder  `build/x{86/64}/Release`and `build/bin` are copied along with `build/python` As noted in the example, the path for these can be changed in the following two variables:

  ```
  sys.path.append(dir_path + '/../../python/openpose/Release);
  os.environ['PATH']  = os.environ['PATH'] + ';' + dir_path + '/../../{x86/x64}/Release;' +  dir_path + '/../../bin;'
  ```


G
gineshidalgo99 已提交
96 97

## Common Issues
98 99 100 101 102
### Do not use PIL
In order to read images in Python, make sure to use OpenCV (do not use PIL). We found that feeding a PIL image format to OpenPose results in the input image appearing in grey and duplicated 9 times (so the output skeleton appear 3 times smaller than they should be, and duplicated 9 times).


### Cannot Import Name PyOpenPose
G
gineshidalgo99 已提交
103
The error in general is that PyOpenPose cannot be found (an error similar to: `ImportError: cannot import name pyopenpose`). Ensure first that `BUILD_PYTHON` flag is set to ON. If the error persists, check the following:
R
Raaj 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

In the script you are running, check for the following line, and run the following command in the same location as where the file is

**Ubuntu/OSX:**

`sys.path.append('../../python');`

```
ls ../../python/openpose
```

Check the contents of this location. It should contain one of the following files:

```
pyopenpose.cpython-35m-x86_64-linux-gnu.so
pyopenpose.so
```

G
gineshidalgo99 已提交
122
If you do not have any one of those, you may not have compiled openpose successfully, or you may be running the examples, not from the build folder but the source folder. If you have the first one, you have compiled PyOpenPose for Python 3, and have to run the scripts with `python3`, and vice versa for the 2nd one. Follow the testing examples above for exact commands.
R
Raaj 已提交
123 124 125

**Windows:**

G
gineshidalgo99 已提交
126 127 128 129 130 131 132 133
Problem 1: If you are in Windows, and you fail to install the required third party Python libraries, it might print an error similar to: `Exception: Error: OpenPose library could not be found. Did you enable BUILD_PYTHON in CMake and have this Python script in the right folder?`. From GitHub issue #941:
```
I had a similar issue with Visual Studio (VS). I am pretty sure that the issue is that while you are compiling OpenPose in VS, it tries to import cv2 (python-opencv) and it fails. So make sure that if you open cmd.exe and run Python, you can actually import cv2 without errors. I could not, but I had cv2 installed in a IPython environment (Anaconda), so I activated that environment, and then ran (change this to adapt it to your VS version and location of OpenPose.sln):

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild.exe C:\path\to\OpenPose.sln
```

Problem 2: Python for Openpose needs to be compiled in Release mode for now. This can be done in [Visual Studio](https://cdn.stereolabs.com/docs/getting-started/images/release_mode.png). Once that is done check this line:
R
Raaj 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147

`sys.path.append(dir_path + '/../../python/openpose/Release');`

```
dir ../../python/openpose/Release
```

Check the contents of this location. It should contain one of the following files:

```
pyopenpose.cp36-win_amd64.pyd
pyopenpose.pyd
```

G
gineshidalgo99 已提交
148
If such a folder does not exist, you need to compile in Release mode as seen above. If you have the first one, you have compiled PyOpenPose for Python 3, and have to run the scripts with `python3`, and vice versa for the 2nd one. Follow the testing examples above for exact commands. If that still does not work, check this line:
R
Raaj 已提交
149 150 151 152 153 154 155 156

`os.environ['PATH']  = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' +  dir_path + '/../../bin;'`

```
dir ../../x64/Release
dir ../../bin
```

G
gineshidalgo99 已提交
157
Ensure that both of these paths exist, as PyOpenPose needs to reference those libraries. If they don't exist, change the path so that they point to the correct location in your build folder.