0

I've never created PowerShell script or anything like that so that's why I'm trying to run the command from Python instead since I thought all I have to do is just called it using the os.popen() command.

I have over 5000 folders all containing images that I'm going to extract keypoints from using a script that I've downloaded from github.

When I tried running my Python script nothing shows up. There's a window containing the image with the keypoints that is supposed to show up when I run the command but nothing shows up.

I have tried the command in PowerShell on one folder and it works perfectly.

Here's my script:

import os
import sys
import time

os.chdir(
    r"C:\Users\Adam\Downloads\openpose-1.7.0-binaries-win64-cpu-python3.7-flir-3d\openpose"
)
for root, dirs, files in os.walk(
    r"C:\Users\Adam\Downloads\LIP_MPV_256_192\MPV_256_192\all\all\images\train"
):
    for d in dirs:
        print("got here")
        os.popen(
            "bin\\OpenPoseDemo.exe --image_dir"
            + " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\train\\"
            + d
            + "--write_json"
            + " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\pose_coco\\train\\"
            + d
        )
        time.sleep(5)
1
  • The os.chdir() seems dubious; does the binary not work if you are not in that directory? Commented May 4, 2021 at 8:15

1 Answer 1

1

You could try with subprocess module from the Python standard library:

import os
import subprocess
import time

os.chdir(
    r"C:\Users\Adam\Downloads\openpose-1.7.0-binaries-win64-cpu-python3.7-flir-3d\openpose"
)
for root, dirs, files in os.walk(
    r"C:\Users\Adam\Downloads\LIP_MPV_256_192\MPV_256_192\all\all\images\train"
):
    for d in dirs:
        print("got here")
        command = [
            "bin\\OpenPoseDemo.exe",
            "--image_dir",
            " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\train\\" + d,
            "--write_json",
            " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\pose_coco\\train\\"
            + d
        ]
        subprocess.run(args=command, shell=False, capture_output=True)
        time.sleep(5)
Sign up to request clarification or add additional context in comments.

3 Comments

With shell=False the command should be a list, not a string. Windows is less sensitive to this, but breaking up the command line to a list of tokens is also less error-prone.
subprocess.run is definitely the way to go, though perhaps it could be mentioned that os.popen() is (now) just an alias for subprocess.Popen(). Using the higher-level run() function is absolutely recommended, unless you need behavior which run cannot accommodate, such as parallel processing (and then you need to do a fair bit of additional housekeeping around the subprocess which run takes care of for you).
@triplee Indeed, I have corrected my answer. Also, thank you for these clarifications and additional information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.