def process_multi(img_path, model): image = common.read_imgfile(img_path, None, None) filename = os.path.split(img_path)[1].split('.')[0] scales = ast.literal_eval(node_or_string='[None]') humans = model.inference(image, scales=scales) image = model.draw_humans(image, humans, imgcopy=False) # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) path_save_2d = path = './results/' + filename + '_2d.jpg' cv2.imwrite(path_save_2d, image) # cv2.imshow('result', image) #cv2.waitKey() #poseLifting = Prob3dPose('/data/ai/JF/pose_estimation/multi_pose_estimator/lifting/models/prob_model_params.mat') poseLifting = Prob3dPose('./lifting/models/prob_model_params.mat') image_h, image_w = image.shape[:2] standard_w = 640 standard_h = 480 pose_2d_mpiis = [] visibilities = [] for human in humans: pose_2d_mpii, visibility = common.MPIIPart.from_coco(human) pose_2d_mpiis.append([(int(x * image_w + 0.5), int(y * image_h + 0.5)) for x, y in pose_2d_mpii]) visibilities.append(visibility) pose_2d_mpiis = np.array(pose_2d_mpiis) visibilities = np.array(visibilities) transformed_pose2d, weights = poseLifting.transform_joints( pose_2d_mpiis, visibilities) pose_3d = poseLifting.compute_3d(transformed_pose2d, weights) num = len(pose_3d) if num % 2 == 0: l = num // 2 else: l = num // 2 + 1 IMAGES_PATH = './results/' if not os.path.exists(IMAGES_PATH): os.makedirs(IMAGES_PATH) path_save_3d = './results/' + filename + '_3d.png' fig = plt.figure() for i, single_3d in enumerate(pose_3d): plot_pose(single_3d, i, l, fig, num) plt.savefig(path_save_3d) # plt.show() return path_save_2d, path_save_3d
def main(): parser = argparse.ArgumentParser(description='tf-pose-estimation run') parser.add_argument('--image', type=str, default='./images/p3.jpg') parser.add_argument( '--resolution', type=str, default='432x368', help='network input resolution. default=432x368') parser.add_argument( '--model', type=str, default='mobilenet_thin', help='cmu / mobilenet_thin') parser.add_argument( '--scales', type=str, default='[None]', help='for multiple scales, eg. [1.0, (1.1, 0.05)]') parser.add_argument( '--stick_only', action='store_true', help='save output without other analysis') parser.add_argument('--plot_3d', action='store_true', help='save 3d poses') args = parser.parse_args() scales = ast.literal_eval(args.scales) w, h = model_wh(args.resolution) e = TfPoseEstimator(get_graph_path(args.model), target_size=(w, h)) # estimate human poses from a single image ! image = common.read_imgfile(args.image, None, None) # image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21) t = time.time() humans = e.inference(image, scales=scales) elapsed = time.time() - t logger.info('inference image: %s in %.4f seconds.' % (args.image, elapsed)) image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False) # cv2.imshow('tf-pose-estimation result', image) # cv2.waitKey() import matplotlib.pyplot as plt fig = plt.figure() a = fig.add_subplot(2, 2, 1) a.set_title('Result') rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.stick_only: cv2.imwrite('output/test.png', image) logger.info('image saved: {}'.format('output/test.png')) import sys sys.exit(0) plt.imshow(rgb_image) bgimg = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB) bgimg = cv2.resize( bgimg, (e.heatMat.shape[1], e.heatMat.shape[0]), interpolation=cv2.INTER_AREA) # show network output a = fig.add_subplot(2, 2, 2) plt.imshow(bgimg, alpha=0.5) tmp = np.amax(e.heatMat[:, :, :-1], axis=2) plt.imshow(tmp, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() tmp2 = e.pafMat.transpose((2, 0, 1)) tmp2_odd = np.amax(np.absolute(tmp2[::2, :, :]), axis=0) tmp2_even = np.amax(np.absolute(tmp2[1::2, :, :]), axis=0) a = fig.add_subplot(2, 2, 3) a.set_title('Vectormap-x') # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5) plt.imshow(tmp2_odd, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() a = fig.add_subplot(2, 2, 4) a.set_title('Vectormap-y') # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5) plt.imshow(tmp2_even, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() if not os.path.exists('output'): os.mkdir('output') plt.savefig('output/test.png') logger.info('image saved: {}'.format('output/test.png')) if not args.plot_3d: import sys sys.exit(0) #For plotting in 3d logger.info('3d lifting initialization.') poseLifting = Prob3dPose('./src/lifting/models/prob_model_params.mat') image_h, image_w = image.shape[:2] standard_w = 640 standard_h = 480 pose_2d_mpiis = [] visibilities = [] for human in humans: pose_2d_mpii, visibility = common.MPIIPart.from_coco(human) pose_2d_mpiis.append([(int(x * standard_w + 0.5), int(y * standard_h + 0.5)) for x, y in pose_2d_mpii]) visibilities.append(visibility) pose_2d_mpiis = np.array(pose_2d_mpiis) visibilities = np.array(visibilities) transformed_pose2d, weights = poseLifting.transform_joints( pose_2d_mpiis, visibilities) pose_3d = poseLifting.compute_3d(transformed_pose2d, weights) for i, single_3d in enumerate(pose_3d): plot_pose(single_3d) plt.draw() plt.savefig('output/pose_3d_test.png') logger.info('3d plot saved: {}'.format('output/pose_3d_test.png'))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # show network output a = fig.add_subplot(2, 2, 2) # plt.imshow(CocoPose.get_bgimg(inp, target_size=(heatmap.shape[1], heatmap.shape[0])), alpha=0.5) tmp = np.amax(e.heatMat, axis=2) plt.imshow(tmp, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() tmp2 = e.pafMat.transpose((2, 0, 1)) tmp2_odd = np.amax(np.absolute(tmp2[::2, :, :]), axis=0) tmp2_even = np.amax(np.absolute(tmp2[1::2, :, :]), axis=0) a = fig.add_subplot(2, 2, 3) a.set_title('Vectormap-x') # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5) plt.imshow(tmp2_odd, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() a = fig.add_subplot(2, 2, 4) a.set_title('Vectormap-y') # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5) plt.imshow(tmp2_even, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() for i, single_3d in enumerate(pose_3d): plot_pose(single_3d) plt.show() pass
plt.colorbar() plt.show() import sys sys.exit(0) logger.info('3d lifting initialization.') poseLifting = Prob3dPose('./src/lifting/models/prob_model_params.mat') image_h, image_w = image.shape[:2] standard_w = 640 standard_h = 480 pose_2d_mpiis = [] visibilities = [] for human in humans: pose_2d_mpii, visibility = common.MPIIPart.from_coco(human) pose_2d_mpiis.append([(int(x * standard_w + 0.5), int(y * standard_h + 0.5)) for x, y in pose_2d_mpii]) visibilities.append(visibility) pose_2d_mpiis = np.array(pose_2d_mpiis) visibilities = np.array(visibilities) transformed_pose2d, weights = poseLifting.transform_joints(pose_2d_mpiis, visibilities) pose_3d = poseLifting.compute_3d(transformed_pose2d, weights) for i, single_3d in enumerate(pose_3d): plot_pose(single_3d) plt.show() pass
def adHocData(self): directory_in_str = sys.path[0] + "\\..\\images\\OurTest\\" # Delete old csv files try: os.remove(outputfile) os.remove(cleanedOutputfile) except OSError: pass # Run through every image in the folder for file in os.listdir(directory_in_str): filename = os.fsdecode(file) if filename.endswith(".jpg") or filename.endswith(".png"): fullpath = directory_in_str + filename # Estimate human poses from a single image ! image = common.read_imgfile(fullpath, None, None) # image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21) t = time.time() humans = self.e.inference(image, scales=self.scales) elapsed = time.time() - t logger.info('inference image: %s in %.4f seconds.' % (fullpath, elapsed)) image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False) # cv2.imshow('tf-pose-estimation result', image) # cv2.waitKey() myFile = open(outputfile, 'a') # myFile.write(str(filename) + ',') # print(filename) myFile.write('\n') myFile.close() # Attempt to plot our skeletons onto the image try: fig = plt.figure() a = fig.add_subplot(2, 2, 1) a.set_title('Result') plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) bgimg = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB) bgimg = cv2.resize( bgimg, (self.e.heatMat.shape[1], self.e.heatMat.shape[0]), interpolation=cv2.INTER_AREA) # show network output a = fig.add_subplot(2, 2, 2) plt.imshow(bgimg, alpha=0.5) tmp = np.amax(self.e.heatMat[:, :, :-1], axis=2) plt.imshow(tmp, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() tmp2 = self.e.pafMat.transpose((2, 0, 1)) tmp2_odd = np.amax(np.absolute(tmp2[::2, :, :]), axis=0) tmp2_even = np.amax(np.absolute(tmp2[1::2, :, :]), axis=0) a = fig.add_subplot(2, 2, 3) a.set_title('Vectormap-x') # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5) plt.imshow(tmp2_odd, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() a = fig.add_subplot(2, 2, 4) a.set_title('Vectormap-y') # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5) plt.imshow(tmp2_even, cmap=plt.cm.gray, alpha=0.5) plt.colorbar() plt.show() logger.info('3d lifting initialization.') poseLifting = Prob3dPose( sys.path[0] + '\\lifting\\models\\prob_model_params.mat') image_h, image_w = image.shape[:2] standard_w = 640 standard_h = 480 pose_2d_mpiis = [] visibilities = [] for human in humans: pose_2d_mpii, visibility = common.MPIIPart.from_coco( human) pose_2d_mpiis.append([(int(x * standard_w + 0.5), int(y * standard_h + 0.5)) for x, y in pose_2d_mpii]) visibilities.append(visibility) pose_2d_mpiis = np.array(pose_2d_mpiis) visibilities = np.array(visibilities) transformed_pose2d, weights = poseLifting.transform_joints( pose_2d_mpiis, visibilities) pose_3d = poseLifting.compute_3d(transformed_pose2d, weights) for i, single_3d in enumerate(pose_3d): plot_pose(single_3d) plt.show() except: print("Error when plotting image ") dataScriptGenerator.dataCleanup(self)
pose_3d = poseLifting.compute_3d(transformed_pose2d, weights) # print(pose_3d,'\n') num = len(pose_3d) if num % 2 == 0: l = num // 2 else: l = num // 2 + 1 VIDEOS_PATH = './results/3D_video/' if not os.path.exists(VIDEOS_PATH): os.makedirs(VIDEOS_PATH) fig = plt.figure() for i, single_3d in enumerate(pose_3d): # print('i:',i) # print('num:', num) plot_pose(single_3d, i, l, fig, num) # plt.show() plt.savefig('./results/3D_video/img_3d.png') img_3d = cv2.imread('./results/3D_video/img_3d.png') video_3d.write(img_3d) # plt.show() ######################################### if ret == False: break # if cv2.waitKey(1) == 27: # break video_3d.release() video.release() cv2.destroyAllWindows()