demo_func.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 src1.torch_openpose import torch_openpose
  10. import src1
  11. # from src.infer_video_d2 import infer_image,load_predictor
  12. from video_pose.inference.infer_video_d2 import run_3d
  13. from video_pose.data.prepare_data_2d_custom import custom
  14. from video_pose.run import run_video_reconstruction
  15. def run_3d_for_video(original_video_path):
  16. run_3d(original_video_path)
  17. video_analyzed_path = original_video_path + ".npz"
  18. print(video_analyzed_path)
  19. custom(video_analyzed_path)
  20. compressed_analyzed_path = video_analyzed_path.replace(".npz", "_compressed.npz")
  21. run_video_reconstruction(original_video_path, compressed_analyzed_path)
  22. reconstructed_video_path = original_video_path.replace(".mp4", "_reconstructed.mp4")
  23. print(reconstructed_video_path)
  24. # return reconstructed_video_path
  25. def run_openpose_for_image_front(index):
  26. PNG_OR_NOT = True
  27. # hand_estimation = Hand('model/hand_pose_model.pth')
  28. test_image = 'pose_source_images/{}-0.png'.format(index)
  29. if os.path.exists(test_image) is False:
  30. PNG_OR_NOT = False
  31. test_image = 'pose_source_images/{}-0.jpg'.format(index)
  32. oriImg = cv2.imread(test_image) # B,G,R order
  33. im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  34. # --- for openpose ---
  35. # body_estimation = Body('model/body_pose_model.pth')
  36. tp = torch_openpose('body_25')
  37. poses = tp(oriImg)
  38. np.save('pose_temp_data/{}-0.npy'.format(index), poses)
  39. canvas = copy.deepcopy(oriImg)
  40. canvas = src1.util.draw_bodypose(canvas, poses, 'body_25')
  41. if PNG_OR_NOT == True:
  42. alpha_value_new = np.reshape(im[:, :, 3], (im.shape[0], im.shape[1], 1))
  43. canvas = np.c_[canvas, alpha_value_new]
  44. cv2.imwrite('pose_processed_images/{}-0-result.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  45. # def run_openpose_for_image_front(index):
  46. # PNG_OR_NOT = True
  47. # # hand_estimation = Hand('model/hand_pose_model.pth')
  48. # test_image = 'pose_source_images/{}-0.png'.format(index)
  49. # if os.path.exists(test_image) is False:
  50. # PNG_OR_NOT = False
  51. # test_image = 'pose_source_images/{}-0.jpg'.format(index)
  52. # oriImg = cv2.imread(test_image) # B,G,R order
  53. # im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  54. #
  55. # # --- for openpose ---
  56. # body_estimation = Body('model/body_pose_model.pth')
  57. # candidate, subset = body_estimation(oriImg)
  58. # print(candidate)
  59. # print("-----")
  60. # print(subset)
  61. #
  62. # # --- for detectron ---
  63. # # predictor = load_predictor()
  64. # # candidate, subset = infer_image(oriImg, predictor)
  65. # np.save('pose_temp_data/{}-0.npy'.format(index), candidate)
  66. #
  67. # canvas = copy.deepcopy(oriImg)
  68. # canvas = util.draw_bodypose(canvas, candidate, subset)
  69. #
  70. # # sssssssssssss
  71. # if PNG_OR_NOT == True:
  72. # alpha_value_new = np.reshape(im[:, :, 3], (im.shape[0], im.shape[1], 1))
  73. # canvas = np.c_[canvas, alpha_value_new]
  74. #
  75. # # test = np.c_[im[:,:,0:3],alpha_value_new]
  76. # # print(test)
  77. # # print(im)
  78. #
  79. # # cv2.imwrite('pose_processed_images/tmp_transparent_ori.png', oriImg, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  80. # cv2.imwrite('pose_processed_images/{}-0-result.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  81. # # cv2.imwrite('pose_processed_images/tmp_transparent_test.png', test, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  82. # # cv2.imwrite('pose_processed_images/tmp_transparent_im.png', im, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  83. #
  84. # # plt.imshow(canvas[:, :, [2, 1, 0]])
  85. # # plt.axis('off')
  86. # # plt.savefig('pose_processed_images/{}-1-result.png'.format(index))
  87. # # plt.show()
  88. def run_openpose_for_image_side(index):
  89. PNG_OR_NOT = True
  90. # hand_estimation = Hand('model/hand_pose_model.pth')
  91. test_image = 'pose_source_images/{}-1.png'.format(index)
  92. if os.path.exists(test_image) is False:
  93. PNG_OR_NOT = False
  94. test_image = 'pose_source_images/{}-1.jpg'.format(index)
  95. oriImg = cv2.imread(test_image) # B,G,R order
  96. im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  97. # --- for openpose ---
  98. # body_estimation = Body('model/body_pose_model.pth')
  99. tp = torch_openpose('body_25')
  100. print(tp)
  101. poses = tp(oriImg)
  102. np.save('pose_temp_data/{}-1.npy'.format(index), poses)
  103. canvas = copy.deepcopy(oriImg)
  104. canvas = src1.util.draw_bodypose(canvas, poses, 'body_25')
  105. if PNG_OR_NOT == True:
  106. alpha_value_new = np.reshape(im[:, :, 3], (im.shape[0], im.shape[1], 1))
  107. canvas = np.c_[canvas, alpha_value_new]
  108. cv2.imwrite('pose_processed_images/{}-1-result.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  109. # def run_openpose_for_image_side(index):
  110. # PNG_OR_NOT = True
  111. # body_estimation = Body('model/body_pose_model.pth')
  112. # # hand_estimation = Hand('model/hand_pose_model.pth')
  113. # test_image = 'pose_source_images/{}-1.png'.format(index)
  114. # if os.path.exists(test_image) is False:
  115. # PNG_OR_NOT = False
  116. # test_image = 'pose_source_images/{}-1.jpg'.format(index)
  117. # oriImg = cv2.imread(test_image) # B,G,R order
  118. # im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  119. # # --- for openpose ---
  120. # body_estimation = Body('model/body_pose_model.pth')
  121. # candidate, subset = body_estimation(oriImg)
  122. #
  123. # # --- for detectron ---
  124. # # predictor = load_predictor()
  125. # # candidate, subset = infer_image(oriImg, predictor)
  126. # np.save('pose_temp_data/{}-1.npy'.format(index), candidate)
  127. #
  128. # canvas = copy.deepcopy(oriImg)
  129. # canvas = util.draw_bodypose(canvas, candidate, subset)
  130. #
  131. # if PNG_OR_NOT == True:
  132. # alpha_value_new = np.reshape(im[:, :, 3], (im.shape[0], im.shape[1], 1))
  133. # canvas = np.c_[canvas, alpha_value_new]
  134. # # test = np.c_[im[:,:,0:3],alpha_value_new]
  135. # # print(test)
  136. # # print(im)
  137. #
  138. # # cv2.imwrite('pose_processed_images/tmp_transparent_ori.png', oriImg, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  139. # cv2.imwrite('pose_processed_images/{}-1-result.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  140. # # cv2.imwrite('pose_processed_images/tmp_transparent_test.png', test, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  141. # # cv2.imwrite('pose_processed_images/tmp_transparent_im.png', im, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  142. #
  143. # # plt.imshow(canvas[:, :, [2, 1, 0]])
  144. # # plt.axis('off')
  145. # # plt.savefig('pose_processed_images/{}-1-result.png'.format(index))
  146. # # plt.show()
  147. def run_openpose_for_normal(index,name):
  148. PNG_OR_NOT = True
  149. tp = torch_openpose('body_25')
  150. p = './capture_image/' + name
  151. test_image = p + '/capture_image{}.png'.format(index)
  152. if os.path.exists(test_image) is False:
  153. PNG_OR_NOT = False
  154. # 原图像的位置,需要更改 !!
  155. test_image = p + '/capture_image{}.jpg'.format(index)
  156. oriImg = cv2.imread(test_image) # B,G,R order
  157. im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  158. poses = tp(oriImg)
  159. np.save(p + '/capture_image{}-1.png'.format(index), poses)
  160. np.save(p + '/capture_image{}-1.png.npy'.format(index), poses)
  161. print(p + '/capture_image{}.png'.format(index))
  162. canvas = copy.deepcopy(oriImg)
  163. canvas = src1.util.draw_bodypose(canvas, poses, 'body_25')
  164. cv2.imwrite(p + '/capture_image_result{}.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  165. # data = np.load(p + '/capture_image{}-1.png.npy'.format(index))
  166. # data = np.load(p + '/capture_image{}-1.png.npy'.format(index))
  167. # print("25----")
  168. # print(data)
  169. #
  170. # plt.imshow(canvas[:, :, [2, 1, 0]])
  171. # plt.axis('off')
  172. # plt.savefig(p + '/capture_image_result{}.png'.format(index))
  173. # plt.show()
  174. # def run_openpose_for_normal1(index,name):
  175. # print(index)
  176. #
  177. # # 输入index和原来一样,就是序号,你可以更改一下输入或者输出,方便你们处理
  178. #
  179. # # 判断是否是PNG格式,如果是,增添对alpha通道的处理
  180. # PNG_OR_NOT = True
  181. # # 调用的模型路径,无需更改
  182. # print('body_model')
  183. # body_estimation = Body('model/body_pose_model.pth')
  184. # #原图像的位置,需要更改 !!
  185. # p='./capture_image/'+name
  186. # test_image = p+'/capture_image{}.png'.format(index)
  187. # if os.path.exists(test_image) is False:
  188. # PNG_OR_NOT = False
  189. # # 原图像的位置,需要更改 !!
  190. # test_image = p+'/capture_image{}.jpg'.format(index)
  191. # oriImg = cv2.imread(test_image) # B,G,R order
  192. # im = cv2.imread(test_image, cv2.IMREAD_UNCHANGED)
  193. #
  194. # candidate, subset = body_estimation(oriImg)
  195. # # 坐标点数值保存路径,需要更改!!
  196. # np.save(p+'/capture_image{}-1.png'.format(index), candidate)
  197. # print(p+'/capture_image{}.png'.format(index))
  198. #
  199. # # 处理
  200. # canvas = copy.deepcopy(oriImg)
  201. # canvas = util.draw_bodypose(canvas, candidate, subset)
  202. # #if PNG_OR_NOT == True:
  203. # #alpha_value_new = np.reshape(im[:,:,3],(im.shape[0],im.shape[1],1))
  204. # #canvas = np.c_[canvas, alpha_value_new]
  205. #
  206. # # 结果图片的保存路径,需要更改 !!
  207. # cv2.imwrite(p+'/capture_image_result{}.png'.format(index), canvas, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
  208. # data = np.load(p + '/capture_image{}-1.png.npy'.format(index))
  209. # print("18----")
  210. # print(data)
  211. #
  212. # plt.imshow(canvas[:, :, [2, 1, 0]])
  213. # plt.axis('off')
  214. # plt.savefig(p + '/capture_image_result{}.png'.format(index))
  215. # plt.show()
  216. if __name__ == '__main__':
  217. ## run_openpose_for_normal(1)
  218. # input_video = "video/15.mp4"
  219. # run_3d_for_video(input_video)
  220. run_openpose_for_normal(2,"31")