demo_func.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import cv2
  2. import matplotlib.pyplot as plt
  3. import copy
  4. import numpy as np
  5. from PIL import Image
  6. import os
  7. from src import util
  8. from src.body import Body
  9. # from src.infer_video_d2 import infer_image,load_predictor
  10. from video_pose.inference.infer_video_d2 import run_3d
  11. from video_pose.data.prepare_data_2d_custom import custom
  12. from video_pose.run import run_video_reconstruction
  13. def run_3d_for_video(original_video_path):
  14. run_3d(original_video_path)
  15. video_analyzed_path = original_video_path + ".npz"
  16. print(video_analyzed_path)
  17. custom(video_analyzed_path)
  18. compressed_analyzed_path = video_analyzed_path.replace(".npz", "_compressed.npz")
  19. run_video_reconstruction(original_video_path, compressed_analyzed_path)
  20. reconstructed_video_path = original_video_path.replace(".mp4", "_reconstructed.mp4")
  21. print(reconstructed_video_path)
  22. # return reconstructed_video_path
  23. def run_openpose_for_image_front(index):
  24. PNG_OR_NOT = True
  25. # hand_estimation = Hand('model/hand_pose_model.pth')
  26. test_image = 'pose_source_images/{}-0.png'.format(index)
  27. if os.path.exists(test_image) is False:
  28. PNG_OR_NOT = False
  29. test_image = 'pose_source_images/{}-0.jpg'.format(index)
  30. oriImg = cv2.imread(test_image) # B,G,R order
  31. im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  32. # --- for openpose ---
  33. body_estimation = Body('model/body_pose_model.pth')
  34. candidate, subset = body_estimation(oriImg)
  35. # --- for detectron ---
  36. # predictor = load_predictor()
  37. # candidate, subset = infer_image(oriImg, predictor)
  38. np.save('pose_temp_data/{}-0.npy'.format(index), candidate)
  39. canvas = copy.deepcopy(oriImg)
  40. canvas = util.draw_bodypose(canvas, candidate, subset)
  41. # sssssssssssss
  42. if PNG_OR_NOT == True:
  43. alpha_value_new = np.reshape(im[:, :, 3], (im.shape[0], im.shape[1], 1))
  44. canvas = np.c_[canvas, alpha_value_new]
  45. # test = np.c_[im[:,:,0:3],alpha_value_new]
  46. # print(test)
  47. # print(im)
  48. # cv2.imwrite('pose_processed_images/tmp_transparent_ori.png', oriImg, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  49. cv2.imwrite('pose_processed_images/{}-0-result.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  50. # cv2.imwrite('pose_processed_images/tmp_transparent_test.png', test, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  51. # cv2.imwrite('pose_processed_images/tmp_transparent_im.png', im, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  52. # plt.imshow(canvas[:, :, [2, 1, 0]])
  53. # plt.axis('off')
  54. # plt.savefig('pose_processed_images/{}-1-result.png'.format(index))
  55. # plt.show()
  56. def run_openpose_for_image_side(index):
  57. PNG_OR_NOT = True
  58. body_estimation = Body('model/body_pose_model.pth')
  59. # hand_estimation = Hand('model/hand_pose_model.pth')
  60. test_image = 'pose_source_images/{}-1.png'.format(index)
  61. if os.path.exists(test_image) is False:
  62. PNG_OR_NOT = False
  63. test_image = 'pose_source_images/{}-1.jpg'.format(index)
  64. oriImg = cv2.imread(test_image) # B,G,R order
  65. im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  66. # --- for openpose ---
  67. body_estimation = Body('model/body_pose_model.pth')
  68. candidate, subset = body_estimation(oriImg)
  69. # --- for detectron ---
  70. # predictor = load_predictor()
  71. # candidate, subset = infer_image(oriImg, predictor)
  72. np.save('pose_temp_data/{}-1.npy'.format(index), candidate)
  73. canvas = copy.deepcopy(oriImg)
  74. canvas = util.draw_bodypose(canvas, candidate, subset)
  75. if PNG_OR_NOT == True:
  76. alpha_value_new = np.reshape(im[:, :, 3], (im.shape[0], im.shape[1], 1))
  77. canvas = np.c_[canvas, alpha_value_new]
  78. # test = np.c_[im[:,:,0:3],alpha_value_new]
  79. # print(test)
  80. # print(im)
  81. # cv2.imwrite('pose_processed_images/tmp_transparent_ori.png', oriImg, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  82. cv2.imwrite('pose_processed_images/{}-1-result.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  83. # cv2.imwrite('pose_processed_images/tmp_transparent_test.png', test, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  84. # cv2.imwrite('pose_processed_images/tmp_transparent_im.png', im, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  85. # plt.imshow(canvas[:, :, [2, 1, 0]])
  86. # plt.axis('off')
  87. # plt.savefig('pose_processed_images/{}-1-result.png'.format(index))
  88. # plt.show()
  89. def run_openpose_for_normal(index,name):
  90. print(index)
  91. # 输入index和原来一样,就是序号,你可以更改一下输入或者输出,方便你们处理
  92. # 判断是否是PNG格式,如果是,增添对alpha通道的处理
  93. PNG_OR_NOT = True
  94. # 调用的模型路径,无需更改
  95. body_estimation = Body('model/body_pose_model.pth')
  96. #原图像的位置,需要更改 !!
  97. p='./capture_image/'+name
  98. test_image = p+'/capture_image{}.png'.format(index)
  99. if os.path.exists(test_image) is False:
  100. PNG_OR_NOT = False
  101. # 原图像的位置,需要更改 !!
  102. test_image = p+'/capture_image{}.jpg'.format(index)
  103. oriImg = cv2.imread(test_image) # B,G,R order
  104. im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  105. candidate, subset = body_estimation(oriImg)
  106. # 坐标点数值保存路径,需要更改!!
  107. np.save(p+'/capture_image{}-1.png'.format(index), candidate)
  108. print(p+'/capture_image{}.png'.format(index))
  109. # 处理
  110. canvas = copy.deepcopy(oriImg)
  111. canvas = util.draw_bodypose(canvas, candidate, subset)
  112. #if PNG_OR_NOT == True:
  113. #alpha_value_new = np.reshape(im[:,:,3],(im.shape[0],im.shape[1],1))
  114. #canvas = np.c_[canvas, alpha_value_new]
  115. # 结果图片的保存路径,需要更改 !!
  116. cv2.imwrite(p+'/capture_image_result{}.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  117. if __name__ == '__main__':
  118. # run_openpose_for_normal(1)
  119. input_video = "video/15.mp4"
  120. run_3d_for_video(input_video)