如何从Python脚本中运行Open Pose binary(.exe)?

如何从Python脚本中运行Open Pose binary(.exe)?,python,computer-vision,exe,executable,openpose,Python,Computer Vision,Exe,Executable,Openpose,我正在制作一个身体跟踪应用程序,如果用户选择跟踪他们的身体运动,我想在其中运行开放式姿势。OpenPose二进制文件可以按如下方式运行: bin\OpenPoseDemo.exe --write_json 'path\to\dump\output' 因此,在我的Python脚本中,我希望有一行运行OpenPose的代码,而不是要求用户通过打开单独的命令行窗口手动运行OpenPose。为此,我尝试了: import os os.popen(r"C:\path\to\bin\OpenPo

我正在制作一个身体跟踪应用程序,如果用户选择跟踪他们的身体运动,我想在其中运行开放式姿势。OpenPose二进制文件可以按如下方式运行:

bin\OpenPoseDemo.exe --write_json 'path\to\dump\output'
因此,在我的Python脚本中,我希望有一行运行OpenPose的代码,而不是要求用户通过打开单独的命令行窗口手动运行OpenPose。为此,我尝试了:

import os
os.popen(r"C:\path\to\bin\OpenPoseDemo.exe --write_json 'C:\path\to\dump\output'")
但这会产生以下错误:

Error:
Could not create directory: 'C:\Users\Admin\Documents\Openpose\. Status error = -1. Does the parent folder exist and/or do you have writing access to that path?
FileNotFoundError: [WinError 2] The system cannot find the file specified
我猜这意味着只有进入
bin
子目录所在的
OpenPose
目录才能打开OpenPose。因此,我编写了一个shell脚本,其中包含以下行:

bin\OpenPoseDemo.exe --write_json 'C:\path\to\dump\output'
并将其保存为
openpose
目录中的
run\u openpose\u binary.sh
(即
bin
所在的同一目录)

然后,我尝试在Python脚本中运行此shell脚本,如下所示:

import subprocess
subprocess.call(['sh', r'C:\path\to\openpose\run_openpose_binary.sh'])
这会产生以下错误:

Error:
Could not create directory: 'C:\Users\Admin\Documents\Openpose\. Status error = -1. Does the parent folder exist and/or do you have writing access to that path?
FileNotFoundError: [WinError 2] The system cannot find the file specified
我还尝试了以下方法:

os.popen(r"C:\path\to\openpose\run_openpose_binary.sh")

这些不会产生任何错误,只会弹出一个空白窗口并关闭


因此,我的问题是,如何从Python脚本中运行OpenPoseDemo.exe?

对于上一个方法,您缺少os.popen的返回值,这是一个管道。因此,您需要的是:

# untested as I don't have access to a Windows system
import os


with os.popen(r"/full/path/to/sh C:/path/to/openpose/run_openpose_binary.sh") as p:
    # pipes work like files
    output_of_command = p.read().strip() # this is a string
或者,如果您想证明自己的未来,另一种选择是:

# untested as I don't have access to a Windows system
popen = subprocess.Popen([r'/full/path/to/sh.exe', r'/full/path/to/run_openpose_binary.sh')],  stdin=subprocess.PIPE, stdout=subprocess.PIPE,encoding='utf-8')
stdout, stderr = popen.communicate(input='')

如果您还有其他困难,请留言。

我尝试了您的两种方法。第一种方法产生的输出与我在上一种方法中提到的相同,即它只是弹出一个空白窗口并关闭。在第二种方法中(通过将
替换为
]
修复语法错误后)]
,我得到以下错误:
OSError:[WinError 193]%1不是有效的Win32应用程序
。现在重试吗?我错过了炮弹指示器,还是一样。第一种方法只是弹出一个空白窗口并关闭。第二种方法这次产生了一个不同的错误:
PermissionError:[WinError 5]访问被拒绝
。然后尝试以超级用户身份运行第二种方法?是的,但在Windows中如何做到这一点?我在谷歌上搜索了一下,所有的答案似乎都是针对unix/linux的。实际上,我对Windows不太熟悉,但我必须在这个项目中使用Windows,因为我的MacBook没有Nvidia GPU,这个项目需要Nvidia GPU才能运行:(