user10976183
user10976183

Reputation: 287

No output when trying to run a shell command from python

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)

Upvotes: 0

Views: 391

Answers (1)

Laurent
Laurent

Reputation: 13518

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)

Upvotes: 1

Related Questions