Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update openpose_3dpose_sandbox_realtime.py #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 66 additions & 65 deletions src/openpose_3dpose_sandbox_realtime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
Expand All @@ -15,6 +14,8 @@
import time
import logging
import glob
import csv

FLAGS = tf.app.flags.FLAGS

order = [15, 12, 25, 26, 27, 17, 18, 19, 1, 2, 3, 6, 7, 8]
Expand All @@ -23,7 +24,6 @@
logger = logging.getLogger(__name__)



def main(_):
done = []

Expand All @@ -44,100 +44,100 @@ def main(_):
with tf.Session(config=tf.ConfigProto(
device_count=device_count,
allow_soft_placement=True)) as sess:
#plt.figure(3)
# plt.figure(3)
batch_size = 128
model = create_model(sess, actions, batch_size)
while True:
key = cv2.waitKey(1) & 0xFF
#logger.info("start reading data")
# logger.info("start reading data")
# check for other file types
list_of_files = glob.iglob("{0}/*".format(openpose_output_dir)) # You may use iglob in Python3
latest_file = ""
try:
latest_file = max(list_of_files, key=os.path.getctime)
except ValueError:
#empthy dir
# empthy dir
pass
if not latest_file:
continue
try:
_file = file_name = latest_file
print (latest_file)
print(latest_file)
if not os.path.isfile(_file): raise Exception("No file found!!, {0}".format(_file))
data = json.load(open(_file))
#take first person
# take first person
_data = data["people"][0]["pose_keypoints_2d"]
xy = []
if len(_data)>=53:
#openpose incl. confidence score
#ignore confidence score
for o in range(0,len(_data),3):
if len(_data) >= 53:
# openpose incl. confidence score
# ignore confidence score
for o in range(0, len(_data), 3):
xy.append(_data[o])
xy.append(_data[o+1])
xy.append(_data[o + 1])
else:
#tf-pose-estimation
# tf-pose-estimation
xy = _data

frame_indx = re.findall("(\d+)", file_name)
frame = int(frame_indx[-1])
logger.debug("found {0} for frame {1}".format(xy, str(frame)))

#body_25 support, convert body_25 output format to coco
if len(xy)>54:
_xy = xy[0:19*2]
# body_25 support, convert body_25 output format to coco
if len(xy) > 54:
_xy = xy[0:19 * 2]
for x in range(len(xy)):
#del jnt 8
if x==8*2:
# del jnt 8
if x == 8 * 2:
del _xy[x]
if x==8*2+1:
if x == 8 * 2 + 1:
del _xy[x]
#map jnt 9 to 8
if x==9*2:
# map jnt 9 to 8
if x == 9 * 2:
_xy[16] = xy[x]
_xy[17] = xy[x+1]
#map jnt 10 to 9
if x==10*2:
_xy[17] = xy[x + 1]
# map jnt 10 to 9
if x == 10 * 2:
_xy[18] = xy[x]
_xy[19] = xy[x+1]
#map jnt 11 to 10
if x==11*2:
_xy[19] = xy[x + 1]
# map jnt 11 to 10
if x == 11 * 2:
_xy[20] = xy[x]
_xy[21] = xy[x+1]
#map jnt 12 to 11
if x==12*2:
_xy[21] = xy[x + 1]
# map jnt 12 to 11
if x == 12 * 2:
_xy[22] = xy[x]
_xy[23] = xy[x+1]
#map jnt 13 to 12
if x==13*2:
_xy[23] = xy[x + 1]
# map jnt 13 to 12
if x == 13 * 2:
_xy[24] = xy[x]
_xy[25] = xy[x+1]
#map jnt 14 to 13
if x==14*2:
_xy[25] = xy[x + 1]
# map jnt 14 to 13
if x == 14 * 2:
_xy[26] = xy[x]
_xy[27] = xy[x+1]
#map jnt 15 to 14
if x==15*2:
_xy[27] = xy[x + 1]
# map jnt 15 to 14
if x == 15 * 2:
_xy[28] = xy[x]
_xy[29] = xy[x+1]
#map jnt 16 to 15
if x==16*2:
_xy[29] = xy[x + 1]
# map jnt 16 to 15
if x == 16 * 2:
_xy[30] = xy[x]
_xy[31] = xy[x+1]
#map jnt 17 to 16
if x==17*2:
_xy[31] = xy[x + 1]
# map jnt 17 to 16
if x == 17 * 2:
_xy[32] = xy[x]
_xy[33] = xy[x+1]
#map jnt 18 to 17
if x==18*2:
_xy[33] = xy[x + 1]
# map jnt 18 to 17
if x == 18 * 2:
_xy[34] = xy[x]
_xy[35] = xy[x+1]
#coco
_xy[35] = xy[x + 1]
# coco
xy = _xy

joints_array = np.zeros((1, 36))
joints_array[0] = [0 for i in range(36)]
for o in range(len(joints_array[0])):
#feed array with xy array
# feed array with xy array
joints_array[0][o] = xy[o]
_data = joints_array[0]
# mapping all body parts or 3d-pose-baseline format
Expand Down Expand Up @@ -172,8 +172,8 @@ def main(_):
gs1 = gridspec.GridSpec(1, 1)
gs1.update(wspace=-0.00, hspace=0.05) # set the spacing between axes.
plt.axis('off')
all_poses_3d.append( poses3d )
enc_in, poses3d = map( np.vstack, [enc_in, all_poses_3d] )
all_poses_3d.append(poses3d)
enc_in, poses3d = map(np.vstack, [enc_in, all_poses_3d])
subplot_idx, exidx = 1, 1
_max = 0
_min = 10000
Expand All @@ -196,42 +196,43 @@ def main(_):

# Plot 3d predictions
ax = plt.subplot(gs1[subplot_idx - 1], projection='3d')
ax.view_init(18, -70)
ax.view_init(18, -70)
logger.debug(np.min(poses3d))
if np.min(poses3d) < -1000 and frame != 0:
poses3d = before_pose

p3d = poses3d
#enter file path below
with open('newcsvfile.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(p3d)

viz.show3Dpose(p3d, ax, lcolor="#9b59b6", rcolor="#2ecc71")
before_pose = poses3d
pngName = 'png/test_{0}.png'.format(str(frame))
plt.savefig(pngName)

#plt.show()
img = cv2.imread(pngName,0)
# plt.show()
img = cv2.imread(pngName, 0)
rect_cpy = img.copy()
cv2.imshow('3d-pose-baseline', rect_cpy)
done.append(file_name)
if key == ord('q'):
break
except Exception as e:
print (e)
print(e)

sess.close()



if __name__ == "__main__":

openpose_output_dir = FLAGS.pose_estimation_json

level = {0:logging.ERROR,
1:logging.WARNING,
2:logging.INFO,
3:logging.DEBUG}

logger.setLevel(level[FLAGS.verbose])
level = {0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG}

logger.setLevel(level[FLAGS.verbose])

tf.app.run()