python程序小练
"""
Demo of HMR.
Note that HMR requires the bounding box of the person in the image. The best performance is obtained
when max length of the person in the image is roughly 150px.
When only the image path is supplied, it assumes that the image is centered on a person whose length
is roughly 150px.
Alternatively, you can supply output of the openpose to figure out the bbox and the right scale fact
or.
Sample usage:
# On images on a tightly cropped image around the person
python -m demo --img_path data/im1963.jpg
python -m demo --img_path data/coco1.png
# On images, with openpose output
python -m demo --img_path data/random.jpg --json_path data/random_keypoints.json
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import sys
from absl import flags
import numpy as np
import skimage.io as io
import tensorflow as tf
from src.util import renderer as vis_util
from src.util import image as img_util
from src.util import openpose as op_util
import src.config
from src.RunModel import RunModel
flags.DEFINE_string('img_path', 'data/im1963.jpg', 'Image to run')
flags.DEFINE_string(
'json_path', None,
'If specified, uses the openpose output to crop the image.')
def visualize(img, proc_param, joints, verts, cam):
"""
Renders the result in original image coordinate frame.
"""
cam_for_render, vert_shifted, joints_orig = vis_util.get_original(
proc_param, verts, cam, joints, img_size=img.shape[:2])
# Render results
skel_img = vis_util.draw_skeleton(img, joints_orig)
rend_img_overlay = renderer(
vert_shifted, cam=cam_for_render, img=img, do_alpha=True)
rend_img = renderer(
vert_shifted, cam=cam_for_render, img_size=img.shape[:2])
rend_img_vp1 = renderer.rotated(
vert_shifted, 60, cam=cam_for_render, img_size=img.shape[:2])
rend_img_vp2 = renderer.rotated(
vert_shifted, -60, cam=cam_for_render, img_size=img.shape[:2])
import matplotlib.pyplot as plt
# plt.ion()
plt.figure(1)
plt.clf()
plt.subplot(231)
plt.imshow(img)
plt.title('input')
plt.axis('off')
plt.subplot(232)
plt.imshow(skel_img)
plt.title('joint projection')
plt.axis('off')
plt.subplot(233)
plt.imshow(rend_img_overlay)
plt.title('3D Mesh overlay')
plt.axis('off')
plt.subplot(234)
plt.imshow(rend_img)
plt.title('3D mesh')
plt.axis('off')
plt.subplot(235)
plt.imshow(rend_img_vp1)
plt.title('diff vp')
plt.axis('off')
plt.subplot(236)
plt.imshow(rend_img_vp2)
plt.title('diff vp')
plt.axis('off')
plt.draw()
plt.show()
# import ipdb
# ipdb.set_trace()
def get_angle1(p1,p2,p3):
x1,y1,z1 = p1[0],p1[1],p1[2]
x2,y2,z2 = p2[0],p2[1],p2[2]
x3,y3,z3 = p3[0],p3[1],p3[2]
a=math.sqrt((x2-x3)**2+(y2-y3)**2+(z2-z3)**2)
b=math.sqrt((x1-x3)**2+(y1-y3)**2+(z1-z3)**2)
c=math.sqrt((x2-x1)**2+(y2-y1)**2+(z2-z1)**2)
if c*b==0:
cosA=1
else:
cosA=(a**2-c**2-b**2)/(-2*c*b)
if cosA < -1.0:
cosA=-1.0
elif cosA>1.0:
cosA=1.0
A=math.acos(cosA)
deg=math.degrees(A)
return deg
def preprocess_image(img_path, json_path=None):
img = io.imread(img_path)
if img.shape[2] == 4:
img = img[:, :, :3]
if json_path is None:
if np.max(img.shape[:2]) != config.img_size:
print('Resizing so the max image size is %d..' % config.img_size)
scale = (float(config.img_size) / np.max(img.shape[:2]))
else:
scale = 1.
center = np.round(np.array(img.shape[:2]) / 2).astype(int)
# image center in (x,y)
center = center[::-1]
else:
scale, center = op_util.get_bbox(json_path)
crop, proc_param = img_util.scale_and_crop(img, scale, center,
config.img_size)
# Normalize image to [-1, 1]
crop = 2 * ((crop / 255.) - 0.5)
return crop, proc_param, img
def main(img_path, json_path=None):
sess = tf.Session()
model = RunModel(config, sess=sess)
input_img, proc_param, img = preprocess_image(img_path, json_path)
# Add batch dimension: 1 x D x D x 3
input_img = np.expand_dims(input_img, 0)
# Theta is the 85D vector holding [camera, pose, shape]
# where camera is 3D [s, tx, ty]
# pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
# shape is 10D shape coefficients of SMPL
joints, verts, cams, joints3d, theta = model.predict(
input_img, get_theta=True)
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
ax = plt.axes(projection="3d")
xdata = joints3d[0][:,:1].reshape(1,19)[0]
ydata = joints3d[0][:,1:2].reshape(1,19)[0]
zdata = joints3d[0][:,2:3].reshape(1,19)[0]
#右抬臂角度
print(joints3d[0][2])
c1 = get_angle1(joints3d[0][12],joints3d[0][2],joints3d[0][13])
c2 = get_angle1(joints3d[0][12],joints3d[0][3],joints3d[0][13])
c3 = get_angle1(joints3d[0][8],joints3d[0][2],joints3d[0][7])
c4 = get_angle1(joints3d[0][9],joints3d[0][3],joints3d[0][10])
c5 = get_angle1(joints3d[0][7],joints3d[0][6],joints3d[0][8])
c6 = get_angle1(joints3d[0][10],joints3d[0][9],joints3d[0][11])
c7 = get_angle1(joints3d[0][2],joints3d[0][1],joints3d[0][3])
c8 = get_angle1(joints3d[0][3],joints3d[0][2],joints3d[0][4])
c9 = get_angle1(joints3d[0][1],joints3d[0][0],joints3d[0][2])
c10 = get_angle1(joints3d[0][4],joints3d[0][3],joints3d[0][5])
ax.scatter3D(xdata,ydata,zdata,cmap="Greens")
plt.show()
print(xdata)
print(ydata)
print(zdata)
print("2D坐标:",joints)
print("3D坐标:",joints3d)
print("cams",cams)
visualize(img, proc_param, joints[0], verts[0], cams[0])
if __name__ == '__main__':
config = flags.FLAGS
config(sys.argv)
# Using pre-trained model, change this to use your own.
config.load_path = src.config.PRETRAINED_MODEL
config.batch_size = 1
renderer = vis_util.SMPLRenderer(face_path=config.smpl_face_path)
main(config.img_path, config.json_path)
Demo of HMR.
Note that HMR requires the bounding box of the person in the image. The best performance is obtained
when max length of the person in the image is roughly 150px.
When only the image path is supplied, it assumes that the image is centered on a person whose length
is roughly 150px.
Alternatively, you can supply output of the openpose to figure out the bbox and the right scale fact
or.
Sample usage:
# On images on a tightly cropped image around the person
python -m demo --img_path data/im1963.jpg
python -m demo --img_path data/coco1.png
# On images, with openpose output
python -m demo --img_path data/random.jpg --json_path data/random_keypoints.json
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import sys
from absl import flags
import numpy as np
import skimage.io as io
import tensorflow as tf
from src.util import renderer as vis_util
from src.util import image as img_util
from src.util import openpose as op_util
import src.config
from src.RunModel import RunModel
flags.DEFINE_string('img_path', 'data/im1963.jpg', 'Image to run')
flags.DEFINE_string(
'json_path', None,
'If specified, uses the openpose output to crop the image.')
def visualize(img, proc_param, joints, verts, cam):
"""
Renders the result in original image coordinate frame.
"""
cam_for_render, vert_shifted, joints_orig = vis_util.get_original(
proc_param, verts, cam, joints, img_size=img.shape[:2])
# Render results
skel_img = vis_util.draw_skeleton(img, joints_orig)
rend_img_overlay = renderer(
vert_shifted, cam=cam_for_render, img=img, do_alpha=True)
rend_img = renderer(
vert_shifted, cam=cam_for_render, img_size=img.shape[:2])
rend_img_vp1 = renderer.rotated(
vert_shifted, 60, cam=cam_for_render, img_size=img.shape[:2])
rend_img_vp2 = renderer.rotated(
vert_shifted, -60, cam=cam_for_render, img_size=img.shape[:2])
import matplotlib.pyplot as plt
# plt.ion()
plt.figure(1)
plt.clf()
plt.subplot(231)
plt.imshow(img)
plt.title('input')
plt.axis('off')
plt.subplot(232)
plt.imshow(skel_img)
plt.title('joint projection')
plt.axis('off')
plt.subplot(233)
plt.imshow(rend_img_overlay)
plt.title('3D Mesh overlay')
plt.axis('off')
plt.subplot(234)
plt.imshow(rend_img)
plt.title('3D mesh')
plt.axis('off')
plt.subplot(235)
plt.imshow(rend_img_vp1)
plt.title('diff vp')
plt.axis('off')
plt.subplot(236)
plt.imshow(rend_img_vp2)
plt.title('diff vp')
plt.axis('off')
plt.draw()
plt.show()
# import ipdb
# ipdb.set_trace()
def get_angle1(p1,p2,p3):
x1,y1,z1 = p1[0],p1[1],p1[2]
x2,y2,z2 = p2[0],p2[1],p2[2]
x3,y3,z3 = p3[0],p3[1],p3[2]
a=math.sqrt((x2-x3)**2+(y2-y3)**2+(z2-z3)**2)
b=math.sqrt((x1-x3)**2+(y1-y3)**2+(z1-z3)**2)
c=math.sqrt((x2-x1)**2+(y2-y1)**2+(z2-z1)**2)
if c*b==0:
cosA=1
else:
cosA=(a**2-c**2-b**2)/(-2*c*b)
if cosA < -1.0:
cosA=-1.0
elif cosA>1.0:
cosA=1.0
A=math.acos(cosA)
deg=math.degrees(A)
return deg
def preprocess_image(img_path, json_path=None):
img = io.imread(img_path)
if img.shape[2] == 4:
img = img[:, :, :3]
if json_path is None:
if np.max(img.shape[:2]) != config.img_size:
print('Resizing so the max image size is %d..' % config.img_size)
scale = (float(config.img_size) / np.max(img.shape[:2]))
else:
scale = 1.
center = np.round(np.array(img.shape[:2]) / 2).astype(int)
# image center in (x,y)
center = center[::-1]
else:
scale, center = op_util.get_bbox(json_path)
crop, proc_param = img_util.scale_and_crop(img, scale, center,
config.img_size)
# Normalize image to [-1, 1]
crop = 2 * ((crop / 255.) - 0.5)
return crop, proc_param, img
def main(img_path, json_path=None):
sess = tf.Session()
model = RunModel(config, sess=sess)
input_img, proc_param, img = preprocess_image(img_path, json_path)
# Add batch dimension: 1 x D x D x 3
input_img = np.expand_dims(input_img, 0)
# Theta is the 85D vector holding [camera, pose, shape]
# where camera is 3D [s, tx, ty]
# pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
# shape is 10D shape coefficients of SMPL
joints, verts, cams, joints3d, theta = model.predict(
input_img, get_theta=True)
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
ax = plt.axes(projection="3d")
xdata = joints3d[0][:,:1].reshape(1,19)[0]
ydata = joints3d[0][:,1:2].reshape(1,19)[0]
zdata = joints3d[0][:,2:3].reshape(1,19)[0]
#右抬臂角度
print(joints3d[0][2])
c1 = get_angle1(joints3d[0][12],joints3d[0][2],joints3d[0][13])
c2 = get_angle1(joints3d[0][12],joints3d[0][3],joints3d[0][13])
c3 = get_angle1(joints3d[0][8],joints3d[0][2],joints3d[0][7])
c4 = get_angle1(joints3d[0][9],joints3d[0][3],joints3d[0][10])
c5 = get_angle1(joints3d[0][7],joints3d[0][6],joints3d[0][8])
c6 = get_angle1(joints3d[0][10],joints3d[0][9],joints3d[0][11])
c7 = get_angle1(joints3d[0][2],joints3d[0][1],joints3d[0][3])
c8 = get_angle1(joints3d[0][3],joints3d[0][2],joints3d[0][4])
c9 = get_angle1(joints3d[0][1],joints3d[0][0],joints3d[0][2])
c10 = get_angle1(joints3d[0][4],joints3d[0][3],joints3d[0][5])
ax.scatter3D(xdata,ydata,zdata,cmap="Greens")
plt.show()
print(xdata)
print(ydata)
print(zdata)
print("2D坐标:",joints)
print("3D坐标:",joints3d)
print("cams",cams)
visualize(img, proc_param, joints[0], verts[0], cams[0])
if __name__ == '__main__':
config = flags.FLAGS
config(sys.argv)
# Using pre-trained model, change this to use your own.
config.load_path = src.config.PRETRAINED_MODEL
config.batch_size = 1
renderer = vis_util.SMPLRenderer(face_path=config.smpl_face_path)
main(config.img_path, config.json_path)
上一篇:盛夏光年 拼音版
下一篇:二十四条逻辑谬误(六)(730)
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章熱度:★★★☆☆
文章難度:★★★★☆
文章質量:☆☆☆☆☆
說明:系統根據文章的熱度、難度、質量自動認證,已認證的文章將參與打字排名!
本文打字排名TOP20
- 1游客17925299... 07-14 17:21407 KPM
- 2游客15158900... 05-28 15:39377 KPM
- 3lin6625 10-15 06:24360 KPM
- 4全拼小王子 10-31 14:10334 KPM
- 5guagua_SAM... 01-25 09:53322 KPM
- 6游客14317680... 04-30 13:24321 KPM
- 7吾王比利 10-22 13:28315 KPM
- 8游客19775551... 07-10 15:00292 KPM
- 9游客14144220... 10-30 21:19272 KPM
- 10游客13169058... 09-26 13:55269 KPM
- 11paner 10-24 20:31269 KPM
- 12游客16672537... 03-08 22:41255 KPM
- 13Nicky 02-19 14:09253 KPM
- 14游客07180722 10-30 17:25251 KPM
- 1518岁帅气男高... 01-28 02:11248 KPM
- 16沐玥 06-15 00:09244 KPM
- 17万江三中陈曦 03-27 19:00229 KPM
- 183132 10-10 18:54228 KPM
- 19游客14063281... 10-22 14:10226 KPM
- 20游客14655145... 03-31 12:01225 KPM
用户更多文章推荐
- 英文乱序2022-02-02
- 2019年中国科学技术大学体育运动大会 竞...2020-02-19