From 1e4a7853572e491c5ec0afac4288346c9004065f Mon Sep 17 00:00:00 2001 From: Raaj Date: Mon, 15 Apr 2019 18:44:40 -0400 Subject: [PATCH] Python fix to #1161, #1170, and keypoint from heatmap example (#1192) --- .../tutorial_api_python/01_body_from_image.py | 30 +- .../02_whole_body_from_image.py | 36 +- .../04_keypoints_from_images.py | 44 +- .../05_keypoints_from_images_multi_gpu.py | 79 +-- .../tutorial_api_python/06_face_from_image.py | 44 +- .../tutorial_api_python/07_hand_from_image.py | 68 +-- .../08_heatmaps_from_image.py | 60 +-- .../09_keypoints_from_heatmaps.py | 42 +- python/openpose/openpose_python.cpp | 467 +++++++++++------- 9 files changed, 501 insertions(+), 369 deletions(-) diff --git a/examples/tutorial_api_python/01_body_from_image.py b/examples/tutorial_api_python/01_body_from_image.py index d284ffc4..70028ffe 100644 --- a/examples/tutorial_api_python/01_body_from_image.py +++ b/examples/tutorial_api_python/01_body_from_image.py @@ -50,18 +50,22 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Process Image -datum = op.Datum() -imageToProcess = cv2.imread(args[0].image_path) -datum.cvInputData = imageToProcess -opWrapper.emplaceAndPop([datum]) + # Process Image + datum = op.Datum() + imageToProcess = cv2.imread(args[0].image_path) + datum.cvInputData = imageToProcess + opWrapper.emplaceAndPop([datum]) -# Display Image -print("Body keypoints: \n" + str(datum.poseKeypoints)) -cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) -cv2.waitKey(0) + # Display Image + print("Body keypoints: \n" + str(datum.poseKeypoints)) + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + cv2.waitKey(0) +except Exception as 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 f71a193c..31acb226 100644 --- a/examples/tutorial_api_python/02_whole_body_from_image.py +++ b/examples/tutorial_api_python/02_whole_body_from_image.py @@ -52,21 +52,25 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Process Image -datum = op.Datum() -imageToProcess = cv2.imread(args[0].image_path) -datum.cvInputData = imageToProcess -opWrapper.emplaceAndPop([datum]) + # Process Image + datum = op.Datum() + imageToProcess = cv2.imread(args[0].image_path) + datum.cvInputData = imageToProcess + opWrapper.emplaceAndPop([datum]) -# Display Image -print("Body keypoints: \n" + str(datum.poseKeypoints)) -print("Face keypoints: \n" + str(datum.faceKeypoints)) -print("Left hand keypoints: \n" + str(datum.handKeypoints[0])) -print("Right hand keypoints: \n" + str(datum.handKeypoints[1])) -cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) -cv2.waitKey(0) + # Display Image + print("Body keypoints: \n" + str(datum.poseKeypoints)) + print("Face keypoints: \n" + str(datum.faceKeypoints)) + print("Left hand keypoints: \n" + str(datum.handKeypoints[0])) + print("Right hand keypoints: \n" + str(datum.handKeypoints[1])) + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + cv2.waitKey(0) +except Exception as 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 b2c4573b..44bbcf96 100644 --- a/examples/tutorial_api_python/04_keypoints_from_images.py +++ b/examples/tutorial_api_python/04_keypoints_from_images.py @@ -52,28 +52,32 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Read frames on directory -imagePaths = op.get_images_on_directory(args[0].image_dir); -start = time.time() + # Read frames on directory + imagePaths = op.get_images_on_directory(args[0].image_dir); + start = time.time() -# Process and display images -for imagePath in imagePaths: - datum = op.Datum() - imageToProcess = cv2.imread(imagePath) - datum.cvInputData = imageToProcess - opWrapper.emplaceAndPop([datum]) + # Process and display images + for imagePath in imagePaths: + datum = op.Datum() + imageToProcess = cv2.imread(imagePath) + datum.cvInputData = imageToProcess + opWrapper.emplaceAndPop([datum]) - print("Body keypoints: \n" + str(datum.poseKeypoints)) + print("Body keypoints: \n" + str(datum.poseKeypoints)) - if not args[0].no_display: - cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) - key = cv2.waitKey(15) - if key == 27: break + if not args[0].no_display: + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + key = cv2.waitKey(15) + if key == 27: break -end = time.time() -print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") + end = time.time() + print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") +except Exception as 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 43defed0..fded0b71 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 @@ -30,11 +30,14 @@ except ImportError as e: 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"]) # Add others in path? for i in range(0, len(args[1])): @@ -52,52 +55,56 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Read frames on directory -imagePaths = op.get_images_on_directory(args[0].image_dir); + # Read frames on directory + imagePaths = op.get_images_on_directory(args[0].image_dir); -# Read number of GPUs in your system -numberGPUs = op.get_gpu_number() -start = time.time() + # Read number of GPUs in your system + start = time.time() -# Process and display images -for imageBaseId in range(0, len(imagePaths), numberGPUs): + # Process and display images + for imageBaseId in range(0, len(imagePaths), numberGPUs): - # Create datums - datums = [] + # Create datums + datums = [] + images = [] - # Read and push images into OpenPose wrapper - for gpuId in range(0, numberGPUs): + # Read and push images into OpenPose wrapper + for gpuId in range(0, numberGPUs): - imageId = imageBaseId+gpuId - if imageId < len(imagePaths): + imageId = imageBaseId+gpuId + if imageId < len(imagePaths): - imagePath = imagePaths[imageBaseId+gpuId] - datum = op.Datum() - imageToProcess = cv2.imread(imagePath) - datum.cvInputData = imageToProcess - datums.append(datum) - opWrapper.waitAndEmplace([datums[-1]]) + imagePath = imagePaths[imageBaseId+gpuId] + datum = op.Datum() + images.append(cv2.imread(imagePath)) + datum.cvInputData = images[-1] + datums.append(datum) + opWrapper.waitAndEmplace([datums[-1]]) - # Retrieve processed results from OpenPose wrapper - for gpuId in range(0, numberGPUs): + # Retrieve processed results from OpenPose wrapper + for gpuId in range(0, numberGPUs): - imageId = imageBaseId+gpuId - if imageId < len(imagePaths): + imageId = imageBaseId+gpuId + if imageId < len(imagePaths): - datum = datums[gpuId] - opWrapper.waitAndPop([datum]) + datum = datums[gpuId] + opWrapper.waitAndPop([datum]) - print("Body keypoints: \n" + str(datum.poseKeypoints)) + print("Body keypoints: \n" + str(datum.poseKeypoints)) - if not args[0].no_display: - cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) - key = cv2.waitKey(15) - if key == 27: break + if not args[0].no_display: + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + key = cv2.waitKey(15) + if key == 27: break -end = time.time() -print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") + end = time.time() + print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds") +except Exception as 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 e5709109..11f99834 100644 --- a/examples/tutorial_api_python/06_face_from_image.py +++ b/examples/tutorial_api_python/06_face_from_image.py @@ -54,26 +54,30 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Read image and face rectangle locations -imageToProcess = cv2.imread(args[0].image_path) -faceRectangles = [ - op.Rectangle(330.119385, 277.532715, 48.717274, 48.717274), - op.Rectangle(24.036991, 267.918793, 65.175171, 65.175171), - op.Rectangle(151.803436, 32.477852, 108.295761, 108.295761), -] + # Read image and face rectangle locations + imageToProcess = cv2.imread(args[0].image_path) + faceRectangles = [ + op.Rectangle(330.119385, 277.532715, 48.717274, 48.717274), + op.Rectangle(24.036991, 267.918793, 65.175171, 65.175171), + op.Rectangle(151.803436, 32.477852, 108.295761, 108.295761), + ] -# Create new datum -datum = op.Datum() -datum.cvInputData = imageToProcess -datum.faceRectangles = faceRectangles + # Create new datum + datum = op.Datum() + datum.cvInputData = imageToProcess + datum.faceRectangles = faceRectangles -# Process and display image -opWrapper.emplaceAndPop([datum]) -print("Face keypoints: \n" + str(datum.faceKeypoints)) -cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) -cv2.waitKey(0) + # Process and display image + opWrapper.emplaceAndPop([datum]) + print("Face keypoints: \n" + str(datum.faceKeypoints)) + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + cv2.waitKey(0) +except Exception as 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 9eb7d6fa..b50e6abd 100644 --- a/examples/tutorial_api_python/07_hand_from_image.py +++ b/examples/tutorial_api_python/07_hand_from_image.py @@ -54,39 +54,43 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Read image and face rectangle locations -imageToProcess = cv2.imread(args[0].image_path) -handRectangles = [ - # Left/Right hands person 0 - [ - op.Rectangle(320.035889, 377.675049, 69.300949, 69.300949), - op.Rectangle(0., 0., 0., 0.), - ], - # Left/Right hands person 1 - [ - op.Rectangle(80.155792, 407.673492, 80.812706, 80.812706), - op.Rectangle(46.449715, 404.559753, 98.898178, 98.898178), - ], - # Left/Right hands person 2 - [ - op.Rectangle(185.692673, 303.112244, 157.587555, 157.587555), - op.Rectangle(88.984360, 268.866547, 117.818230, 117.818230), + # Read image and face rectangle locations + imageToProcess = cv2.imread(args[0].image_path) + handRectangles = [ + # Left/Right hands person 0 + [ + op.Rectangle(320.035889, 377.675049, 69.300949, 69.300949), + op.Rectangle(0., 0., 0., 0.), + ], + # Left/Right hands person 1 + [ + op.Rectangle(80.155792, 407.673492, 80.812706, 80.812706), + op.Rectangle(46.449715, 404.559753, 98.898178, 98.898178), + ], + # Left/Right hands person 2 + [ + op.Rectangle(185.692673, 303.112244, 157.587555, 157.587555), + op.Rectangle(88.984360, 268.866547, 117.818230, 117.818230), + ] ] -] -# Create new datum -datum = op.Datum() -datum.cvInputData = imageToProcess -datum.handRectangles = handRectangles + # Create new datum + datum = op.Datum() + datum.cvInputData = imageToProcess + datum.handRectangles = handRectangles -# Process and display image -opWrapper.emplaceAndPop([datum]) -print("Left hand keypoints: \n" + str(datum.handKeypoints[0])) -print("Right hand keypoints: \n" + str(datum.handKeypoints[1])) -cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) -cv2.waitKey(0) + # Process and display image + opWrapper.emplaceAndPop([datum]) + print("Left hand keypoints: \n" + str(datum.handKeypoints[0])) + print("Right hand keypoints: \n" + str(datum.handKeypoints[1])) + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + cv2.waitKey(0) +except Exception as 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 6d36052d..9b5e6267 100644 --- a/examples/tutorial_api_python/08_heatmaps_from_image.py +++ b/examples/tutorial_api_python/08_heatmaps_from_image.py @@ -54,34 +54,38 @@ for i in range(0, len(args[1])): # op.init_argv(args[1]) # oppython = op.OpenposePython() -# Starting OpenPose -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() +try: + # Starting OpenPose + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Process Image -datum = op.Datum() -imageToProcess = cv2.imread(args[0].image_path) -datum.cvInputData = imageToProcess -opWrapper.emplaceAndPop([datum]) + # Process Image + datum = op.Datum() + imageToProcess = cv2.imread(args[0].image_path) + datum.cvInputData = imageToProcess + opWrapper.emplaceAndPop([datum]) -# Process outputs -outputImageF = (datum.inputNetData[0].copy())[0,:,:,:] + 0.5 -outputImageF = cv2.merge([outputImageF[0,:,:], outputImageF[1,:,:], outputImageF[2,:,:]]) -outputImageF = (outputImageF*255.).astype(dtype='uint8') -heatmaps = datum.poseHeatMaps.copy() -heatmaps = (heatmaps).astype(dtype='uint8') + # Process outputs + outputImageF = (datum.inputNetData[0].copy())[0,:,:,:] + 0.5 + outputImageF = cv2.merge([outputImageF[0,:,:], outputImageF[1,:,:], outputImageF[2,:,:]]) + outputImageF = (outputImageF*255.).astype(dtype='uint8') + heatmaps = datum.poseHeatMaps.copy() + heatmaps = (heatmaps).astype(dtype='uint8') -# Display Image -counter = 0 -while 1: - num_maps = heatmaps.shape[0] - heatmap = heatmaps[counter, :, :].copy() - heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) - combined = cv2.addWeighted(outputImageF, 0.5, heatmap, 0.5, 0) - cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", combined) - key = cv2.waitKey(-1) - if key == 27: - break - counter += 1 - counter = counter % num_maps + # Display Image + counter = 0 + while 1: + num_maps = heatmaps.shape[0] + heatmap = heatmaps[counter, :, :].copy() + heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) + combined = cv2.addWeighted(outputImageF, 0.5, heatmap, 0.5, 0) + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", combined) + key = cv2.waitKey(-1) + if key == 27: + break + counter += 1 + counter = counter % num_maps +except Exception as e: + # print(e) + sys.exit(-1) \ No newline at end of file diff --git a/examples/tutorial_api_python/09_keypoints_from_heatmaps.py b/examples/tutorial_api_python/09_keypoints_from_heatmaps.py index 8a73643a..c116a68b 100644 --- a/examples/tutorial_api_python/09_keypoints_from_heatmaps.py +++ b/examples/tutorial_api_python/09_keypoints_from_heatmaps.py @@ -35,6 +35,7 @@ args = parser.parse_known_args() 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 @@ -59,24 +60,29 @@ def get_sample_heatmaps(): return poseHeatMaps -# Get Heatmap -poseHeatMaps = get_sample_heatmaps() +try: + # Get Heatmap + poseHeatMaps = get_sample_heatmaps() -# Starting OpenPose -params = dict() -params["model_folder"] = "../../../models/" -params["body"] = 2 # Disable OP Network -opWrapper = op.WrapperPython() -opWrapper.configure(params) -opWrapper.start() + # Starting OpenPose + params = dict() + params["model_folder"] = "../../../models/" + params["body"] = 2 # Disable OP Network + params["upsampling_ratio"] = 0 # * Unset this variable + opWrapper = op.WrapperPython() + opWrapper.configure(params) + opWrapper.start() -# Pass Heatmap and Run OP -datum = op.Datum() -datum.cvInputData = imageToProcess -datum.poseNetOutput = poseHeatMaps -opWrapper.emplaceAndPop([datum]) + # Pass Heatmap and Run OP + datum = op.Datum() + datum.cvInputData = imageToProcess + datum.poseNetOutput = poseHeatMaps + opWrapper.emplaceAndPop([datum]) -# Display Image -print("Body keypoints: \n" + str(datum.poseKeypoints)) -cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) -cv2.waitKey(0) + # Display Image + print("Body keypoints: \n" + str(datum.poseKeypoints)) + cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData) + cv2.waitKey(0) +except Exception as e: + # print(e) + sys.exit(-1) \ No newline at end of file diff --git a/python/openpose/openpose_python.cpp b/python/openpose/openpose_python.cpp index 99761964..655ca89b 100644 --- a/python/openpose/openpose_python.cpp +++ b/python/openpose/openpose_python.cpp @@ -24,171 +24,258 @@ namespace py = pybind11; void parse_gflags(const std::vector& argv) { - std::vector argv_vec; - for(auto& arg : argv) argv_vec.emplace_back((char*)arg.c_str()); - char** cast = &argv_vec[0]; - int size = argv_vec.size(); - gflags::ParseCommandLineFlags(&size, &cast, true); + try + { + std::vector argv_vec; + for(auto& arg : argv) argv_vec.emplace_back((char*)arg.c_str()); + char** cast = &argv_vec[0]; + int size = 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) { - std::vector argv; - argv.emplace_back("openpose.py"); - for (auto item : d){ - argv.emplace_back("--" + std::string(py::str(item.first))); - argv.emplace_back(py::str(item.second)); + 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__); } - parse_gflags(argv); } void init_argv(std::vector argv) { - argv.insert(argv.begin(), "openpose.py"); - parse_gflags(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; + std::unique_ptr opWrapper; WrapperPython(int mode = 0) { - op::log("Starting OpenPose Python Wrapper...", op::Priority::High); + log("Starting OpenPose Python Wrapper...", Priority::High); // Construct opWrapper - opWrapper = std::unique_ptr(new op::Wrapper(static_cast(mode))); + opWrapper = std::unique_ptr(new Wrapper(static_cast(mode))); } void configure(py::dict params = py::dict()) { - if(params.size()) init_int(params); + try + { + if(params.size()) init_int(params); - // logging_level - op::check(0 <= FLAGS_logging_level && FLAGS_logging_level <= 255, "Wrong logging_level value.", - __LINE__, __FUNCTION__, __FILE__); - op::ConfigureLog::setPriorityThreshold((op::Priority)FLAGS_logging_level); - op::Profiler::setDefaultX(FLAGS_profile_speed); + // logging_level + check(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 = op::flagsToPoint(FLAGS_output_resolution, "-1x-1"); - // netInputSize - const auto netInputSize = op::flagsToPoint(FLAGS_net_resolution, "-1x368"); - // faceNetInputSize - const auto faceNetInputSize = op::flagsToPoint(FLAGS_face_net_resolution, "368x368 (multiples of 16)"); - // handNetInputSize - const auto handNetInputSize = op::flagsToPoint(FLAGS_hand_net_resolution, "368x368 (multiples of 16)"); - // poseMode - const auto poseMode = op::flagsToPoseMode(FLAGS_body); - // poseModel - const auto poseModel = op::flagsToPoseModel(FLAGS_model_pose); - // JSON saving - if (!FLAGS_write_keypoint.empty()) - op::log("Flag `write_keypoint` is deprecated and will eventually be removed." - " Please, use `write_json` instead.", op::Priority::Max); - // keypointScaleMode - const auto keypointScaleMode = op::flagsToScaleMode(FLAGS_keypoint_scale); - // heatmaps to add - const auto heatMapTypes = op::flagsToHeatMaps(FLAGS_heatmaps_add_parts, FLAGS_heatmaps_add_bkg, - FLAGS_heatmaps_add_PAFs); - const auto heatMapScaleMode = op::flagsToHeatMapScaleMode(FLAGS_heatmaps_scale); - // >1 camera view? - const auto multipleView = (FLAGS_3d || FLAGS_3d_views > 1); - // Face and hand detectors - const auto faceDetector = op::flagsToDetector(FLAGS_face_detector); - const auto handDetector = op::flagsToDetector(FLAGS_hand_detector); - // Enabling Google Logging - const bool enableGoogleLogging = true; + // Applying user defined configuration - GFlags to program variables + // outputSize + const auto outputSize = flagsToPoint(FLAGS_output_resolution, "-1x-1"); + // netInputSize + const auto netInputSize = flagsToPoint(FLAGS_net_resolution, "-1x368"); + // faceNetInputSize + const auto faceNetInputSize = flagsToPoint(FLAGS_face_net_resolution, "368x368 (multiples of 16)"); + // handNetInputSize + const auto handNetInputSize = flagsToPoint(FLAGS_hand_net_resolution, "368x368 (multiples of 16)"); + // poseMode + const auto poseMode = flagsToPoseMode(FLAGS_body); + // poseModel + const auto poseModel = flagsToPoseModel(FLAGS_model_pose); + // JSON saving + if (!FLAGS_write_keypoint.empty()) + log("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, FLAGS_model_folder, heatMapTypes, heatMapScaleMode, FLAGS_part_candidates, - (float)FLAGS_render_threshold, FLAGS_number_people_max, FLAGS_maximize_positives, FLAGS_fps_max, - FLAGS_prototxt_path, FLAGS_caffemodel_path, (float)FLAGS_upsampling_ratio, enableGoogleLogging}; - opWrapper->configure(wrapperStructPose); - // Face configuration (use op::WrapperStructFace{} to disable it) - const op::WrapperStructFace wrapperStructFace{ - FLAGS_face, faceDetector, faceNetInputSize, - op::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 op::WrapperStructHand{} to disable it) - const op::WrapperStructHand wrapperStructHand{ - FLAGS_hand, handDetector, handNetInputSize, FLAGS_hand_scale_number, (float)FLAGS_hand_scale_range, - op::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 op::WrapperStructExtra{} to disable it) - const op::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 op::WrapperStructOutput wrapperStructOutput{ - FLAGS_cli_verbose, FLAGS_write_keypoint, op::stringToDataFormat(FLAGS_write_keypoint_format), - FLAGS_write_json, FLAGS_write_coco_json, FLAGS_write_coco_json_variants, FLAGS_write_coco_json_variant, - FLAGS_write_images, FLAGS_write_images_format, FLAGS_write_video, FLAGS_write_video_fps, - FLAGS_write_video_with_audio, FLAGS_write_heatmaps, FLAGS_write_heatmaps_format, FLAGS_write_video_3d, - FLAGS_write_video_adam, FLAGS_write_bvh, FLAGS_udp_host, FLAGS_udp_port}; - opWrapper->configure(wrapperStructOutput); - // No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{}); - // Set to single-thread (for sequential processing and/or debugging and/or reducing latency) - if (FLAGS_disable_multi_thread) - opWrapper->disableMultiThreading(); + // Pose configuration (use WrapperStructPose{} for default and recommended configuration) + const WrapperStructPose wrapperStructPose{ + poseMode, netInputSize, outputSize, keypointScaleMode, FLAGS_num_gpu, FLAGS_num_gpu_start, + FLAGS_scale_number, (float)FLAGS_scale_gap, flagsToRenderMode(FLAGS_render_pose, multipleView), + poseModel, !FLAGS_disable_blending, (float)FLAGS_alpha_pose, (float)FLAGS_alpha_heatmap, + FLAGS_part_to_show, FLAGS_model_folder, heatMapTypes, heatMapScaleMode, FLAGS_part_candidates, + (float)FLAGS_render_threshold, FLAGS_number_people_max, FLAGS_maximize_positives, FLAGS_fps_max, + FLAGS_prototxt_path, 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, FLAGS_write_keypoint, stringToDataFormat(FLAGS_write_keypoint_format), + FLAGS_write_json, FLAGS_write_coco_json, FLAGS_write_coco_json_variants, FLAGS_write_coco_json_variant, + FLAGS_write_images, FLAGS_write_images_format, FLAGS_write_video, FLAGS_write_video_fps, + FLAGS_write_video_with_audio, FLAGS_write_heatmaps, FLAGS_write_heatmaps_format, FLAGS_write_video_3d, + FLAGS_write_video_adam, FLAGS_write_bvh, FLAGS_udp_host, 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(){ - opWrapper->start(); - } - - void stop(){ - opWrapper->stop(); - } - - void exec(){ - const auto cameraSize = op::flagsToPoint(FLAGS_camera_resolution, "-1x-1"); - op::ProducerType producerType; - std::string producerString; - std::tie(producerType, producerString) = op::flagsToProducer( - FLAGS_image_dir, FLAGS_video, FLAGS_ip_camera, FLAGS_camera, FLAGS_flir_camera, FLAGS_flir_camera_index); - // Producer (use default to disable any input) - const op::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, 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 op::WrapperStructGui wrapperStructGui{ - op::flagsToDisplayMode(FLAGS_display, FLAGS_3d), !FLAGS_no_gui_verbose, FLAGS_fullscreen}; - opWrapper->configure(wrapperStructGui); - opWrapper->exec(); - } - - void emplaceAndPop(std::vector>& l) + void start() { - auto datumsPtr = std::make_shared>>(l); - opWrapper->emplaceAndPop(datumsPtr); + try + { + opWrapper->start(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } } - void waitAndEmplace(std::vector>& l) + void stop() { - auto datumsPtr = std::make_shared>>(l); - opWrapper->waitAndEmplace(datumsPtr); + try + { + opWrapper->stop(); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } } - bool waitAndPop(std::vector>& l) + void exec() { - auto datumsPtr = std::make_shared>>(l); - return opWrapper->waitAndPop(datumsPtr); + try + { + const auto cameraSize = flagsToPoint(FLAGS_camera_resolution, "-1x-1"); + ProducerType producerType; + std::string producerString; + std::tie(producerType, producerString) = flagsToProducer( + FLAGS_image_dir, FLAGS_video, 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, 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) { - return op::getFilesOnDirectory(directoryPath, op::Extensions::Images); + try + { + return getFilesOnDirectory(directoryPath, Extensions::Images); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + return {}; + } } PYBIND11_MODULE(pyopenpose, m) { @@ -196,8 +283,8 @@ 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", &op::getGpuNumber, "Get Total GPU"); - m.def("get_images_on_directory", &op::getImagesFromDirectory, "Get Images On Directory"); + 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") @@ -213,62 +300,62 @@ PYBIND11_MODULE(pyopenpose, m) { ; // Datum Object - py::class_>(m, "Datum") + py::class_>(m, "Datum") .def(py::init<>()) - .def_readwrite("id", &op::Datum::id) - .def_readwrite("subId", &op::Datum::subId) - .def_readwrite("subIdMax", &op::Datum::subIdMax) - .def_readwrite("name", &op::Datum::name) - .def_readwrite("frameNumber", &op::Datum::frameNumber) - .def_readwrite("cvInputData", &op::Datum::cvInputData) - .def_readwrite("inputNetData", &op::Datum::inputNetData) - .def_readwrite("outputData", &op::Datum::outputData) - .def_readwrite("cvOutputData", &op::Datum::cvOutputData) - .def_readwrite("cvOutputData3D", &op::Datum::cvOutputData3D) - .def_readwrite("poseKeypoints", &op::Datum::poseKeypoints) - .def_readwrite("poseIds", &op::Datum::poseIds) - .def_readwrite("poseScores", &op::Datum::poseScores) - .def_readwrite("poseHeatMaps", &op::Datum::poseHeatMaps) - .def_readwrite("poseCandidates", &op::Datum::poseCandidates) - .def_readwrite("faceRectangles", &op::Datum::faceRectangles) - .def_readwrite("faceKeypoints", &op::Datum::faceKeypoints) - .def_readwrite("faceHeatMaps", &op::Datum::faceHeatMaps) - .def_readwrite("handRectangles", &op::Datum::handRectangles) - .def_readwrite("handKeypoints", &op::Datum::handKeypoints) - .def_readwrite("handHeatMaps", &op::Datum::handHeatMaps) - .def_readwrite("poseKeypoints3D", &op::Datum::poseKeypoints3D) - .def_readwrite("faceKeypoints3D", &op::Datum::faceKeypoints3D) - .def_readwrite("handKeypoints3D", &op::Datum::handKeypoints3D) - .def_readwrite("cameraMatrix", &op::Datum::cameraMatrix) - .def_readwrite("cameraExtrinsics", &op::Datum::cameraExtrinsics) - .def_readwrite("cameraIntrinsics", &op::Datum::cameraIntrinsics) - .def_readwrite("poseNetOutput", &op::Datum::poseNetOutput) - .def_readwrite("scaleInputToNetInputs", &op::Datum::scaleInputToNetInputs) - .def_readwrite("netInputSizes", &op::Datum::netInputSizes) - .def_readwrite("scaleInputToOutput", &op::Datum::scaleInputToOutput) - .def_readwrite("netOutputSize", &op::Datum::netOutputSize) - .def_readwrite("scaleNetToOutput", &op::Datum::scaleNetToOutput) - .def_readwrite("elementRendered", &op::Datum::elementRendered) + .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__", [](op::Rectangle &a) { return a.toString(); }) + py::class_>(m, "Rectangle") + .def("__repr__", [](Rectangle &a) { return a.toString(); }) .def(py::init<>()) .def(py::init()) - .def_readwrite("x", &op::Rectangle::x) - .def_readwrite("y", &op::Rectangle::y) - .def_readwrite("width", &op::Rectangle::width) - .def_readwrite("height", &op::Rectangle::height) + .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__", [](op::Point &a) { return a.toString(); }) + py::class_>(m, "Point") + .def("__repr__", [](Point &a) { return a.toString(); }) .def(py::init<>()) .def(py::init()) - .def_readwrite("x", &op::Point::x) - .def_readwrite("y", &op::Point::y) + .def_readwrite("x", &Point::x) + .def_readwrite("y", &Point::y) ; #ifdef VERSION_INFO @@ -291,23 +378,31 @@ template <> struct type_caster> { // Cast numpy to op::Array bool load(handle src, bool imp) { - // array b(src, true); - array b = reinterpret_borrow(src); - buffer_info info = b.request(); + try + { + // array b(src, true); + array b = reinterpret_borrow(src); + buffer_info info = b.request(); - if (info.format != format_descriptor::format()) - throw std::runtime_error("op::Array only supports float32 now"); + if (info.format != format_descriptor::format()) + op::error("op::Array only supports float32 now", __LINE__, __FUNCTION__, __FILE__); - //std::vector a(info.shape); - std::vector shape(std::begin(info.shape), std::end(info.shape)); + //std::vector a(info.shape); + std::vector shape(std::begin(info.shape), std::end(info.shape)); - // No copy - value = op::Array(shape, (float*)info.ptr); - // Copy - //value = op::Array(shape); - //memcpy(value.getPtr(), info.ptr, value.getVolume()*sizeof(float)); + // No copy + value = op::Array(shape, (float*)info.ptr); + // Copy + //value = op::Array(shape); + //memcpy(value.getPtr(), info.ptr, value.getVolume()*sizeof(float)); - return true; + return true; + } + catch (const std::exception& e) + { + op::error(e.what(), __LINE__, __FUNCTION__, __FILE__); + return {}; + } } // Cast op::Array to numpy