|
|
# From Python
|
|
|
# It requires OpenCV installed for Python
|
|
|
import sys
|
|
|
import cv2
|
|
|
import os
|
|
|
from sys import platform
|
|
|
import argparse
|
|
|
import time
|
|
|
|
|
|
try:
|
|
|
# Import Openpose (Windows/Ubuntu/OSX)
|
|
|
# 获取当前脚本文件的目录路径
|
|
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
try:
|
|
|
# 检查操作系统类型
|
|
|
# Windows Import
|
|
|
# 对于Windows系统
|
|
|
if platform == "win32":
|
|
|
|
|
|
# 修改这些变量以指向正确的文件夹(如Release/x64等)
|
|
|
# 使用 os.path.join 来避免路径分隔符的问题
|
|
|
openpose_path = os.path.join(dir_path, '../../python/openpose/Release')
|
|
|
sys.path.append(openpose_path)
|
|
|
#sys.path.append(dir_path + '/../../python/openpose/Release');
|
|
|
#os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;'
|
|
|
# 设置环境变量PATH,以便可以在命令行中调用OpenPose的可执行文件
|
|
|
# 在Python脚本中通常不需要这样做,这样可以在脚本中调用OpenPose的可执行文件
|
|
|
# 使用 os.pathsep 来确保路径分隔符的正确性
|
|
|
os.environ['PATH'] = os.environ['PATH'] + os.pathsep + os.path.join(dir_path,'../../x64/Release') \
|
|
|
+ os.pathsep + os.path.join(dir_path, '../../bin')
|
|
|
# 导入OpenPose的Python接口
|
|
|
import pyopenpose as op
|
|
|
else:
|
|
|
# 对于Linux或MacOS系统
|
|
|
# 修改这些变量以指向正确的文件夹(如Release/x64等)
|
|
|
# 这里假设OpenPose的Python库位于相对于脚本的固定路径下
|
|
|
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')
|
|
|
# 如果运行了 `make install`(对于Ubuntu,默认路径是 `/usr/local/python`),则也可以从那里访问OpenPose/python模块
|
|
|
# 注意:通常不需要这样做,除非OpenPose的Python库确实被安装到了非标准路径
|
|
|
# 并且,下面的代码行已经被注释掉了,因为它可能会覆盖掉其他重要的Python库路径
|
|
|
# sys.path.append('/usr/local/python')
|
|
|
# 从openpose模块导入pyopenpose
|
|
|
from openpose import pyopenpose as op
|
|
|
except ImportError as e:
|
|
|
# 如果在尝试导入pyopenpose时发生错误,则打印错误消息并重新抛出异常
|
|
|
print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
|
|
|
|
|
|
# 打印原始异常信息
|
|
|
print(e)
|
|
|
# 重新抛出异常,以便调用者可以处理它
|
|
|
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()
|
|
|
|
|
|
# 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()
|
|
|
opWrapper.configure(params)
|
|
|
opWrapper.start()
|
|
|
|
|
|
# 读取目录中的图片
|
|
|
imagePaths = op.get_images_on_directory(args[0].image_dir)
|
|
|
start = time.time()
|
|
|
|
|
|
# 处理并显示图片
|
|
|
for imagePath in imagePaths:
|
|
|
datum = op.Datum()
|
|
|
imageToProcess = cv2.imread(imagePath)
|
|
|
datum.cvInputData = imageToProcess
|
|
|
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
|
|
|
|
|
|
print("Body keypoints: \n" + str(datum.poseKeypoints))
|
|
|
|
|
|
if not args[0].no_display:
|
|
|
cv2.imshow("OpenPose 1.7.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")
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
sys.exit(-1)
|