05_keypoints_from_images_multi_gpu.py 4.0 KB
Newer Older
G
gineshidalgo99 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
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

# 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.")
33
parser.add_argument("--num_gpu", default=op.get_gpu_number(), help="Number of GPUs.")
G
gineshidalgo99 已提交
34 35 36 37 38
args = parser.parse_known_args()

# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
params["model_folder"] = "../../../models/"
39 40
params["num_gpu"] = int(vars(args[0])["num_gpu"])
numberGPUs = int(params["num_gpu"])
G
gineshidalgo99 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

# 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()

58 59 60 61 62
try:
    # Starting OpenPose
    opWrapper = op.WrapperPython()
    opWrapper.configure(params)
    opWrapper.start()
G
gineshidalgo99 已提交
63

64 65
    # Read frames on directory
    imagePaths = op.get_images_on_directory(args[0].image_dir);
G
gineshidalgo99 已提交
66

67 68
    # Read number of GPUs in your system
    start = time.time()
G
gineshidalgo99 已提交
69

70 71
    # Process and display images
    for imageBaseId in range(0, len(imagePaths), numberGPUs):
G
gineshidalgo99 已提交
72

73 74 75
        # Create datums
        datums = []
        images = []
G
gineshidalgo99 已提交
76

77 78
        # Read and push images into OpenPose wrapper
        for gpuId in range(0, numberGPUs):
G
gineshidalgo99 已提交
79

80 81
            imageId = imageBaseId+gpuId
            if imageId < len(imagePaths):
G
gineshidalgo99 已提交
82

83 84 85 86 87 88
                imagePath = imagePaths[imageBaseId+gpuId]
                datum = op.Datum()
                images.append(cv2.imread(imagePath))
                datum.cvInputData = images[-1]
                datums.append(datum)
                opWrapper.waitAndEmplace([datums[-1]])
G
gineshidalgo99 已提交
89

90 91
        # Retrieve processed results from OpenPose wrapper
        for gpuId in range(0, numberGPUs):
G
gineshidalgo99 已提交
92

93 94
            imageId = imageBaseId+gpuId
            if imageId < len(imagePaths):
G
gineshidalgo99 已提交
95

96 97
                datum = datums[gpuId]
                opWrapper.waitAndPop([datum])
G
gineshidalgo99 已提交
98

99
                print("Body keypoints: \n" + str(datum.poseKeypoints))
G
gineshidalgo99 已提交
100

101
                if not args[0].no_display:
G
Gines Hidalgo 已提交
102
                    cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData)
103 104
                    key = cv2.waitKey(15)
                    if key == 27: break
G
gineshidalgo99 已提交
105

106 107 108 109 110
    end = time.time()
    print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds")
except Exception as e:
    # print(e)
    sys.exit(-1)