import osimport subprocessimport globimport tempfilefrom IPython.display import Videoimport randomcurfolder = os.getcwd()# Openpose demo.exe locationopenposefol = curfolder+'/openpose/'openpose_demo_loc = openposefol +'/bin/OpenPoseDemo.exe'# This is the model to employmodel_to_employ ='BODY_135'# List folders in a main folderfolderstotrack = glob.glob(curfolder +'/projectdata/*')# Get all folders per participant, per sessionpcnfolders = []for i in folderstotrack: pcn1folders = glob.glob(i +'/P0/*') pcn2folders = glob.glob(i +'/P1/*') pcnfolders_in_session = pcn1folders + pcn2folders# Append to the list pcnfolders = pcnfolders + pcnfolders_in_session# There might be some other things we don't want nowpcnfolders = [x for x in pcnfolders if'Config'notin x]pcnfolders = [x for x in pcnfolders if'opensim'notin x]pcnfolders = [x for x in pcnfolders if'xml'notin x]pcnfolders = [x for x in pcnfolders if'ResultsInverseDynamics'notin x]pcnfolders = [x for x in pcnfolders if'ResultsInverseKinematics'notin x]pcnfolders = [x for x in pcnfolders if'sto'notin x]pcnfolders = [x for x in pcnfolders if'txt'notin x]print(pcnfolders[0:10])
Running OpenPose as a subprocess
Instead of running OpenPose in the notebook, we will send commands to the OpenPose executable as a subprocess. This is more efficient and allows us to run OpenPose in parallel for multiple videos.
Code to run OpenPose as a subprocess
def runcommand(command):# Run the command using subprocess for OPENPOSE TRACKINGtry: subprocess.run(command, shell=True, check=True)except subprocess.CalledProcessError as e:print(f"Command execution failed with error code {e.returncode}")exceptFileNotFoundError:print("The OpenPoseDemo.exe executable was not found.")
for i in pcnfolders: os.chdir(openposefol)print('working on '+ i)# Identify all avi files in folder direc = glob.glob(i +'/raw-2d/'+'*.avi')# 3 cameras video0 = direc[0] video1 = direc[1] video2 = direc[2] videolist = [video0, video1, video2]# Make a new directory if it doesn't existifnot os.path.exists(i+'/pose/'): os.makedirs(i+'/pose/')ifnot os.path.exists(i+'/pose/pose_cam1_json/'): os.makedirs(i+'/pose/pose_cam1_json/')ifnot os.path.exists(i+'/pose/pose_cam2_json/'): os.makedirs(i+'/pose/pose_cam2_json/')ifnot os.path.exists(i+'/pose/pose_cam3_json/'): os.makedirs(i+'/pose/pose_cam3_json/')# Also make directory for openpose videos (pose-2d-trackingvideos)ifnot os.path.exists(i+'/pose-2d-trackingvideos/'): os.makedirs(i+'/pose-2d-trackingvideos/')# Initialize the pose2 folder outputfol1 = i+'/pose/pose_cam1_json/' outputfol2 = i+'/pose/pose_cam2_json/' outputfol3 = i+'/pose/pose_cam3_json/' outputfollist = [outputfol1, outputfol2, outputfol3]for it, j inenumerate(outputfollist):# Prepare the command openposelocation =' '+ openpose_demo_loc +' ' model ='--model_pose'+' '+ model_to_employ +' ' video ='--video '+ videolist[it] +' ' todo ='--write_json ' outputfol = j +' ' videoadd ='--write_video ' videopath = i+'/pose-2d-trackingvideos/'+'video'+str(it)+'.avi'+' '# Send the command via subprocess command =r' '+openposelocation+model+video+todo+outputfol+videoadd+videopathprint('were going to send this to command prompt: '+ command) runcommand(command)