Skip to content

Commit

Permalink
Python fix (#674)
Browse files Browse the repository at this point in the history
* Bug fixes for Python Windows and Cmake
* Update to python doc
* Added BUILD_DLL
  • Loading branch information
soulslicer authored and gineshidalgo99 committed Jun 26, 2018
1 parent 62eebd6 commit c8e3879
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 659 deletions.
64 changes: 51 additions & 13 deletions CMakeLists.txt
Expand Up @@ -235,9 +235,12 @@ option(DOWNLOAD_HAND_MODEL "Download hand model." ON)
option(BUILD_EXAMPLES "Build OpenPose examples." ON)
option(BUILD_DOCS "Build OpenPose documentation." OFF)
option(BUILD_PYTHON "Build OpenPose python." OFF)
if (WIN32)
option(BUILD_DLL "Copy all required DLL files into the same folder." ON)
endif ()

# Build as shared library
option(BUILD_SHARED_LIBS "Build as shared lib" ON)
option(BUILD_SHARED_LIBS "Build as shared lib." ON)

# Speed profiler
option(PROFILER_ENABLED "If enabled, OpenPose will be able to print out speed information at runtime." OFF)
Expand Down Expand Up @@ -467,14 +470,18 @@ if (WIN32)
find_library(Caffe_Proto_LIB caffeproto HINTS ${FIND_LIB_PREFIX}/caffe/lib)
endif (${GPU_MODE} MATCHES "CPU_ONLY")
endif (${GPU_MODE} MATCHES "OPENCL")

if (${GPU_MODE} MATCHES "OPENCL")
unset(BOOST_SYSTEM_LIB_RELEASE CACHE)
unset(BOOST_SYSTEM_LIB_DEBUG CACHE)
find_library(BOOST_SYSTEM_LIB_RELEASE boost_system-vc140-mt-1_61 HINTS ${FIND_LIB_PREFIX}/caffe3rdparty/lib)
find_library(BOOST_SYSTEM_LIB_DEBUG boost_system-vc140-mt-gd-1_61 HINTS ${FIND_LIB_PREFIX}/caffe3rdparty/lib)
endif (${GPU_MODE} MATCHES "OPENCL")

# Boost DepCopy over required DLL F
if (${GPU_MODE} MATCHES "CPU_ONLY" OR ${GPU_MODE} MATCHES "OPENCL" OR BUILD_PYTHON)
find_library(BOOST_SYSTEM_LIB_RELEASE libboost_system-vc140-mt-1_61 HINTS ${FIND_LIB_PREFIX}/caffe3rdparty/lib)
find_library(BOOST_SYSTEM_LIB_DEBUG libboost_system-vc140-mt-gd-1_61 HINTS ${FIND_LIB_PREFIX}/caffe3rdparty/lib)
find_library(BOOST_FILESYSTEM_LIB_RELEASE libboost_filesystem-vc140-mt-1_61 HINTS ${FIND_LIB_PREFIX}/caffe3rdparty/lib)
find_library(BOOST_FILESYSTEM_LIB_DEBUG libboost_filesystem-vc140-mt-gd-1_61 HINTS ${FIND_LIB_PREFIX}/caffe3rdparty/lib)
else ()
set(BOOST_SYSTEM_LIB_RELEASE "")
set(BOOST_SYSTEM_LIB_DEBUG "")
set(BOOST_FILESYSTEM_LIB_RELEASE "")
set(BOOST_FILESYSTEM_LIB_DEBUG "")
endif ()
if (WITH_3D_RENDERER)
find_library(GLUT_LIBRARY freeglut HINTS ${FIND_LIB_PREFIX}/freeglut/lib)
message(STATUS "\${GLUT_LIBRARY} = ${GLUT_LIBRARY}")
Expand Down Expand Up @@ -507,6 +514,32 @@ if (WIN32)
set(SPINNAKER_INCLUDE_DIRS "3rdparty/windows/spinnaker/include")
endif (WITH_FLIR_CAMERA)
set(Caffe_FOUND 1)

# Build DLL Must be on if Build Python is on
if (BUILD_PYTHON)
if (NOT BUILD_DLL)
message(FATAL_ERROR "BUILD_DLL must be turned on to as well to build python library")
endif ()
endif ()

# Auto copy DLLs
if (BUILD_DLL)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Auto copy DLLs
if (${GPU_MODE} MATCHES "CUDA")
file(GLOB CAFFE_DLL "${CMAKE_SOURCE_DIR}/3rdparty/windows/caffe/bin/*.dll")
elseif (${GPU_MODE} MATCHES "OPENCL")
file(GLOB CAFFE_DLL "${CMAKE_SOURCE_DIR}/3rdparty/windows/caffe_opencl/bin/*.dll")
elseif (${GPU_MODE} MATCHES "CPU_ONLY")
file(GLOB CAFFE_DLL "${CMAKE_SOURCE_DIR}/3rdparty/windows/caffe_cpu/bin/*.dll")
endif ()
file(GLOB OPENCV_DLL "${CMAKE_SOURCE_DIR}/3rdparty/windows/opencv/x64/vc14/bin/*.dll")
file(GLOB OPENCV3PTY_DLL "${CMAKE_SOURCE_DIR}/3rdparty/windows/caffe3rdparty/lib/*.dll")
file(COPY ${CAFFE_DLL} DESTINATION ${CMAKE_BINARY_DIR}/lib)
file(COPY ${OPENCV_DLL} DESTINATION ${CMAKE_BINARY_DIR}/lib)
file(COPY ${OPENCV3PTY_DLL} DESTINATION ${CMAKE_BINARY_DIR}/lib)
endif ()

endif (WIN32)


Expand Down Expand Up @@ -739,11 +772,16 @@ if (USE_MKL)
endif (USE_MKL)
if (${GPU_MODE} MATCHES "OPENCL")
set(OpenPose_3rdparty_libraries ${OpenPose_3rdparty_libraries} ${CMAKE_THREAD_LIBS_INIT} ${OpenCL_LIBRARIES})
if (WIN32)
set(OpenPose_3rdparty_libraries ${OpenPose_3rdparty_libraries}
debug ${BOOST_SYSTEM_LIB_DEBUG} optimized ${BOOST_SYSTEM_LIB_RELEASE})
endif (WIN32)
endif (${GPU_MODE} MATCHES "OPENCL")
# Boost
if (WIN32)
if (${GPU_MODE} MATCHES "CPU_ONLY" OR ${GPU_MODE} MATCHES "OPENCL" OR BUILD_PYTHON)
set(OpenPose_3rdparty_libraries ${OpenPose_3rdparty_libraries}
debug ${BOOST_SYSTEM_LIB_RELEASE} optimized ${BOOST_SYSTEM_LIB_RELEASE})
set(OpenPose_3rdparty_libraries ${OpenPose_3rdparty_libraries}
debug ${BOOST_FILESYSTEM_LIB_RELEASE} optimized ${BOOST_FILESYSTEM_LIB_RELEASE})
endif ()
endif (WIN32)
# 3-D
if (WITH_3D_ADAM_MODEL)
set(OpenPose_3rdparty_libraries ${OpenPose_3rdparty_libraries}
Expand Down
67 changes: 18 additions & 49 deletions doc/modules/python_module.md
Expand Up @@ -9,14 +9,23 @@ OpenPose Python Module


## Introduction
This experimental module exposes a Python API for OpenPose. This allows you to construct an OpenPose object, pass in a numpy array for an image, and get a numpy array of the pose positions. This API also exposes an API that allows you to directly pass in heatmaps from a network and extract poses out of it.

This experimental module exposes a Python API for OpenPose. This allows you to construct an OpenPose object, pass in a numpy array for an image, and get a numpy array of the pose positions. This API also exposes an API that allows you to directly pass in heatmaps from a network and extract poses out of it (Requires Python Caffe to be installed seperately)

At present the Python API only supports body pose. Hands and Face will be added in the future.

## Installation
Check [doc/installation.md#python-module](./installation.md#python-module) for installation steps.

To simply test the OpenPose API in your project without installation, ensure that the line `sys.path.append('{OpenPose_path}/python')` is set in your *.py files, where `{OpenPose_path}` points to your build folder of OpenPose. Take a look at `build/examples/tutorial_pose/1_extract_pose.py` for an example.

On an Ubuntu or OSX based system, you may use it globally. Running `sudo make install` will install OpenPose by default into `/usr/local/python`. You can set this into your python path and start using it at any location.

The Python API requires Numpy for array management, and OpenCV for image loading. They can be installed via:

```
pip install numpy
pip install opencv-python
```

## Compatibility
The OpenPose Python module is compatible with both Python 2 and Python 3. In addition, it will also run in all OpenPose compatible operating systems.
Expand All @@ -26,56 +35,16 @@ The OpenPose Python module is compatible with both Python 2 and Python 3. In add
## Testing
Two examples can be found in `build/examples/tutorial_python` in your build folder. Navigate directly to this path to run examples.

- `1_extract_pose` demonstrates a simple use of the API.
- `2_pose_from_heatmaps` demonstrates constructing pose from heatmaps from the caffe network.
- `1_extract_pose` demonstrates a simple use of the API.
- `2_pose_from_heatmaps` demonstrates constructing pose from heatmaps from the caffe network. (Requires Python Caffe to be installed seperately)

```
# From command line
cd build/examples/tutorial_python
python
python 1_extract_pose.py
```

```python
# From Python
# It requires OpenCV installed for Python
import cv2
import os
import sys

# Remember to add your installation path here
# Option a
sys.path.append('{OpenPose_path}/python')
# Option b
# If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
# sys.path.append('/usr/local/python')

from openpose import *

# Parameters for OpenPose. Take a look at C++ OpenPose example for meaning of components. Ensure all below are filled
params = dict()
params["logging_level"] = 3
params["output_resolution"] = "-1x-1"
params["net_resolution"] = "-1x368"
params["model_pose"] = "BODY_25"
params["alpha_pose"] = 0.6
params["scale_gap"] = 0.3
params["scale_number"] = 1
params["render_threshold"] = 0.05
params["num_gpu_start"] = 0
# If GPU version is built, and multiple GPUs are available, set the ID here
params["disable_blending"] = False
params["default_model_folder"] = "/home/user/openpose/models"
# Construct OpenPose object allocates GPU memory
openpose = OpenPose(params)

while 1:
# Read new image
img = cv2.imread("image.png")
# Output keypoints and the image with the human skeleton blended on it
keypoints, output_image = openpose.forward(img, True)
# Print the human pose keypoints, i.e., a [#people x #keypoints x 3]-dimensional numpy object with the keypoints of all the people on that image
print keypoints
# Display the image
cv2.imshow("output", output_image)
cv2.waitKey(15)
```


## Code Sample
See `examples/tutorial_python/1_extract_pose.py`.
30 changes: 24 additions & 6 deletions examples/tutorial_python/1_extract_pose.py
@@ -1,27 +1,45 @@
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform

# Remember to add your installation path here
# Option a
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append('../../python')
from openpose import *
if platform == "win32": sys.path.append(dir_path + '/../../python/openpose/');
else: sys.path.append('../../python');
# Option b
# If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
# sys.path.append('/usr/local/python')

# Parameters for OpenPose. Take a look at C++ OpenPose example for meaning of components. Ensure all below are filled
from openpose import *
params = dict()
params["logging_level"] = 3
params["output_resolution"] = "-1x-1"
params["net_resolution"] = "-1x368"
params["model_pose"] = "COCO"
params["model_pose"] = "BODY_25"
params["alpha_pose"] = 0.6
params["scale_gap"] = 0.3
params["scale_number"] = 1
params["render_threshold"] = 0.05
# If GPU version is built, and multiple GPUs are available, set the ID here
params["num_gpu_start"] = 0
params["disable_blending"] = False
# Ensure you point to the correct path where models are located
params["default_model_folder"] = dir_path + "/../../../models/"
# Construct OpenPose object allocates GPU memory
openpose = OpenPose(params)
img = cv2.imread(dir_path + "/../../../examples/media/COCO_val2014_000000000192.jpg")
arr, output_image = openpose.forward(img, True)
print arr

while 1:
# Read new image
img = cv2.imread("image.png")
# Output keypoints and the image with the human skeleton blended on it
keypoints, output_image = openpose.forward(img, True)
# Print the human pose keypoints, i.e., a [#people x #keypoints x 3]-dimensional numpy object with the keypoints of all the people on that image
print(keypoints)
# Display the image
cv2.imshow("output", output_image)
cv2.waitKey(15)
13 changes: 11 additions & 2 deletions examples/tutorial_python/2_pose_from_heatmaps.py
@@ -1,3 +1,12 @@
from sys import platform
import sys
try:
import caffe
except ImportError:
print("This sample can only be run if Python Caffe if available on your system")
print("Currently OpenPose does not compile Python Caffe. This may be supported in the future")
sys.exit(-1)

import os
os.environ["GLOG_minloglevel"] = "1"
import caffe
Expand Down Expand Up @@ -36,7 +45,7 @@ class Param:
nets = []
for scale in scales:
nets.append(caffe.Net(Param.prototxt, Param.caffemodel, caffe.TEST))
print "Net loaded"
print("Net loaded")

# Test Function
first_run = True
Expand All @@ -57,7 +66,7 @@ def func(frame):
net.reshape()

first_run = False
print "Reshaped"
print("Reshaped")

# Forward pass to get heatmaps
heatmaps = []
Expand Down
4 changes: 2 additions & 2 deletions python/CMakeLists.txt
@@ -1,2 +1,2 @@
add_subdirectory(openpose)

add_subdirectory(openpose)

28 changes: 14 additions & 14 deletions python/openpose/CMakeLists.txt
@@ -1,14 +1,14 @@
set(PYTHON_FILES
openpose.py
__init__.py
_openpose.cpp)

add_library(_openpose SHARED ${PYTHON_FILES})
target_link_libraries(_openpose openpose ${GLOG_LIBRARY} ${GFLAGS_LIBRARY} ${Caffe_LIBS} ${MKL_LIBS} ${GLUT_LIBRARY} ${SPINNAKER_LIB} ${OpenCL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
SET_TARGET_PROPERTIES(_openpose PROPERTIES PREFIX "")
configure_file(openpose.py openpose.py)
configure_file(__init__.py __init__.py)

#install(TARGETS _openpose DESTINATION python)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION python/openpose FILES_MATCHING PATTERN "*.so")
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION python/openpose FILES_MATCHING PATTERN "*.py")
set(PYTHON_FILES
openpose.py
__init__.py
_openpose.cpp)

add_library(_openpose SHARED ${PYTHON_FILES})
target_link_libraries(_openpose openpose ${OpenPose_3rdparty_libraries})
SET_TARGET_PROPERTIES(_openpose PROPERTIES PREFIX "")
configure_file(openpose.py openpose.py)
configure_file(__init__.py __init__.py)

#install(TARGETS _openpose DESTINATION python)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION python/openpose FILES_MATCHING PATTERN "*.so")
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION python/openpose FILES_MATCHING PATTERN "*.py")
2 changes: 1 addition & 1 deletion python/openpose/__init__.py
@@ -1 +1 @@
from openpose import *
from openpose import *

0 comments on commit c8e3879

Please sign in to comment.