09_keypoints_from_heatmaps.py 3.3 KB
Newer Older
R
Raaj 已提交
1 2 3 4 5 6 7 8 9 10
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import argparse
import numpy as np

try:
G
Gines Hidalgo 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    # 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
R
Raaj 已提交
29

G
Gines Hidalgo 已提交
30 31 32 33
    # 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()
R
Raaj 已提交
34

G
Gines Hidalgo 已提交
35 36
    # Load image
    imageToProcess = cv2.imread(args[0].image_path)
R
Raaj 已提交
37

G
Gines Hidalgo 已提交
38 39 40 41 42 43 44 45 46 47
    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
R
Raaj 已提交
48

G
Gines Hidalgo 已提交
49 50 51 52
        # Starting OpenPose
        opWrapper = op.WrapperPython()
        opWrapper.configure(params)
        opWrapper.start()
R
Raaj 已提交
53

G
Gines Hidalgo 已提交
54 55 56 57
        # Process Image and get heatmap
        datum = op.Datum()
        imageToProcess = cv2.imread(args[0].image_path)
        datum.cvInputData = imageToProcess
P
Pinocchioo 已提交
58
        opWrapper.emplaceAndPop(op.VectorDatum([datum]))
G
Gines Hidalgo 已提交
59 60
        poseHeatMaps = datum.poseHeatMaps.copy()
        opWrapper.stop()
R
Raaj 已提交
61

G
Gines Hidalgo 已提交
62
        return poseHeatMaps
R
Raaj 已提交
63

64 65
    # Get Heatmap
    poseHeatMaps = get_sample_heatmaps()
R
Raaj 已提交
66

67 68 69 70 71 72 73 74
    # 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()
R
Raaj 已提交
75

76 77 78 79
    # Pass Heatmap and Run OP
    datum = op.Datum()
    datum.cvInputData = imageToProcess
    datum.poseNetOutput = poseHeatMaps
80
    opWrapper.emplaceAndPop(op.VectorDatum([datum]))
R
Raaj 已提交
81

82 83
    # Display Image
    print("Body keypoints: \n" + str(datum.poseKeypoints))
G
Gines Hidalgo 已提交
84
    cv2.imshow("OpenPose 1.7.0 - Tutorial Python API", datum.cvOutputData)
85 86
    cv2.waitKey(0)
except Exception as e:
G
Gines Hidalgo 已提交
87 88
    print(e)
    sys.exit(-1)