-1

I am making a body tracking application where I want to run Open Pose if the user chooses to track their body movements. The OpenPose binary file can be run like so:

bin\OpenPoseDemo.exe --write_json 'path\to\dump\output'

So, in my Python script, I want to have a line of code that would run Open Pose, instead of having to ask the user to manually run OpenPose by opening a separate command line window. For that, I have tried:

import os
os.popen(r"C:\path\to\bin\OpenPoseDemo.exe --write_json 'C:\path\to\dump\output'")

But this gives the following error:

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?

Which I guess means that OpenPose can be opened only by going inside the openpose directory where the bin subdirectory resides. So, I wrote a shell script containing this line:

bin\OpenPoseDemo.exe --write_json 'C:\path\to\dump\output'

and saved it as run_openpose_binary.sh in the openpose directory (i.e., the same directory where bin is located).

I then tried to run this shell script from within my Python script like so:

import subprocess
subprocess.call(['sh', r'C:\path\to\openpose\run_openpose_binary.sh'])

and this gives the following error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

I also tried the following:

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

and

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

These do not produce any error, but instead just pop up a blank window and closes.

So, my question is, how do I run the OpenPoseDemo.exe from within my Python script?

Kristada673
  • 3,512
  • 6
  • 39
  • 93

2 Answers2

0

For your last method, you're missing the return value from os.popen, which is a pipe. So, what you need is something like:

# 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

or, if you want to future-proof yourself, the alternative is:

# 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='')

Leave a comment if you have further difficulty.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • I tried both your methods. The first approach produces the same output as the one I mentioned in my last approach, which is that it just pops up a blank window and closes. And in your second approach (after fixing the syntax error by replacing the `)]` with `]`), I get the following error: `OSError: [WinError 193] %1 is not a valid Win32 application`. – Kristada673 Jul 30 '20 at 11:17
  • Try now? I missed the shell designator. – hd1 Jul 30 '20 at 14:13
  • Still the same. The first method just pops up a blank window and closes. And the second method produces a different error this time: `PermissionError: [WinError 5] Access is denied`. – Kristada673 Jul 31 '20 at 05:05
  • Try running the second as the superuser then? – hd1 Jul 31 '20 at 05:08
  • Yeah, but how do I do that in Windows? I googled and all the answers seem to be for unix/linux. I am not too familiar with Windows actually, but I'm having to use WIndows for this project as my MacBook doesn't have Nvidia GPU and this projects needs an Nvidia GPU to run :( – Kristada673 Jul 31 '20 at 05:14
  • If you right click on the application, you shuld see a ``Run as Administrator'' option. – hd1 Jul 31 '20 at 06:42
0

I've had to fight this battle several times and I've found a solution. It's likely not the most elegant solution but it does work, and I'll explain it using an example of how to run OpenPose on a video. You've got your path to the openpose download and your path to the video, and from there it's a 3-line solution. First, change the current working directory to that openpose folder, and then create your command, then call subprocess.run (I tried using subprocess.call and that did not work. I did not try shell=False but I have heard it's a safer way to do so. I'll leave that up to you.)

import os
import subprocess

openpose_path = "C:\\Users\\me\\Desktop\\openpose-1.7.0-binaries-win64-gpu-python3.7-flir-3d_recommended\\openpose\\"
video_path = "C:\\Users\\me\\Desktop\\myvideo.mp4"
os.chdir(openpose_path)
command = "".join(["bin\\OpenPoseDemo.exe", " -video ", video_path])
subprocess.run(command, shell=True)
Breadman10
  • 81
  • 6