diff --git a/doc/modules/python_module.md b/doc/modules/python_module.md index 41ec890f..ee6db4e5 100644 --- a/doc/modules/python_module.md +++ b/doc/modules/python_module.md @@ -74,14 +74,14 @@ All the Python examples from the Tutorial API Python module can be found in `bui cd build/examples/tutorial_api_python # Python 3 (default version) -python3 1_body_from_image.py -python3 2_whole_body_from_image.py -# python3 [any_other_example.py] +python3 01_body_from_image.py +python3 02_whole_body_from_image.py +# python3 [any_other_python_example.py] # Python 2 -python2 1_body_from_image.py -python2 2_whole_body_from_image.py -# python2 [any_other_example.py] +python2 01_body_from_image.py +python2 02_whole_body_from_image.py +# python2 [any_other_python_example.py] ``` @@ -91,8 +91,8 @@ Note: This step is only required if you are moving the `*.py` files outside thei Ubuntu/OSX: -- 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/1_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/1_body_from_image.py` for an example. +- 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. Windows: diff --git a/examples/tutorial_api_python/01_body_from_image.py b/examples/tutorial_api_python/01_body_from_image.py index 7ca220e5..337aa275 100644 --- a/examples/tutorial_api_python/01_body_from_image.py +++ b/examples/tutorial_api_python/01_body_from_image.py @@ -6,51 +6,51 @@ import os from sys import platform import argparse -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -67,5 +67,5 @@ try: cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) cv2.waitKey(0) except Exception as e: - # print(e) + print(e) sys.exit(-1) diff --git a/examples/tutorial_api_python/02_whole_body_from_image.py b/examples/tutorial_api_python/02_whole_body_from_image.py index ecec10d5..d1e70b5d 100644 --- a/examples/tutorial_api_python/02_whole_body_from_image.py +++ b/examples/tutorial_api_python/02_whole_body_from_image.py @@ -6,53 +6,53 @@ import os from sys import platform import argparse -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" -params["face"] = True -params["hand"] = True + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" + params["face"] = True + params["hand"] = True -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -72,5 +72,5 @@ try: cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) cv2.waitKey(0) except Exception as e: - # print(e) + print(e) sys.exit(-1) diff --git a/examples/tutorial_api_python/04_keypoints_from_images.py b/examples/tutorial_api_python/04_keypoints_from_images.py index 6fd0697f..f37a0a03 100644 --- a/examples/tutorial_api_python/04_keypoints_from_images.py +++ b/examples/tutorial_api_python/04_keypoints_from_images.py @@ -7,52 +7,52 @@ from sys import platform import argparse import time -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_dir", default="../../../examples/media/", help="Process a directory of images. Read all standard formats (jpg, png, bmp, etc.).") -parser.add_argument("--no_display", default=False, help="Enable to disable the visual display.") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_dir", default="../../../examples/media/", help="Process a directory of images. Read all standard formats (jpg, png, bmp, etc.).") + parser.add_argument("--no_display", default=False, help="Enable to disable the visual display.") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -79,5 +79,5 @@ try: end = time.time() print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") except Exception as e: - # print(e) + print(e) sys.exit(-1) diff --git a/examples/tutorial_api_python/05_keypoints_from_images_multi_gpu.py b/examples/tutorial_api_python/05_keypoints_from_images_multi_gpu.py index 79574363..59f0b44b 100644 --- a/examples/tutorial_api_python/05_keypoints_from_images_multi_gpu.py +++ b/examples/tutorial_api_python/05_keypoints_from_images_multi_gpu.py @@ -7,55 +7,55 @@ from sys import platform import argparse import time -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_dir", default="../../../examples/media/", help="Process a directory of images. Read all standard formats (jpg, png, bmp, etc.).") -parser.add_argument("--no_display", default=False, help="Enable to disable the visual display.") -parser.add_argument("--num_gpu", default=op.get_gpu_number(), help="Number of GPUs.") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_dir", default="../../../examples/media/", help="Process a directory of images. Read all standard formats (jpg, png, bmp, etc.).") + parser.add_argument("--no_display", default=False, help="Enable to disable the visual display.") + parser.add_argument("--num_gpu", default=op.get_gpu_number(), help="Number of GPUs.") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" -params["num_gpu"] = int(vars(args[0])["num_gpu"]) -numberGPUs = int(params["num_gpu"]) + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" + params["num_gpu"] = int(vars(args[0])["num_gpu"]) + numberGPUs = int(params["num_gpu"]) -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -106,5 +106,5 @@ try: end = time.time() print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") except Exception as e: - # print(e) + print(e) sys.exit(-1) diff --git a/examples/tutorial_api_python/06_face_from_image.py b/examples/tutorial_api_python/06_face_from_image.py index ebc115d1..c1f572bd 100644 --- a/examples/tutorial_api_python/06_face_from_image.py +++ b/examples/tutorial_api_python/06_face_from_image.py @@ -7,54 +7,54 @@ from sys import platform import argparse import time -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" -params["face"] = True -params["face_detector"] = 2 -params["body"] = 0 + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" + params["face"] = True + params["face_detector"] = 2 + params["body"] = 0 -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -79,5 +79,5 @@ try: cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) cv2.waitKey(0) except Exception as e: - # print(e) + print(e) sys.exit(-1) diff --git a/examples/tutorial_api_python/07_hand_from_image.py b/examples/tutorial_api_python/07_hand_from_image.py index 50f18691..fab69535 100644 --- a/examples/tutorial_api_python/07_hand_from_image.py +++ b/examples/tutorial_api_python/07_hand_from_image.py @@ -7,54 +7,54 @@ from sys import platform import argparse import time -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" -params["hand"] = True -params["hand_detector"] = 2 -params["body"] = 0 + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" + params["hand"] = True + params["hand_detector"] = 2 + params["body"] = 0 -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -92,5 +92,5 @@ try: cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) cv2.waitKey(0) except Exception as e: - # print(e) + print(e) sys.exit(-1) diff --git a/examples/tutorial_api_python/08_heatmaps_from_image.py b/examples/tutorial_api_python/08_heatmaps_from_image.py index 18746f86..66ed3d39 100644 --- a/examples/tutorial_api_python/08_heatmaps_from_image.py +++ b/examples/tutorial_api_python/08_heatmaps_from_image.py @@ -6,55 +6,55 @@ import os from sys import platform import argparse -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" -params["heatmaps_add_parts"] = True -params["heatmaps_add_bkg"] = True -params["heatmaps_add_PAFs"] = True -params["heatmaps_scale"] = 2 + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" + params["heatmaps_add_parts"] = True + params["heatmaps_add_bkg"] = True + params["heatmaps_add_PAFs"] = True + params["heatmaps_scale"] = 2 -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -try: # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) @@ -87,5 +87,5 @@ try: counter += 1 counter = counter % num_maps except Exception as e: - # print(e) - sys.exit(-1) \ No newline at end of file + print(e) + sys.exit(-1) diff --git a/examples/tutorial_api_python/09_keypoints_from_heatmaps.py b/examples/tutorial_api_python/09_keypoints_from_heatmaps.py index d82f2498..d9faa68c 100644 --- a/examples/tutorial_api_python/09_keypoints_from_heatmaps.py +++ b/examples/tutorial_api_python/09_keypoints_from_heatmaps.py @@ -7,60 +7,60 @@ from sys import platform import argparse import numpy as np -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000294.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000294.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Load image -imageToProcess = cv2.imread(args[0].image_path) - -def get_sample_heatmaps(): - # These parameters are globally set. You need to unset variables set here if you have a new OpenPose object. See * - params = dict() - params["model_folder"] = "../../../models/" - params["heatmaps_add_parts"] = True - params["heatmaps_add_bkg"] = True - params["heatmaps_add_PAFs"] = True - params["heatmaps_scale"] = 3 - params["upsampling_ratio"] = 1 - params["body"] = 1 - - # Starting OpenPose - opWrapper = op.WrapperPython() - opWrapper.configure(params) - opWrapper.start() - - # Process Image and get heatmap - datum = op.Datum() + # Load image imageToProcess = cv2.imread(args[0].image_path) - datum.cvInputData = imageToProcess - opWrapper.emplaceAndPop([datum]) - poseHeatMaps = datum.poseHeatMaps.copy() - opWrapper.stop() - return poseHeatMaps + def get_sample_heatmaps(): + # These parameters are globally set. You need to unset variables set here if you have a new OpenPose object. See * + params = dict() + params["model_folder"] = "../../../models/" + params["heatmaps_add_parts"] = True + params["heatmaps_add_bkg"] = True + params["heatmaps_add_PAFs"] = True + params["heatmaps_scale"] = 3 + params["upsampling_ratio"] = 1 + params["body"] = 1 + + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() + + # Process Image and get heatmap + datum = op.Datum() + imageToProcess = cv2.imread(args[0].image_path) + datum.cvInputData = imageToProcess + opWrapper.emplaceAndPop([datum]) + poseHeatMaps = datum.poseHeatMaps.copy() + opWrapper.stop() + + return poseHeatMaps -try: # Get Heatmap poseHeatMaps = get_sample_heatmaps() @@ -84,5 +84,5 @@ try: cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) cv2.waitKey(0) except Exception as e: - # print(e) - sys.exit(-1) \ No newline at end of file + print(e) + sys.exit(-1) diff --git a/examples/tutorial_api_python/openpose_python.py b/examples/tutorial_api_python/openpose_python.py index 55c4333d..8c6630c2 100644 --- a/examples/tutorial_api_python/openpose_python.py +++ b/examples/tutorial_api_python/openpose_python.py @@ -6,51 +6,55 @@ import os from sys import platform import argparse -# Import Openpose (Windows/Ubuntu/OSX) -dir_path = os.path.dirname(os.path.realpath(__file__)) try: - # Windows Import - if platform == "win32": - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append(dir_path + '/../../python/openpose/Release'); - os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' - import pyopenpose as op - else: - # Change these variables to point to the correct folder (Release/x64 etc.) - sys.path.append('../../python'); - # 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 pyopenpose as op -except ImportError as e: - print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') - raise e + # Import Openpose (Windows/Ubuntu/OSX) + dir_path = os.path.dirname(os.path.realpath(__file__)) + try: + # Windows Import + if platform == "win32": + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append(dir_path + '/../../python/openpose/Release'); + os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;' + import pyopenpose as op + else: + # Change these variables to point to the correct folder (Release/x64 etc.) + sys.path.append('../../python'); + # 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 pyopenpose as op + except ImportError as e: + print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?') + raise e -# Flags -parser = argparse.ArgumentParser() -parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") -args = parser.parse_known_args() + # Flags + parser = argparse.ArgumentParser() + parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).") + args = parser.parse_known_args() -# Custom Params (refer to include/openpose/flags.hpp for more parameters) -params = dict() -params["model_folder"] = "../../../models/" + # Custom Params (refer to include/openpose/flags.hpp for more parameters) + params = dict() + params["model_folder"] = "../../../models/" -# Add others in path? -for i in range(0, len(args[1])): - curr_item = args[1][i] - if i != len(args[1])-1: next_item = args[1][i+1] - else: next_item = "1" - if "--" in curr_item and "--" in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = "1" - elif "--" in curr_item and "--" not in next_item: - key = curr_item.replace('-','') - if key not in params: params[key] = next_item + # Add others in path? + for i in range(0, len(args[1])): + curr_item = args[1][i] + if i != len(args[1])-1: next_item = args[1][i+1] + else: next_item = "1" + if "--" in curr_item and "--" in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = "1" + elif "--" in curr_item and "--" not in next_item: + key = curr_item.replace('-','') + if key not in params: params[key] = next_item -# Construct it from system arguments -# op.init_argv(args[1]) -# oppython = op.OpenposePython() + # Construct it from system arguments + # op.init_argv(args[1]) + # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython(3) -opWrapper.configure(params) -opWrapper.execute() + # Starting OpenPose + opWrapper = op.WrapperPython(3) + opWrapper.configure(params) + opWrapper.execute() +except Exception as e: + print(e) + sys.exit(-1) diff --git a/include/openpose/core/matrix.hpp b/include/openpose/core/matrix.hpp index fadf9a44..ac718d89 100644 --- a/include/openpose/core/matrix.hpp +++ b/include/openpose/core/matrix.hpp @@ -126,13 +126,20 @@ namespace op /** * Equivalent to cv::Mat::data + * @return A raw pointer to the internal data of cv::Mat. */ - unsigned char* data(); /** * Equivalent to cv::Mat::data + * @return A raw pointer to the internal data of cv::Mat. */ const unsigned char* dataConst() const; + /** + * Similar to dataConst(), but it allows the data to be edited. + * This function is only implemented for Pybind11 usage. + * @return A raw pointer to the internal data of cv::Mat. + */ + unsigned char* dataPseudoConst() const; /** * Equivalent to cv::Mat::eye diff --git a/include/openpose/core/string.hpp b/include/openpose/core/string.hpp index 9bab6cfe..76407204 100644 --- a/include/openpose/core/string.hpp +++ b/include/openpose/core/string.hpp @@ -30,6 +30,8 @@ namespace op const std::string& getStdString() const; + bool empty() const; + private: // PIMPL idiom // http://www.cppsamples.com/common-tasks/pimpl.html diff --git a/include/openpose/wrapper/wrapperAuxiliary.hpp b/include/openpose/wrapper/wrapperAuxiliary.hpp index b6226036..c90723e2 100644 --- a/include/openpose/wrapper/wrapperAuxiliary.hpp +++ b/include/openpose/wrapper/wrapperAuxiliary.hpp @@ -747,7 +747,7 @@ namespace op } opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__); // Write people pose/foot/face/hand/etc. data on disk (COCO validation JSON format) - if (!wrapperStructOutput.writeCocoJson.getStdString().empty()) + if (!wrapperStructOutput.writeCocoJson.empty()) { opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__); // If humanFormat: bigger size (& maybe slower to process), but easier for user to read it @@ -773,8 +773,8 @@ namespace op } opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__); auto originalVideoFps = 0.; - if (!wrapperStructOutput.writeVideo.getStdString().empty() || !wrapperStructOutput.writeVideo3D.getStdString().empty() - || !wrapperStructOutput.writeBvh.getStdString().empty()) + if (!wrapperStructOutput.writeVideo.empty() || !wrapperStructOutput.writeVideo3D.empty() + || !wrapperStructOutput.writeBvh.empty()) { opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__); if (wrapperStructOutput.writeVideoFps <= 0 @@ -790,7 +790,7 @@ namespace op } opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__); // Write frames as *.avi video on hard disk - if (!wrapperStructOutput.writeVideo.getStdString().empty()) + if (!wrapperStructOutput.writeVideo.empty()) { opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__); // Sanity checks @@ -892,12 +892,12 @@ namespace op finalOutputSizeGui, wrapperStructGui.fullScreen, threadManager.getIsRunningSharedPtr(), spVideoSeek, poseExtractorNets, faceExtractorNets, handExtractorNets, renderers, wrapperStructPose.poseModel, wrapperStructGui.displayMode, - !wrapperStructOutput.writeVideo3D.getStdString().empty() + !wrapperStructOutput.writeVideo3D.empty() ); // WGui guiW = {std::make_shared>(gui)}; // Write 3D frames as *.avi video on hard disk - if (!wrapperStructOutput.writeVideo3D.getStdString().empty()) + if (!wrapperStructOutput.writeVideo3D.empty()) { const auto videoSaver = std::make_shared( wrapperStructOutput.writeVideo3D.getStdString(), getCvFourcc('M','J','P','G'), originalVideoFps, ""); @@ -916,7 +916,7 @@ namespace op // WGui guiW = {std::make_shared>(gui)}; // Write 3D frames as *.avi video on hard disk - if (!wrapperStructOutput.writeVideo3D.getStdString().empty()) + if (!wrapperStructOutput.writeVideo3D.empty()) error("3D video can only be recorded if 3D render is enabled.", __LINE__, __FUNCTION__, __FILE__); } diff --git a/python/openpose/openpose_python.cpp b/python/openpose/openpose_python.cpp index ca823f09..5700f029 100644 --- a/python/openpose/openpose_python.cpp +++ b/python/openpose/openpose_python.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -18,160 +17,21 @@ #define OP_EXPORT #endif -namespace op{ - -namespace py = pybind11; - -void parse_gflags(const std::vector& argv) +namespace op { - try - { - std::vector argv_vec; - for (auto& arg : argv) - argv_vec.emplace_back((char*)arg.c_str()); - char** cast = &argv_vec[0]; - int size = (int)argv_vec.size(); - gflags::ParseCommandLineFlags(&size, &cast, true); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - } -} -void init_int(py::dict d) -{ - try - { - std::vector argv; - argv.emplace_back("openpose.py"); - for (auto item : d){ - // Sanity check - std::size_t found = std::string(py::str(item.first)).find("="); - if (found != std::string::npos) - error("PyOpenPose does not support equal sign flags (e.g., " - + std::string(py::str(item.first)) + ").", __LINE__, __FUNCTION__, __FILE__); - // Add argument - argv.emplace_back("--" + std::string(py::str(item.first)) + "=" + std::string(py::str(item.second))); - } - parse_gflags(argv); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - } -} + namespace py = pybind11; -void init_argv(std::vector argv) -{ - try - { - argv.insert(argv.begin(), "openpose.py"); - parse_gflags(argv); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - } -} - -class WrapperPython{ -public: - std::unique_ptr opWrapper; - - WrapperPython(int mode = 0) - { - opLog("Starting OpenPose Python Wrapper...", Priority::High); - - // Construct opWrapper - opWrapper = std::unique_ptr(new Wrapper(static_cast(mode))); - } - - void configure(py::dict params = py::dict()) + void parse_gflags(const std::vector& argv) { try { - if (params.size()) - init_int(params); - - // logging_level - checkBool( - 0 <= FLAGS_logging_level && FLAGS_logging_level <= 255, "Wrong logging_level value.", - __LINE__, __FUNCTION__, __FILE__); - ConfigureLog::setPriorityThreshold((Priority)FLAGS_logging_level); - Profiler::setDefaultX(FLAGS_profile_speed); - - // Applying user defined configuration - GFlags to program variables - // outputSize - const auto outputSize = flagsToPoint(op::String(FLAGS_output_resolution), "-1x-1"); - // netInputSize - const auto netInputSize = flagsToPoint(op::String(FLAGS_net_resolution), "-1x368"); - // faceNetInputSize - const auto faceNetInputSize = flagsToPoint(op::String(FLAGS_face_net_resolution), "368x368 (multiples of 16)"); - // handNetInputSize - const auto handNetInputSize = flagsToPoint(op::String(FLAGS_hand_net_resolution), "368x368 (multiples of 16)"); - // poseMode - const auto poseMode = flagsToPoseMode(FLAGS_body); - // poseModel - const auto poseModel = flagsToPoseModel(op::String(FLAGS_model_pose)); - // JSON saving - if (!FLAGS_write_keypoint.empty()) - opLog("Flag `write_keypoint` is deprecated and will eventually be removed." - " Please, use `write_json` instead.", Priority::Max); - // keypointScaleMode - const auto keypointScaleMode = flagsToScaleMode(FLAGS_keypoint_scale); - // heatmaps to add - const auto heatMapTypes = flagsToHeatMaps(FLAGS_heatmaps_add_parts, FLAGS_heatmaps_add_bkg, - FLAGS_heatmaps_add_PAFs); - const auto heatMapScaleMode = flagsToHeatMapScaleMode(FLAGS_heatmaps_scale); - // >1 camera view? - const auto multipleView = (FLAGS_3d || FLAGS_3d_views > 1); - // Face and hand detectors - const auto faceDetector = flagsToDetector(FLAGS_face_detector); - const auto handDetector = flagsToDetector(FLAGS_hand_detector); - // Enabling Google Logging - const bool enableGoogleLogging = true; - - // Pose configuration (use WrapperStructPose{} for default and recommended configuration) - const op::WrapperStructPose wrapperStructPose{ - poseMode, netInputSize, outputSize, keypointScaleMode, FLAGS_num_gpu, FLAGS_num_gpu_start, - FLAGS_scale_number, (float)FLAGS_scale_gap, op::flagsToRenderMode(FLAGS_render_pose, multipleView), - poseModel, !FLAGS_disable_blending, (float)FLAGS_alpha_pose, (float)FLAGS_alpha_heatmap, - FLAGS_part_to_show, op::String(FLAGS_model_folder), heatMapTypes, heatMapScaleMode, FLAGS_part_candidates, - (float)FLAGS_render_threshold, FLAGS_number_people_max, FLAGS_maximize_positives, FLAGS_fps_max, - op::String(FLAGS_prototxt_path), op::String(FLAGS_caffemodel_path), - (float)FLAGS_upsampling_ratio, enableGoogleLogging}; - opWrapper->configure(wrapperStructPose); - // Face configuration (use WrapperStructFace{} to disable it) - const WrapperStructFace wrapperStructFace{ - FLAGS_face, faceDetector, faceNetInputSize, - flagsToRenderMode(FLAGS_face_render, multipleView, FLAGS_render_pose), - (float)FLAGS_face_alpha_pose, (float)FLAGS_face_alpha_heatmap, (float)FLAGS_face_render_threshold}; - opWrapper->configure(wrapperStructFace); - // Hand configuration (use WrapperStructHand{} to disable it) - const WrapperStructHand wrapperStructHand{ - FLAGS_hand, handDetector, handNetInputSize, FLAGS_hand_scale_number, (float)FLAGS_hand_scale_range, - flagsToRenderMode(FLAGS_hand_render, multipleView, FLAGS_render_pose), (float)FLAGS_hand_alpha_pose, - (float)FLAGS_hand_alpha_heatmap, (float)FLAGS_hand_render_threshold}; - opWrapper->configure(wrapperStructHand); - // Extra functionality configuration (use WrapperStructExtra{} to disable it) - const WrapperStructExtra wrapperStructExtra{ - FLAGS_3d, FLAGS_3d_min_views, FLAGS_identification, FLAGS_tracking, FLAGS_ik_threads}; - opWrapper->configure(wrapperStructExtra); - // Output (comment or use default argument to disable any output) - const WrapperStructOutput wrapperStructOutput{ - FLAGS_cli_verbose, op::String(FLAGS_write_keypoint), op::stringToDataFormat(FLAGS_write_keypoint_format), - op::String(FLAGS_write_json), op::String(FLAGS_write_coco_json), FLAGS_write_coco_json_variants, - FLAGS_write_coco_json_variant, op::String(FLAGS_write_images), op::String(FLAGS_write_images_format), - op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio, - op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d), - op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host), - op::String(FLAGS_udp_port)}; - opWrapper->configure(wrapperStructOutput); - // No GUI. Equivalent to: opWrapper.configure(WrapperStructGui{}); - // Set to single-thread (for sequential processing and/or debugging and/or reducing latency) - if (FLAGS_disable_multi_thread) - opWrapper->disableMultiThreading(); + std::vector argv_vec; + for (auto& arg : argv) + argv_vec.emplace_back((char*)arg.c_str()); + char** cast = &argv_vec[0]; + int size = (int)argv_vec.size(); + gflags::ParseCommandLineFlags(&size, &cast, true); } catch (const std::exception& e) { @@ -179,11 +39,22 @@ public: } } - void start() + void init_int(py::dict d) { try { - opWrapper->start(); + std::vector argv; + argv.emplace_back("openpose.py"); + for (auto item : d){ + // Sanity check + std::size_t found = std::string(py::str(item.first)).find("="); + if (found != std::string::npos) + error("PyOpenPose does not support equal sign flags (e.g., " + + std::string(py::str(item.first)) + ").", __LINE__, __FUNCTION__, __FILE__); + // Add argument + argv.emplace_back("--" + std::string(py::str(item.first)) + "=" + std::string(py::str(item.second))); + } + parse_gflags(argv); } catch (const std::exception& e) { @@ -191,11 +62,12 @@ public: } } - void stop() + void init_argv(std::vector argv) { try { - opWrapper->stop(); + argv.insert(argv.begin(), "openpose.py"); + parse_gflags(argv); } catch (const std::exception& e) { @@ -203,175 +75,302 @@ public: } } - void exec() + class WrapperPython{ + public: + std::unique_ptr opWrapper; + + WrapperPython(int mode = 0) + { + opLog("Starting OpenPose Python Wrapper...", Priority::High); + + // Construct opWrapper + opWrapper = std::unique_ptr(new Wrapper(static_cast(mode))); + } + + void configure(py::dict params = py::dict()) + { + try + { + if (params.size()) + init_int(params); + + // logging_level + checkBool( + 0 <= FLAGS_logging_level && FLAGS_logging_level <= 255, "Wrong logging_level value.", + __LINE__, __FUNCTION__, __FILE__); + ConfigureLog::setPriorityThreshold((Priority)FLAGS_logging_level); + Profiler::setDefaultX(FLAGS_profile_speed); + + // Applying user defined configuration - GFlags to program variables + // outputSize + const auto outputSize = flagsToPoint(op::String(FLAGS_output_resolution), "-1x-1"); + // netInputSize + const auto netInputSize = flagsToPoint(op::String(FLAGS_net_resolution), "-1x368"); + // faceNetInputSize + const auto faceNetInputSize = flagsToPoint(op::String(FLAGS_face_net_resolution), "368x368 (multiples of 16)"); + // handNetInputSize + const auto handNetInputSize = flagsToPoint(op::String(FLAGS_hand_net_resolution), "368x368 (multiples of 16)"); + // poseMode + const auto poseMode = flagsToPoseMode(FLAGS_body); + // poseModel + const auto poseModel = flagsToPoseModel(op::String(FLAGS_model_pose)); + // JSON saving + if (!FLAGS_write_keypoint.empty()) + opLog("Flag `write_keypoint` is deprecated and will eventually be removed." + " Please, use `write_json` instead.", Priority::Max); + // keypointScaleMode + const auto keypointScaleMode = flagsToScaleMode(FLAGS_keypoint_scale); + // heatmaps to add + const auto heatMapTypes = flagsToHeatMaps(FLAGS_heatmaps_add_parts, FLAGS_heatmaps_add_bkg, + FLAGS_heatmaps_add_PAFs); + const auto heatMapScaleMode = flagsToHeatMapScaleMode(FLAGS_heatmaps_scale); + // >1 camera view? + const auto multipleView = (FLAGS_3d || FLAGS_3d_views > 1); + // Face and hand detectors + const auto faceDetector = flagsToDetector(FLAGS_face_detector); + const auto handDetector = flagsToDetector(FLAGS_hand_detector); + // Enabling Google Logging + const bool enableGoogleLogging = true; + + // Pose configuration (use WrapperStructPose{} for default and recommended configuration) + const op::WrapperStructPose wrapperStructPose{ + poseMode, netInputSize, outputSize, keypointScaleMode, FLAGS_num_gpu, FLAGS_num_gpu_start, + FLAGS_scale_number, (float)FLAGS_scale_gap, op::flagsToRenderMode(FLAGS_render_pose, multipleView), + poseModel, !FLAGS_disable_blending, (float)FLAGS_alpha_pose, (float)FLAGS_alpha_heatmap, + FLAGS_part_to_show, op::String(FLAGS_model_folder), heatMapTypes, heatMapScaleMode, FLAGS_part_candidates, + (float)FLAGS_render_threshold, FLAGS_number_people_max, FLAGS_maximize_positives, FLAGS_fps_max, + op::String(FLAGS_prototxt_path), op::String(FLAGS_caffemodel_path), + (float)FLAGS_upsampling_ratio, enableGoogleLogging}; + opWrapper->configure(wrapperStructPose); + // Face configuration (use WrapperStructFace{} to disable it) + const WrapperStructFace wrapperStructFace{ + FLAGS_face, faceDetector, faceNetInputSize, + flagsToRenderMode(FLAGS_face_render, multipleView, FLAGS_render_pose), + (float)FLAGS_face_alpha_pose, (float)FLAGS_face_alpha_heatmap, (float)FLAGS_face_render_threshold}; + opWrapper->configure(wrapperStructFace); + // Hand configuration (use WrapperStructHand{} to disable it) + const WrapperStructHand wrapperStructHand{ + FLAGS_hand, handDetector, handNetInputSize, FLAGS_hand_scale_number, (float)FLAGS_hand_scale_range, + flagsToRenderMode(FLAGS_hand_render, multipleView, FLAGS_render_pose), (float)FLAGS_hand_alpha_pose, + (float)FLAGS_hand_alpha_heatmap, (float)FLAGS_hand_render_threshold}; + opWrapper->configure(wrapperStructHand); + // Extra functionality configuration (use WrapperStructExtra{} to disable it) + const WrapperStructExtra wrapperStructExtra{ + FLAGS_3d, FLAGS_3d_min_views, FLAGS_identification, FLAGS_tracking, FLAGS_ik_threads}; + opWrapper->configure(wrapperStructExtra); + // Output (comment or use default argument to disable any output) + const WrapperStructOutput wrapperStructOutput{ + FLAGS_cli_verbose, op::String(FLAGS_write_keypoint), op::stringToDataFormat(FLAGS_write_keypoint_format), + op::String(FLAGS_write_json), op::String(FLAGS_write_coco_json), FLAGS_write_coco_json_variants, + FLAGS_write_coco_json_variant, op::String(FLAGS_write_images), op::String(FLAGS_write_images_format), + op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio, + op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d), + op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host), + op::String(FLAGS_udp_port)}; + opWrapper->configure(wrapperStructOutput); + // No GUI. Equivalent to: opWrapper.configure(WrapperStructGui{}); + // Set to single-thread (for sequential processing and/or debugging and/or reducing latency) + if (FLAGS_disable_multi_thread) + opWrapper->disableMultiThreading(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + + void start() + { + try + { + opWrapper->start(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + + void stop() + { + try + { + opWrapper->stop(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + + void exec() + { + try + { + const auto cameraSize = flagsToPoint(op::String(FLAGS_camera_resolution), "-1x-1"); + ProducerType producerType; + op::String producerString; + std::tie(producerType, producerString) = flagsToProducer( + op::String(FLAGS_image_dir), op::String(FLAGS_video), op::String(FLAGS_ip_camera), FLAGS_camera, + FLAGS_flir_camera, FLAGS_flir_camera_index); + // Producer (use default to disable any input) + const WrapperStructInput wrapperStructInput{ + producerType, producerString, FLAGS_frame_first, FLAGS_frame_step, FLAGS_frame_last, + FLAGS_process_real_time, FLAGS_frame_flip, FLAGS_frame_rotate, FLAGS_frames_repeat, + cameraSize, op::String(FLAGS_camera_parameter_path), FLAGS_frame_undistort, FLAGS_3d_views}; + opWrapper->configure(wrapperStructInput); + // GUI (comment or use default argument to disable any visual output) + const WrapperStructGui wrapperStructGui{ + flagsToDisplayMode(FLAGS_display, FLAGS_3d), !FLAGS_no_gui_verbose, FLAGS_fullscreen}; + opWrapper->configure(wrapperStructGui); + opWrapper->exec(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + + void emplaceAndPop(std::vector>& l) + { + try + { + auto datumsPtr = std::make_shared>>(l); + opWrapper->emplaceAndPop(datumsPtr); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + + void waitAndEmplace(std::vector>& l) + { + try + { + auto datumsPtr = std::make_shared>>(l); + opWrapper->waitAndEmplace(datumsPtr); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + + bool waitAndPop(std::vector>& l) + { + try + { + auto datumsPtr = std::make_shared>>(l); + return opWrapper->waitAndPop(datumsPtr); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + return false; + } + } + }; + + std::vector getImagesFromDirectory(const std::string& directoryPath) { try { - const auto cameraSize = flagsToPoint(op::String(FLAGS_camera_resolution), "-1x-1"); - ProducerType producerType; - op::String producerString; - std::tie(producerType, producerString) = flagsToProducer( - op::String(FLAGS_image_dir), op::String(FLAGS_video), op::String(FLAGS_ip_camera), FLAGS_camera, - FLAGS_flir_camera, FLAGS_flir_camera_index); - // Producer (use default to disable any input) - const WrapperStructInput wrapperStructInput{ - producerType, producerString, FLAGS_frame_first, FLAGS_frame_step, FLAGS_frame_last, - FLAGS_process_real_time, FLAGS_frame_flip, FLAGS_frame_rotate, FLAGS_frames_repeat, - cameraSize, op::String(FLAGS_camera_parameter_path), FLAGS_frame_undistort, FLAGS_3d_views}; - opWrapper->configure(wrapperStructInput); - // GUI (comment or use default argument to disable any visual output) - const WrapperStructGui wrapperStructGui{ - flagsToDisplayMode(FLAGS_display, FLAGS_3d), !FLAGS_no_gui_verbose, FLAGS_fullscreen}; - opWrapper->configure(wrapperStructGui); - opWrapper->exec(); + return getFilesOnDirectory(directoryPath, Extensions::Images); } catch (const std::exception& e) { error(e.what(), __LINE__, __FUNCTION__, __FILE__); + return {}; } } - void emplaceAndPop(std::vector>& l) - { - try - { - auto datumsPtr = std::make_shared>>(l); - opWrapper->emplaceAndPop(datumsPtr); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - } + PYBIND11_MODULE(pyopenpose, m) { + + // Functions for Init Params + m.def("init_int", &init_int, "Init Function"); + m.def("init_argv", &init_argv, "Init Function"); + m.def("get_gpu_number", &getGpuNumber, "Get Total GPU"); + m.def("get_images_on_directory", &getImagesFromDirectory, "Get Images On Directory"); + + // OpenposePython + py::class_(m, "WrapperPython") + .def(py::init<>()) + .def(py::init()) + .def("configure", &WrapperPython::configure) + .def("start", &WrapperPython::start) + .def("stop", &WrapperPython::stop) + .def("execute", &WrapperPython::exec) + .def("emplaceAndPop", &WrapperPython::emplaceAndPop) + .def("waitAndEmplace", &WrapperPython::waitAndEmplace) + .def("waitAndPop", &WrapperPython::waitAndPop) + ; + + // Datum Object + py::class_>(m, "Datum") + .def(py::init<>()) + .def_readwrite("id", &Datum::id) + .def_readwrite("subId", &Datum::subId) + .def_readwrite("subIdMax", &Datum::subIdMax) + .def_readwrite("name", &Datum::name) + .def_readwrite("frameNumber", &Datum::frameNumber) + .def_readwrite("cvInputData", &Datum::cvInputData) + .def_readwrite("inputNetData", &Datum::inputNetData) + .def_readwrite("outputData", &Datum::outputData) + .def_readwrite("cvOutputData", &Datum::cvOutputData) + .def_readwrite("cvOutputData3D", &Datum::cvOutputData3D) + .def_readwrite("poseKeypoints", &Datum::poseKeypoints) + .def_readwrite("poseIds", &Datum::poseIds) + .def_readwrite("poseScores", &Datum::poseScores) + .def_readwrite("poseHeatMaps", &Datum::poseHeatMaps) + .def_readwrite("poseCandidates", &Datum::poseCandidates) + .def_readwrite("faceRectangles", &Datum::faceRectangles) + .def_readwrite("faceKeypoints", &Datum::faceKeypoints) + .def_readwrite("faceHeatMaps", &Datum::faceHeatMaps) + .def_readwrite("handRectangles", &Datum::handRectangles) + .def_readwrite("handKeypoints", &Datum::handKeypoints) + .def_readwrite("handHeatMaps", &Datum::handHeatMaps) + .def_readwrite("poseKeypoints3D", &Datum::poseKeypoints3D) + .def_readwrite("faceKeypoints3D", &Datum::faceKeypoints3D) + .def_readwrite("handKeypoints3D", &Datum::handKeypoints3D) + .def_readwrite("cameraMatrix", &Datum::cameraMatrix) + .def_readwrite("cameraExtrinsics", &Datum::cameraExtrinsics) + .def_readwrite("cameraIntrinsics", &Datum::cameraIntrinsics) + .def_readwrite("poseNetOutput", &Datum::poseNetOutput) + .def_readwrite("scaleInputToNetInputs", &Datum::scaleInputToNetInputs) + .def_readwrite("netInputSizes", &Datum::netInputSizes) + .def_readwrite("scaleInputToOutput", &Datum::scaleInputToOutput) + .def_readwrite("netOutputSize", &Datum::netOutputSize) + .def_readwrite("scaleNetToOutput", &Datum::scaleNetToOutput) + .def_readwrite("elementRendered", &Datum::elementRendered) + ; + + // Rectangle + py::class_>(m, "Rectangle") + .def("__repr__", [](Rectangle &a) { return a.toString(); }) + .def(py::init<>()) + .def(py::init()) + .def_readwrite("x", &Rectangle::x) + .def_readwrite("y", &Rectangle::y) + .def_readwrite("width", &Rectangle::width) + .def_readwrite("height", &Rectangle::height) + ; + + // Point + py::class_>(m, "Point") + .def("__repr__", [](Point &a) { return a.toString(); }) + .def(py::init<>()) + .def(py::init()) + .def_readwrite("x", &Point::x) + .def_readwrite("y", &Point::y) + ; + + #ifdef VERSION_INFO + m.attr("__version__") = VERSION_INFO; + #else + m.attr("__version__") = "dev"; + #endif } - - void waitAndEmplace(std::vector>& l) - { - try - { - auto datumsPtr = std::make_shared>>(l); - opWrapper->waitAndEmplace(datumsPtr); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - } - } - - bool waitAndPop(std::vector>& l) - { - try - { - auto datumsPtr = std::make_shared>>(l); - return opWrapper->waitAndPop(datumsPtr); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - return false; - } - } -}; - -std::vector getImagesFromDirectory(const std::string& directoryPath) -{ - try - { - return getFilesOnDirectory(directoryPath, Extensions::Images); - } - catch (const std::exception& e) - { - error(e.what(), __LINE__, __FUNCTION__, __FILE__); - return {}; - } -} - -PYBIND11_MODULE(pyopenpose, m) { - - // Functions for Init Params - m.def("init_int", &init_int, "Init Function"); - m.def("init_argv", &init_argv, "Init Function"); - m.def("get_gpu_number", &getGpuNumber, "Get Total GPU"); - m.def("get_images_on_directory", &getImagesFromDirectory, "Get Images On Directory"); - - // OpenposePython - py::class_(m, "WrapperPython") - .def(py::init<>()) - .def(py::init()) - .def("configure", &WrapperPython::configure) - .def("start", &WrapperPython::start) - .def("stop", &WrapperPython::stop) - .def("execute", &WrapperPython::exec) - .def("emplaceAndPop", &WrapperPython::emplaceAndPop) - .def("waitAndEmplace", &WrapperPython::waitAndEmplace) - .def("waitAndPop", &WrapperPython::waitAndPop) - ; - - // Datum Object - py::class_>(m, "Datum") - .def(py::init<>()) - .def_readwrite("id", &Datum::id) - .def_readwrite("subId", &Datum::subId) - .def_readwrite("subIdMax", &Datum::subIdMax) - .def_readwrite("name", &Datum::name) - .def_readwrite("frameNumber", &Datum::frameNumber) - .def_readwrite("cvInputData", &Datum::cvInputData) - .def_readwrite("inputNetData", &Datum::inputNetData) - .def_readwrite("outputData", &Datum::outputData) - .def_readwrite("cvOutputData", &Datum::cvOutputData) - .def_readwrite("cvOutputData3D", &Datum::cvOutputData3D) - .def_readwrite("poseKeypoints", &Datum::poseKeypoints) - .def_readwrite("poseIds", &Datum::poseIds) - .def_readwrite("poseScores", &Datum::poseScores) - .def_readwrite("poseHeatMaps", &Datum::poseHeatMaps) - .def_readwrite("poseCandidates", &Datum::poseCandidates) - .def_readwrite("faceRectangles", &Datum::faceRectangles) - .def_readwrite("faceKeypoints", &Datum::faceKeypoints) - .def_readwrite("faceHeatMaps", &Datum::faceHeatMaps) - .def_readwrite("handRectangles", &Datum::handRectangles) - .def_readwrite("handKeypoints", &Datum::handKeypoints) - .def_readwrite("handHeatMaps", &Datum::handHeatMaps) - .def_readwrite("poseKeypoints3D", &Datum::poseKeypoints3D) - .def_readwrite("faceKeypoints3D", &Datum::faceKeypoints3D) - .def_readwrite("handKeypoints3D", &Datum::handKeypoints3D) - .def_readwrite("cameraMatrix", &Datum::cameraMatrix) - .def_readwrite("cameraExtrinsics", &Datum::cameraExtrinsics) - .def_readwrite("cameraIntrinsics", &Datum::cameraIntrinsics) - .def_readwrite("poseNetOutput", &Datum::poseNetOutput) - .def_readwrite("scaleInputToNetInputs", &Datum::scaleInputToNetInputs) - .def_readwrite("netInputSizes", &Datum::netInputSizes) - .def_readwrite("scaleInputToOutput", &Datum::scaleInputToOutput) - .def_readwrite("netOutputSize", &Datum::netOutputSize) - .def_readwrite("scaleNetToOutput", &Datum::scaleNetToOutput) - .def_readwrite("elementRendered", &Datum::elementRendered) - ; - - // Rectangle - py::class_>(m, "Rectangle") - .def("__repr__", [](Rectangle &a) { return a.toString(); }) - .def(py::init<>()) - .def(py::init()) - .def_readwrite("x", &Rectangle::x) - .def_readwrite("y", &Rectangle::y) - .def_readwrite("width", &Rectangle::width) - .def_readwrite("height", &Rectangle::height) - ; - - // Point - py::class_>(m, "Point") - .def("__repr__", [](Point &a) { return a.toString(); }) - .def(py::init<>()) - .def(py::init()) - .def_readwrite("x", &Point::x) - .def_readwrite("y", &Point::y) - ; - - #ifdef VERSION_INFO - m.attr("__version__") = VERSION_INFO; - #else - m.attr("__version__") = "dev"; - #endif -} - } // Numpy - op::Array interop @@ -431,15 +430,15 @@ template <> struct type_caster> { }; }} // namespace pybind11::detail -// Numpy - cv::Mat interop +// Numpy - op::Matrix interop namespace pybind11 { namespace detail { -template <> struct type_caster { +template <> struct type_caster { public: - PYBIND11_TYPE_CASTER(cv::Mat, _("numpy.ndarray")); + PYBIND11_TYPE_CASTER(op::Matrix, _("numpy.ndarray")); - // Cast numpy to cv::Mat + // Cast numpy to op::Matrix bool load(handle src, bool) { /* Try a default converting into a Python */ @@ -483,18 +482,19 @@ template <> struct type_caster { std::vector shape = {(int)info.shape[0], (int)info.shape[1]}; - value = cv::Mat(cv::Size(shape[1], shape[0]), dtype, info.ptr, cv::Mat::AUTO_STEP); + value = op::Matrix(shape[0], shape[1], dtype, info.ptr); + // value = cv::Mat(cv::Size(shape[1], shape[0]), dtype, info.ptr, cv::Mat::AUTO_STEP); return true; } - // Cast cv::Mat to numpy - static handle cast(const cv::Mat &m, return_value_policy, handle defval) + // Cast op::Matrix to numpy + static handle cast(const op::Matrix &matrix, return_value_policy, handle defval) { UNUSED(defval); std::string format = format_descriptor::format(); size_t elemsize = sizeof(unsigned char); int dim; - switch(m.type()) { + switch(matrix.type()) { case CV_8U: format = format_descriptor::format(); elemsize = sizeof(unsigned char); @@ -522,19 +522,19 @@ template <> struct type_caster { std::vector bufferdim; std::vector strides; if (dim == 2) { - bufferdim = {(size_t) m.rows, (size_t) m.cols}; - strides = {elemsize * (size_t) m.cols, elemsize}; + bufferdim = {(size_t) matrix.rows(), (size_t) matrix.cols()}; + strides = {elemsize * (size_t) matrix.cols(), elemsize}; } else if (dim == 3) { - bufferdim = {(size_t) m.rows, (size_t) m.cols, (size_t) 3}; - strides = {(size_t) elemsize * m.cols * 3, (size_t) elemsize * 3, (size_t) elemsize}; + bufferdim = {(size_t) matrix.rows(), (size_t) matrix.cols(), (size_t) 3}; + strides = {(size_t) elemsize * matrix.cols() * 3, (size_t) elemsize * 3, (size_t) elemsize}; } return array(buffer_info( - m.data, /* Pointer to buffer */ - elemsize, /* Size of one scalar */ - format, /* Python struct-style format descriptor */ - dim, /* Number of dimensions */ - bufferdim, /* Buffer dimensions */ - strides /* Strides (in bytes) for each index */ + matrix.dataPseudoConst(), /* Pointer to buffer */ + elemsize, /* Size of one scalar */ + format, /* Python struct-style format descriptor */ + dim, /* Number of dimensions */ + bufferdim, /* Buffer dimensions */ + strides /* Strides (in bytes) for each index */ )).release(); } diff --git a/src/openpose/core/matrix.cpp b/src/openpose/core/matrix.cpp index 8f04412a..cf6cb7d5 100644 --- a/src/openpose/core/matrix.cpp +++ b/src/openpose/core/matrix.cpp @@ -105,6 +105,19 @@ namespace op } } + unsigned char* Matrix::dataPseudoConst() const + { + try + { + return spImpl->mCvMat.data; + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + return nullptr; + } + } + Matrix Matrix::eye(const int rows, const int cols, const int type) { try diff --git a/src/openpose/core/string.cpp b/src/openpose/core/string.cpp index 141525e1..6c47a6c2 100644 --- a/src/openpose/core/string.cpp +++ b/src/openpose/core/string.cpp @@ -44,4 +44,17 @@ namespace op return spImpl->mString; } } + + bool String::empty() const + { + try + { + return spImpl->mString.empty(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + return true; + } + } } diff --git a/src/openpose/wrapper/wrapperAuxiliary.cpp b/src/openpose/wrapper/wrapperAuxiliary.cpp index 7857ff00..ea7a900b 100644 --- a/src/openpose/wrapper/wrapperAuxiliary.cpp +++ b/src/openpose/wrapper/wrapperAuxiliary.cpp @@ -24,21 +24,21 @@ namespace op if (wrapperStructPose.scaleGap <= 0.f && wrapperStructPose.scalesNumber > 1) error("The scale gap must be greater than 0 (it has no effect if the number of scales is 1).", __LINE__, __FUNCTION__, __FILE__); - if (!renderOutput && (!wrapperStructOutput.writeImages.getStdString().empty() - || !wrapperStructOutput.writeVideo.getStdString().empty())) + if (!renderOutput && (!wrapperStructOutput.writeImages.empty() + || !wrapperStructOutput.writeVideo.empty())) { const auto message = "In order to save the rendered frames (`--write_images` or `--write_video`), you" " cannot disable `--render_pose`."; opLog(message, Priority::High); } - if (!wrapperStructOutput.writeHeatMaps.getStdString().empty() && wrapperStructPose.heatMapTypes.empty()) + if (!wrapperStructOutput.writeHeatMaps.empty() && wrapperStructPose.heatMapTypes.empty()) { const auto message = "In order to save the heatmaps (`--write_heatmaps`), you need to pick which heat" " maps you want to save: `--heatmaps_add_X` flags or fill the" " wrapperStructPose.heatMapTypes."; error(message, __LINE__, __FUNCTION__, __FILE__); } - if (!wrapperStructOutput.writeHeatMaps.getStdString().empty() + if (!wrapperStructOutput.writeHeatMaps.empty() && (wrapperStructPose.heatMapScaleMode != ScaleMode::UnsignedChar && wrapperStructOutput.writeHeatMapsFormat.getStdString() != "float")) { @@ -56,12 +56,12 @@ namespace op " own output worker class before calling this function." }; const auto savingSomething = ( - !wrapperStructOutput.writeImages.getStdString().empty() || !wrapperStructOutput.writeVideo.getStdString().empty() - || !wrapperStructOutput.writeKeypoint.getStdString().empty() || !wrapperStructOutput.writeJson.getStdString().empty() - || !wrapperStructOutput.writeCocoJson.getStdString().empty() || !wrapperStructOutput.writeHeatMaps.getStdString().empty() + !wrapperStructOutput.writeImages.empty() || !wrapperStructOutput.writeVideo.empty() + || !wrapperStructOutput.writeKeypoint.empty() || !wrapperStructOutput.writeJson.empty() + || !wrapperStructOutput.writeCocoJson.empty() || !wrapperStructOutput.writeHeatMaps.empty() ); const auto savingCvOutput = ( - !wrapperStructOutput.writeImages.getStdString().empty() || !wrapperStructOutput.writeVideo.getStdString().empty() + !wrapperStructOutput.writeImages.empty() || !wrapperStructOutput.writeVideo.empty() ); const bool guiEnabled = (wrapperStructGui.displayMode != DisplayMode::NoDisplay); if (!guiEnabled && !savingCvOutput && renderOutput) @@ -102,7 +102,7 @@ namespace op opLog(message, Priority::High); } } - if (!wrapperStructOutput.writeVideo.getStdString().empty() && producerSharedPtr == nullptr) + if (!wrapperStructOutput.writeVideo.empty() && producerSharedPtr == nullptr) error("Writting video (`--write_video`) is only available if the OpenPose producer is used (i.e." " producerSharedPtr cannot be a nullptr). Otherwise, OpenPose would not know the frame rate" " of that output video nor whether all the images maintain the same resolution. You might" diff --git a/src/openpose/wrapper/wrapperStructOutput.cpp b/src/openpose/wrapper/wrapperStructOutput.cpp index 6a54479f..0a05d578 100644 --- a/src/openpose/wrapper/wrapperStructOutput.cpp +++ b/src/openpose/wrapper/wrapperStructOutput.cpp @@ -32,7 +32,7 @@ namespace op { try { - if (!writeBvh.getStdString().empty()) + if (!writeBvh.empty()) error("BVH writing is experimental and not available yet (flag `--write_bvh`). Please, disable this" " flag and do not open a GitHub issue asking for it.", __LINE__, __FUNCTION__, __FILE__); }