Improve Python API including enabled AsynchronousOut mode (#1593)

* Move producer configuration into configure(...) method of Python API

* Enable output from OpenPose in Python API by making VectorDatum an opaque STL type

* Return empty arrays as None in Python API

* Add type_caster for Array<long long> for datum.poseIds in Python API

* Add ThreadManagerMode enum to Python interface

* Update Python tutorial in line with updated interface and add demo of AsynchronousOut mode

* Remove static_cast of ThreadManagerMode which is no longer needed

* Only set up producer in Python API when SynchronousIn is set

* Fix up memory handling in emplaceAndPop

* Pass AsynchronousOut in async out example
This commit is contained in:
Frankie Robertson
2020-10-29 10:48:49 -04:00
committed by GitHub
parent 69ce288772
commit 7b960082e2
11 changed files with 200 additions and 36 deletions
@@ -60,7 +60,7 @@ try:
datum = op.Datum()
imageToProcess = cv2.imread(args[0].image_path)
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Display Image
print("Body keypoints: \n" + str(datum.poseKeypoints))
@@ -62,7 +62,7 @@ try:
datum = op.Datum()
imageToProcess = cv2.imread(args[0].image_path)
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Display Image
print("Body keypoints: \n" + str(datum.poseKeypoints))
@@ -67,7 +67,7 @@ try:
datum = op.Datum()
imageToProcess = cv2.imread(imagePath)
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
print("Body keypoints: \n" + str(datum.poseKeypoints))
@@ -71,7 +71,6 @@ try:
for imageBaseId in range(0, len(imagePaths), numberGPUs):
# Create datums
datums = []
images = []
# Read and push images into OpenPose wrapper
@@ -84,8 +83,7 @@ try:
datum = op.Datum()
images.append(cv2.imread(imagePath))
datum.cvInputData = images[-1]
datums.append(datum)
opWrapper.waitAndEmplace([datums[-1]])
opWrapper.waitAndEmplace(op.VectorDatum([datum]))
# Retrieve processed results from OpenPose wrapper
for gpuId in range(0, numberGPUs):
@@ -93,8 +91,9 @@ try:
imageId = imageBaseId+gpuId
if imageId < len(imagePaths):
datum = datums[gpuId]
opWrapper.waitAndPop([datum])
datums = op.VectorDatum()
opWrapper.waitAndPop(datums)
datum = datums[0]
print("Body keypoints: \n" + str(datum.poseKeypoints))
@@ -74,7 +74,7 @@ try:
datum.faceRectangles = faceRectangles
# Process and display image
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
print("Face keypoints: \n" + str(datum.faceKeypoints))
cv2.imshow("OpenPose 1.6.0 - Tutorial Python API", datum.cvOutputData)
cv2.waitKey(0)
@@ -86,7 +86,7 @@ try:
datum.handRectangles = handRectangles
# Process and display image
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
print("Left hand keypoints: \n" + str(datum.handKeypoints[0]))
print("Right hand keypoints: \n" + str(datum.handKeypoints[1]))
cv2.imshow("OpenPose 1.6.0 - Tutorial Python API", datum.cvOutputData)
@@ -64,7 +64,7 @@ try:
datum = op.Datum()
imageToProcess = cv2.imread(args[0].image_path)
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Process outputs
outputImageF = (datum.inputNetData[0].copy())[0,:,:,:] + 0.5
@@ -77,7 +77,7 @@ try:
datum = op.Datum()
datum.cvInputData = imageToProcess
datum.poseNetOutput = poseHeatMaps
opWrapper.emplaceAndPop([datum])
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Display Image
print("Body keypoints: \n" + str(datum.poseKeypoints))
@@ -0,0 +1,89 @@
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import argparse
def display(datums):
datum = datums[0]
cv2.imshow("OpenPose 1.6.0 - Tutorial Python API", datum.cvOutputData)
key = cv2.waitKey(1)
return (key == 27)
def printKeypoints(datums):
datum = datums[0]
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]))
try:
# 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("--no-display", action="store_true", help="Disable display.")
args = parser.parse_known_args()
# 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
# Construct it from system arguments
# op.init_argv(args[1])
# oppython = op.OpenposePython()
# Starting OpenPose
opWrapper = op.WrapperPython(op.ThreadManagerMode.AsynchronousOut)
opWrapper.configure(params)
opWrapper.start()
# Main loop
userWantsToExit = False
while not userWantsToExit:
# Pop frame
datumProcessed = op.VectorDatum()
if opWrapper.waitAndPop(datumProcessed):
if not args[0].no_display:
# Display image
userWantsToExit = display(datumProcessed)
printKeypoints(datumProcessed)
else:
break
except Exception as e:
print(e)
sys.exit(-1)
@@ -52,7 +52,7 @@ try:
# oppython = op.OpenposePython()
# Starting OpenPose
opWrapper = op.WrapperPython(3)
opWrapper = op.WrapperPython(op.ThreadManagerMode.Synchronous)
opWrapper.configure(params)
opWrapper.execute()
except Exception as e: