Ejemplo n.º 1
0
    def _parse_body(self, file):
        """
        parses the body of the file
        :param file: the file that has already had its header parsed
        """
        # initialize array to maintain the multibeam bathymetry data
        self.multibeam = np.empty((self.nrows, self.ncols))
        end_file = ''
        body_i = 0
        body_line = file.readline()

        # create animation to update user of parsing progress
        if self.animations:
            parse_animation = Animation(state='parsing file', size=self.nrows)

        # check to make sure that all rows contain correct number of elements
        while body_line != end_file:
            # check that number of columns is correct
            if len(body_line.split()) != self.ncols:
                raise FileFormatError('invalid number of cols')

            # store each new row of data in the array
            self.multibeam[body_i] = np.array(
                [self._parse_number(s) for s in body_line.split()])
            body_i += 1
            body_line = file.readline()

            # update the animation to indicate process status
            if self.animations:
                parse_animation.animate(body_i)

        # check that number of rows is correct
        if body_i != self.nrows:
            raise FileFormatError('invalid number of rows')
Ejemplo n.º 2
0
def constraints(anim, **kwargs):
    """
    Constraint list for Animation
    
    This constraint list can be used in the
    VerletParticle solver to constrain
    a animation global joint positions.
    
    Parameters
    ----------
    
    anim : Animation
        Input animation
        
    masses : (F, J) ndarray
        Optional list of masses
        for joints J across frames F
        defaults to weighting by
        vertical height
    
    Returns
    -------
    
    constraints : [(int, int, (F, J) ndarray, (F, J) ndarray, (F, J) ndarray)]
        A list of constraints in the format:
        (Joint1, Joint2, Masses1, Masses2, Lengths)
    
    """
    
    masses = kwargs.pop('masses', None)
    
    children = children_list(anim.parents)
    constraints = []

    points_offsets = Animation.offsets_global(anim)
    points = Animation.positions_global(anim)
    
    if masses is None:
        masses = 1.0 / (0.1 + np.absolute(points_offsets[:,1]))
        masses = masses[np.newaxis].repeat(len(anim), axis=0)
    
    for j in range(anim.shape[1]):
                
        """ Add constraints between all joints and their children """
        for c0 in children[j]:
            
            dists = np.sum((points[:, c0] - points[:, j])**2.0, axis=1)**0.5
            constraints.append((c0, j, masses[:,c0], masses[:,j], dists))
            
            """ Add constraints between all children of joint """
            for c1 in children[j]:
                if c0 == c1: continue

                dists = np.sum((points[:, c0] - points[:, c1])**2.0, axis=1)**0.5
                constraints.append((c0, c1, masses[:,c0], masses[:,c1], dists))  
    
    return constraints
Ejemplo n.º 3
0
 def __init__(self, cords: list):
     self.cords = cords
     self.events = Player.Events(self.go)
     self.animation_dict = {
         'go_forward': Animation('sheet.png', 4, 3)
     }
     self.animation = Animation('idle.png', 1, 1)
     self.move_buffer = [0, 0]
     self.obj_type = 'Player'
     self.speed = 70  # Points per second
     self.sprite = pygame.image.load('player_sprite.png').convert()
     self.sprite_size = self.sprite.get_rect().size
     self.has_opened_window = False
Ejemplo n.º 4
0
 def __init__(self, cords: list):
     self.cords = cords
     self.h = 50
     self.w = 50
     self.animation_dict = {
         'closed': Animation('', 1, 4)
     }
     self.events = Chest.Chest_Events(self.open_func(), self.close_func(),
                                      self.append, self.delete)
     self.len = 25
     self.content = [[None, None, None, None, None] * 5]
Ejemplo n.º 5
0
    def read_command(self):
        '''Pop a command from the brain memory and interpret it'''
        if self.brain:
            command = self.brain.pop()

            if command in (Command.FORWARD, Command.BACKWARD):
                self.state = TankState.MOVING
                self.offset_dt = FACING_TO_VEC[self.facing]

                self.driving_forward = (command is Command.FORWARD)

                end = self.world.tile_size[0]
                if self.facing in (Facing.DOWN, Facing.UP):
                    end = self.world.tile_size[1]

                self.animation = Animation(0, abs(end), 1.0)

            if command in (Facing.UP, Facing.DOWN, Facing.LEFT, Facing.RIGHT):
                self.state = TankState.TURNING
                self.turn_to = command  # bleh

                time_to_turn = 1.0
                if command in (Facing.UP, Facing.DOWN):
                    if self.facing in (Facing.LEFT, Facing.RIGHT):
                        time_to_turn = 0.5

                else:
                    if self.facing in (Facing.UP, Facing.DOWN):
                        time_to_turn = 0.5

                if command == self.facing:
                    time_to_turn = 0

                self.animation = Animation(0, time_to_turn, 1.0)

            if command is Command.SHOOT and self.bullet is None:
                #print self.color, 'is SHOOTING'
                self.state = TankState.SHOOTING
Ejemplo n.º 6
0
    def read_command(self):
        '''Pop a command from the brain memory and interpret it'''
        if self.brain:
            command = self.brain.pop()

            if command in (Command.FORWARD, Command.BACKWARD):
                self.state = TankState.MOVING
                self.offset_dt = FACING_TO_VEC[self.facing]

                self.driving_forward = (command is Command.FORWARD)

                end = self.world.tile_size[0]
                if self.facing in (Facing.DOWN, Facing.UP):
                    end = self.world.tile_size[1]

                self.animation = Animation(0, abs(end), 1.0)

            if command in (Facing.UP, Facing.DOWN, Facing.LEFT, Facing.RIGHT):
                self.state = TankState.TURNING
                self.turn_to = command # bleh

                time_to_turn = 1.0
                if command in (Facing.UP, Facing.DOWN):
                    if self.facing in (Facing.LEFT, Facing.RIGHT):
                        time_to_turn = 0.5

                else:
                    if self.facing in (Facing.UP, Facing.DOWN):
                        time_to_turn = 0.5

                if command == self.facing:
                    time_to_turn = 0

                self.animation = Animation(0, time_to_turn, 1.0)

            if command is Command.SHOOT and self.bullet is None:
                #print self.color, 'is SHOOTING'
                self.state = TankState.SHOOTING
Ejemplo n.º 7
0
def bvh_reproj(args):
    anim, names, frametime = BVH.load(args.bvh_path)
    positions = Animation.positions_global(anim)
    print(positions.shape)

    camera_data = load_camera(args.json_path)

    for cam_name in camera_data.keys():
        if args.multi_process:
            Process(target=bvh_reproj_for_cam,
                    args=(args, cam_name, camera_data, positions,
                          anim)).start()
        else:
            bvh_reproj_for_cam(args, cam_name, camera_data, positions, anim)
Ejemplo n.º 8
0
    def __init__(self, config, is_train=True):

        poses_3d_root, rotations, bones, alphas, contacts, projections = [], [], [], [], [], []
        self.frames = []
        self.config = config
        self.rotation_number = ROTATION_NUMBERS.get(config.arch.rotation_type)

        datasets = ['bvh']  #, 'bvh']
        if 'h36m' in datasets:
            dim_to_use_3d = h36m_utils.dimension_reducer(
                3, config.arch.predict_joints)
            subjects = h36m_utils.TRAIN_SUBJECTS if is_train else h36m_utils.TEST_SUBJECTS
            actions = h36m_utils.define_actions('All')
            self.cameras = h36m_utils.load_cameras(config.trainer.data_path)
            for subject in subjects:
                for action in actions:
                    for subaction in range(1, 3):
                        data_file = h5py.File(
                            '%s/S%s/%s-%s/annot.h5' %
                            (config.trainer.data_path, subject, action,
                             subaction), 'r')
                        data_size = data_file['frame'].size / 4
                        data_set = np.array(data_file['pose/3d']).reshape(
                            (-1, 96))[:, dim_to_use_3d]
                        for i in range(4):
                            camera_name = data_file['camera'][int(data_size *
                                                                  i)]
                            R, T, f, c, k, p, res_w, res_h = self.cameras[(
                                subject, str(camera_name))]
                            set_3d = data_set[int(data_size *
                                                  i):int(data_size *
                                                         (i + 1))].copy()
                            set_3d_world = h36m_utils.camera_to_world_frame(
                                set_3d.reshape((-1, 3)), R, T)
                            # set_3d_world[:, [1, 2]] = set_3d_world[:, [2, 1]]
                            # set_3d_world[:, [2]] *= -1
                            # set_3d_world = set_3d_world.reshape((-1, config.arch.predict_joints * 3))
                            set_3d_root = set_3d_world - np.tile(
                                set_3d_world[:, :3],
                                [1, int(set_3d_world.shape[-1] / 3)])

                            set_bones = self.get_bones(
                                set_3d_root, config.arch.predict_joints)
                            set_alphas = np.mean(set_bones, axis=1)

                            self.frames.append(set_3d_root.shape[0])
                            poses_3d_root.append(
                                set_3d_root /
                                np.expand_dims(set_alphas, axis=-1))
                            rotations.append(
                                np.zeros((set_3d_root.shape[0],
                                          int(set_3d_root.shape[1] / 3 *
                                              self.rotation_number))))
                            bones.append(set_bones /
                                         np.expand_dims(set_alphas, axis=-1))
                            alphas.append(set_alphas)
                            contacts.append(
                                self.get_contact(set_3d_world,
                                                 config.arch.predict_joints))
                            projections.append(
                                (set_3d_world.copy() /
                                 np.expand_dims(set_alphas, axis=-1)).reshape(
                                     (set_3d_world.shape[0], -1, 3))[:, 0, 2])

        if 'bvh' in datasets:
            to_keep = [
                0, 7, 8, 9, 2, 3, 4, 12, 15, 18, 19, 20, 25, 26, 27
            ] if config.arch.predict_joints == 15 else [
                0, 7, 8, 9, 2, 3, 4, 12, 13, 15, 16, 18, 19, 20, 25, 26, 27
            ]
            parents = [
                -1, 0, 1, 2, 0, 4, 5, 0, 7, 7, 9, 10, 7, 12, 13
            ] if config.arch.predict_joints == 15 else [
                -1, 0, 1, 2, 0, 4, 5, 0, 7, 8, 9, 8, 11, 12, 8, 14, 15
            ]

            bvh_files = util.make_dataset(['/mnt/dataset/test_bvh'],
                                          phase='bvh',
                                          data_split=1)
            bvh_files = bvh_files[:int(len(bvh_files) *
                                       0.8)] if is_train else bvh_files[
                                           int(len(bvh_files) * 0.8):]
            for bvh_file in bvh_files:
                original_anim, joint_names, frame_rate = BVH.load(bvh_file)
                set_skel_in = original_anim.positions[:, to_keep, :]
                set_rotations = original_anim.rotations.qs[:, to_keep, :]
                anim = Animation.Animation(
                    Quaternions(set_rotations), set_skel_in,
                    original_anim.orients.qs[to_keep, :], set_skel_in,
                    np.array(parents))
                set_3d_world = Animation.positions_global(anim).reshape(
                    set_rotations.shape[0], -1)
                set_3d_world[:, 0:3] = (set_3d_world[:, 3:6] +
                                        set_3d_world[:, 12:15]) / 2
                set_3d_root = set_3d_world - np.tile(
                    set_3d_world[:, :3],
                    [1, int(set_3d_world.shape[-1] / 3)])

                set_bones = self.get_bones(set_3d_root,
                                           config.arch.predict_joints)
                set_alphas = np.mean(set_bones, axis=1)

                self.frames.append(set_3d_root.shape[0])
                poses_3d_root.append(set_3d_root /
                                     np.expand_dims(set_alphas, axis=-1))
                rotations.append(
                    np.zeros((set_3d_root.shape[0],
                              int(set_3d_root.shape[1] / 3 *
                                  self.rotation_number))))
                bones.append(set_bones / np.expand_dims(set_alphas, axis=-1))
                alphas.append(set_alphas)
                contacts.append(
                    self.get_contact(set_3d_world, config.arch.predict_joints))
                projections.append(
                    (set_3d_world.copy() /
                     np.expand_dims(set_alphas, axis=-1)).reshape(
                         (set_3d_world.shape[0], -1, 3))[:, 0, 2])

        self.poses_3d = np.concatenate(poses_3d_root, axis=0)
        self.rotations = np.concatenate(rotations, axis=0)
        self.bones = np.concatenate(bones, axis=0)
        self.alphas = np.concatenate(alphas, axis=0)
        self.contacts = np.concatenate(contacts, axis=0)
        self.projections = np.concatenate(projections, axis=0)

        posed_3d_flip = self.get_flipping(self.poses_3d, 3,
                                          config.arch.predict_joints)
        if config.trainer.data_aug_flip and is_train:
            self.poses_3d = np.concatenate([self.poses_3d, posed_3d_flip],
                                           axis=0)

        self.poses_2d = self.get_projection(self.poses_3d)
        self.poses_2d_root = (self.poses_2d -
                              self.poses_2d[:, 0, None]).reshape(
                                  (self.poses_3d.shape[0], -1))

        import matplotlib.pyplot as plt
        import matplotlib.gridspec as gridspec
        from utils import visualization
        fig = plt.figure()
        gs = gridspec.GridSpec(1, 2)
        for i in range(1):
            ax1 = plt.subplot(gs[0], projection='3d')
            visualization.show3Dpose(self.poses_3d[i], ax1, radius=5)

            ax2 = plt.subplot(gs[1])
            visualization.show2Dpose(self.poses_2d_root[i] * 1000 + 500,
                                     ax2,
                                     radius=1000)

            fig.savefig('./images/2d_3d/_%d.png' % i)
            fig.clear()

        self.update_sequence_index()
Ejemplo n.º 9
0
class Tank:
    '''Draws and handles tank actions.'''
    def __init__(self, world, x, y, facing, color):
        self.set_position(x, y)
        self.facing = facing
        self.color = color

        self.offset_dt = (0, 0)
        self.offset = (0, 0)

        self.state = TankState.IDLE
        self.driving_forward = True
        self.animation = None
        self.brain = Brain(self)
        self.world = world
        self.bullet = None
        self.shots = 0

        # speed is per second
        self.speed = 100
        self.reduced_speed = self.speed * 0.5

        self.load_resources()

    def __str__(self):
        return "%s tank" % self.color

    def __repr__(self):
        return "Tank(%s)" % self.color

    def rect(self):
        '''Returns a Rect suitable for collision'''
        x, y = self.world.world_to_screen(self.x, self.y)
        x += self.offset[0]
        y += self.offset[1] - self.world.half_stack
        return Rect(x, y, self.world.tile_size[0], -self.world.tile_size[1])

    def get_facing(self):
        '''Returns the facing as Facing.LEFT, Facing.RIGHT, etc'''
        return self.facing

    def get_facing_vector(self):
        '''Returns the facing as (dx,dy)'''
        return FACING_TO_VEC[self.facing]

    def get_position(self):
        '''Returns position (x,y) as a tuple'''
        return (self.x, self.y)

    def get_warp_destination(self):
        '''Used in World to get the tank's warp coordinates.'''
        return (self.warp_x, self.warp_y)

    def set_position(self, x, y):
        self.x = x
        self.y = y
        self.warp_x = None
        self.warp_y = None

    def load_resources(self):
        def load(dir):
            pack = "tank"
            col = self.color
            return pyglet.resource.image('%s/%s_%s.png' % (pack, col, dir))

        self.up = load('up')
        self.down = load('down')
        self.left = load('left')
        self.right = load('right')

        # build images
        img = pyglet.resource.image('tank/bullet.png')
        img.anchor_x = img.width * 0.5
        img.anchor_y = img.height * 0.5

        # lookup table for bullet facing
        self.bullet_facing_img = {
            Facing.UP: img.get_transform(rotate=0),
            Facing.RIGHT: img.get_transform(rotate=90),
            Facing.DOWN: img.get_transform(rotate=180),
            Facing.LEFT: img.get_transform(rotate=270),
        }

        # lookup table for tank facing
        self.tank_facing_img = {
            Facing.UP: self.up,
            Facing.RIGHT: self.right,
            Facing.DOWN: self.down,
            Facing.LEFT: self.left,
        }

    def blit(self, x, y, z):
        x += self.offset[0]
        y += self.offset[1]
        self.tank_facing_img[self.facing].blit(x, y, z)
        #self.rect().debug_draw()

    def read_command(self):
        '''Pop a command from the brain memory and interpret it'''
        if self.brain:
            command = self.brain.pop()

            if command in (Command.FORWARD, Command.BACKWARD):
                self.state = TankState.MOVING
                self.offset_dt = FACING_TO_VEC[self.facing]

                self.driving_forward = (command is Command.FORWARD)

                end = self.world.tile_size[0]
                if self.facing in (Facing.DOWN, Facing.UP):
                    end = self.world.tile_size[1]

                self.animation = Animation(0, abs(end), 1.0)

            if command in (Facing.UP, Facing.DOWN, Facing.LEFT, Facing.RIGHT):
                self.state = TankState.TURNING
                self.turn_to = command  # bleh

                time_to_turn = 1.0
                if command in (Facing.UP, Facing.DOWN):
                    if self.facing in (Facing.LEFT, Facing.RIGHT):
                        time_to_turn = 0.5

                else:
                    if self.facing in (Facing.UP, Facing.DOWN):
                        time_to_turn = 0.5

                if command == self.facing:
                    time_to_turn = 0

                self.animation = Animation(0, time_to_turn, 1.0)

            if command is Command.SHOOT and self.bullet is None:
                #print self.color, 'is SHOOTING'
                self.state = TankState.SHOOTING

    def stop(self):
        '''Stop current state and return to idle.'''
        self.offset = (0, 0)
        self.animation = None
        self.state = TankState.IDLE

    def update(self, dt):
        '''Process tank state, with respect to time'''
        if self.animation:
            self.animation.update(dt)

        if self.state is TankState.IDLE:
            self.read_command()

        if self.state is TankState.TURNING:
            if self.animation.done:
                self.state = TankState.IDLE
                self.facing = self.turn_to
            return

        if self.state is TankState.SHOOTING:
            self.shots += 1
            self.bullet = Bullet(self)
            self.world.add_bullet(self.bullet)
            self.state = TankState.IDLE
            return

        if self.state is TankState.MOVING:
            o = self.offset
            dt = self.offset_dt
            anim = self.animation
            world = self.world

            sign = 1 if self.driving_forward else -1

            if anim.done:  # done moving, warp to final destination
                # set up a warp
                self.warp_x = self.x + dt[0] * sign
                self.warp_y = self.y + dt[1] * sign

                world.warp(self)
                self.set_position(self.warp_x, self.warp_y)
                self.stop()

            else:  # still moving
                target = world.get_tile(self.x + dt[0] * sign,
                                        self.y + dt[1] * sign)
                current = world.get_tile(self.x, self.y)

                # look for blocking items
                if target[0] in world.blocking_tiles or target[
                        1] in world.blocking_items:
                    self.stop()
                    print self.color, 'tried to drive into item, stopping'
                    return

                # don't move off map
                if target[0] is None:
                    self.stop()
                    print self.color, 'tried to drive off map, stopping'
                    return

                # don't collide with another tank
                if target[1] in world.tanks:
                    self.stop()
                    print self.color, 'tried to ram another tank, stopping'
                    return

                # move speed adjustment
                jitter = (0, 0)
                ri = random.randint
                progress = anim.unit()
                anim.speed = self.speed
                if current[0] is world.dirt and progress < 0.5:
                    anim.speed = self.reduced_speed
                    jitter = (ri(-1, 1), ri(0, 2))
                if target[0] is world.dirt and progress > 0.5:
                    anim.speed = self.reduced_speed
                    jitter = (ri(-1, 1), ri(0, 2))

                self.offset = (dt[0] * anim.value * sign + jitter[0],
                               -dt[1] * anim.value * sign + jitter[1])

    def is_idle(self):
        return self.state is TankState.IDLE

    def kill(self):
        print "BANG! %s is dead." % self.color

        self.state = TankState.DEAD

        if self.brain:
            self.brain.detach()
            self.world.detonate(self)
Ejemplo n.º 10
0
# coding=utf-8
from utils import maus, run, Animation

animation = Animation()

animation.add(maus.move, 30)
animation.add(maus.turn, 20)
run(animation) 
Ejemplo n.º 11
0
def main(config, args, output_folder):
    resume = args.resume
    name_list = [
        'Hips', 'RightUpLeg', 'RightLeg', 'RightFoot', 'LeftUpLeg', 'LeftLeg',
        'LeftFoot', 'Spine', 'Spine1', 'Neck', 'Head', 'LeftArm',
        'LeftForeArm', 'LeftHand', 'RightArm', 'RightForeArm', 'RightHand'
    ]
    model = getattr(models, config.arch.type)(config)

    checkpoint = torch.load(resume)
    state_dict = checkpoint['state_dict']
    model.load_state_dict(state_dict)

    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    model = model.to(device)
    model.eval()

    if args.input == 'h36m':
        test_data_loader = h36m_loader(config, is_training=False)
        test_parameters = [
            torch.from_numpy(np.array(item)).float().to(device)
            for item in test_data_loader.dataset.get_parameters()
        ]
        error_list = {}
        errors = []
        sampling_export = np.random.choice(test_data_loader.n_samples - 1,
                                           size=4,
                                           replace=False)
        for video_idx, datas in enumerate(test_data_loader):
            video_name = datas[-1][0]
            datas = [item.float().to(device) for item in datas[:-1]]
            poses_2d, poses_3d, bones, contacts, alphas, proj_facters = datas
            with torch.no_grad():
                pre_bones, pre_rotations, pre_rotations_full, pre_pose_3d, pre_c, pre_proj = model.forward_fk(
                    poses_2d, test_parameters)
            error = metric.mean_points_error(poses_3d,
                                             pre_pose_3d) * torch.mean(
                                                 alphas[0]).data.cpu().numpy()
            errors.append(error)
            action_name = video_name.split('_')[1].split(' ')[0]
            if action_name in error_list.keys():
                error_list[action_name].append(error)
            else:
                error_list[action_name] = [error]
            if video_idx in sampling_export:
                if config.arch.translation:
                    R, T, f, c, k, p, res_w, res_h = test_data_loader.dataset.cameras[
                        (int(video_name.split('_')[0].replace('S', '')),
                         int(video_name.split('_')[-1]))]
                    pose_2d_film = (poses_2d[0, :, :2].cpu().numpy() -
                                    c[:, 0]) / f[:, 0]
                    translations = np.ones(shape=(pose_2d_film.shape[0], 3))
                    translations[:, :2] = pose_2d_film
                    translation = (translations * np.repeat(
                        pre_proj[0].cpu().numpy(), 3, axis=-1).reshape(
                            (-1, 3))) * 5
                else:
                    translation = np.zeros((poses_2d.shape[1], 3))
                rotations = pre_rotations_full[0].cpu().numpy()
                length = (pre_bones * test_parameters[3].unsqueeze(0) +
                          test_parameters[2].repeat(bones.shape[0], 1,
                                                    1))[0].cpu().numpy()
                BVH.save('%s/%s.bvh' % (output_folder, video_name),
                         Animation.load_from_network(translation,
                                                     rotations,
                                                     length,
                                                     third_dimension=1),
                         names=name_list)
        error_file = '%s/errors.txt' % output_folder
        with open(error_file, 'w') as f:
            f.writelines('=====Action===== ==mm==\n')
            total = []
            for key in error_list.keys():
                mean_error = np.mean(np.array(error_list[key]))
                total.append(mean_error)
                print('%16s %.2f' % (key, mean_error))
                f.writelines('%16s %.2f \n' % (key, mean_error))
            print('%16s %.2f' % ('Average', np.mean(np.array(errors))))
            f.writelines('%16s %.2f \n' %
                         ('Average', np.mean(np.array(errors))))
            f.close()
    else:
        parameters = [
            torch.from_numpy(np.array(item)).float().to(device) for item in
            h36m_loader(config, is_training=True).dataset.get_parameters()
        ]

        def export(pose_folder):
            video_name = pose_folder.split('/')[-1]
            files = util.make_dataset([pose_folder],
                                      phase='json',
                                      data_split=1,
                                      sort=True,
                                      sort_index=0)
            IMAGE_WIDTH = 1080  # Should be changed refer to your test data
            pose_batch = []
            confidence_batch = []
            for pose_file_name in files:
                with open(pose_file_name, 'r') as f:
                    h36m_locations, h36m_confidence = h36m_utils.convert_openpose(
                        json.load(f))
                    pose_batch.append(h36m_locations)
                    confidence_batch.append(h36m_confidence)
            poses_2d = np.concatenate(pose_batch, axis=0) / IMAGE_WIDTH
            confidences = np.concatenate(confidence_batch, axis=0)
            poses_2d_root = (
                poses_2d -
                np.tile(poses_2d[:, :2], [1, int(poses_2d.shape[-1] / 2)]))
            if config.arch.confidence:
                poses_2d_root_c = np.zeros(
                    (poses_2d_root.shape[0],
                     int(poses_2d_root.shape[-1] / 2 * 3)))
                for joint_index in range(int(poses_2d_root.shape[-1] / 2)):
                    poses_2d_root_c[:, 3 *
                                    joint_index] = poses_2d_root[:, 2 *
                                                                 joint_index].copy(
                                                                 )
                    poses_2d_root_c[:, 3 * joint_index +
                                    1] = poses_2d_root[:, 2 * joint_index +
                                                       1].copy()
                    poses_2d_root_c[:, 3 * joint_index + 2] = np.array(
                        confidences)[:, joint_index].copy()
                poses_2d = poses_2d_root_c
            poses_2d = np.divide((poses_2d - parameters[0].cpu().numpy()),
                                 parameters[1].cpu().numpy())
            poses_2d = torch.from_numpy(
                np.array(poses_2d)).unsqueeze(0).float().to(device)
            with torch.no_grad():
                pre_bones, pre_rotations, pre_rotations_full, pre_pose_3d, pre_c, pre_proj = model.forward_fk(
                    poses_2d, parameters)
            if config.arch.translation:
                pose_2d_film = (poses_2d[0, :, :2].cpu().numpy() - 0.5)
                translations = np.ones(shape=(pose_2d_film.shape[0], 3))
                translations[:, :2] = pose_2d_film
                translation = (translations * np.repeat(
                    pre_proj[0].cpu().numpy(), 3, axis=-1).reshape(
                        (-1, 3))) * 3
                translation[:] -= translation[[0]]
            else:
                translation = np.zeros((poses_2d.shape[1], 3))
            rotations = pre_rotations_full[0].cpu().numpy()
            length = (pre_bones * parameters[3].unsqueeze(0) +
                      parameters[2].repeat(pre_bones.shape[0], 1,
                                           1))[0].cpu().numpy()
            BVH.save('%s/%s.bvh' % (output_folder, video_name),
                     Animation.load_from_network(translation,
                                                 rotations,
                                                 length,
                                                 third_dimension=1),
                     names=name_list)
            print('The bvh file of %s has been saved!' % video_name)

        export(args.input)
Ejemplo n.º 12
0
 def export(pose_folder):
     video_name = pose_folder.split('/')[-1]
     files = util.make_dataset([pose_folder],
                               phase='json',
                               data_split=1,
                               sort=True,
                               sort_index=0)
     IMAGE_WIDTH = 1080  # Should be changed refer to your test data
     pose_batch = []
     confidence_batch = []
     for pose_file_name in files:
         with open(pose_file_name, 'r') as f:
             h36m_locations, h36m_confidence = h36m_utils.convert_openpose(
                 json.load(f))
             pose_batch.append(h36m_locations)
             confidence_batch.append(h36m_confidence)
     poses_2d = np.concatenate(pose_batch, axis=0) / IMAGE_WIDTH
     confidences = np.concatenate(confidence_batch, axis=0)
     poses_2d_root = (
         poses_2d -
         np.tile(poses_2d[:, :2], [1, int(poses_2d.shape[-1] / 2)]))
     if config.arch.confidence:
         poses_2d_root_c = np.zeros(
             (poses_2d_root.shape[0],
              int(poses_2d_root.shape[-1] / 2 * 3)))
         for joint_index in range(int(poses_2d_root.shape[-1] / 2)):
             poses_2d_root_c[:, 3 *
                             joint_index] = poses_2d_root[:, 2 *
                                                          joint_index].copy(
                                                          )
             poses_2d_root_c[:, 3 * joint_index +
                             1] = poses_2d_root[:, 2 * joint_index +
                                                1].copy()
             poses_2d_root_c[:, 3 * joint_index + 2] = np.array(
                 confidences)[:, joint_index].copy()
         poses_2d = poses_2d_root_c
     poses_2d = np.divide((poses_2d - parameters[0].cpu().numpy()),
                          parameters[1].cpu().numpy())
     poses_2d = torch.from_numpy(
         np.array(poses_2d)).unsqueeze(0).float().to(device)
     with torch.no_grad():
         pre_bones, pre_rotations, pre_rotations_full, pre_pose_3d, pre_c, pre_proj = model.forward_fk(
             poses_2d, parameters)
     if config.arch.translation:
         pose_2d_film = (poses_2d[0, :, :2].cpu().numpy() - 0.5)
         translations = np.ones(shape=(pose_2d_film.shape[0], 3))
         translations[:, :2] = pose_2d_film
         translation = (translations * np.repeat(
             pre_proj[0].cpu().numpy(), 3, axis=-1).reshape(
                 (-1, 3))) * 3
         translation[:] -= translation[[0]]
     else:
         translation = np.zeros((poses_2d.shape[1], 3))
     rotations = pre_rotations_full[0].cpu().numpy()
     length = (pre_bones * parameters[3].unsqueeze(0) +
               parameters[2].repeat(pre_bones.shape[0], 1,
                                    1))[0].cpu().numpy()
     BVH.save('%s/%s.bvh' % (output_folder, video_name),
              Animation.load_from_network(translation,
                                          rotations,
                                          length,
                                          third_dimension=1),
              names=name_list)
     print('The bvh file of %s has been saved!' % video_name)
Ejemplo n.º 13
0
class Player:
    class Events:
        def __init__(self, go_func=None):
            self.go_events = list()
            self.events_set = set()
            self.reaction_dict = {
                'go': go_func
            }

        def clear(self):
            self.go_events = list()

    def __init__(self, cords: list):
        self.cords = cords
        self.events = Player.Events(self.go)
        self.animation_dict = {
            'go_forward': Animation('sheet.png', 4, 3)
        }
        self.animation = Animation('idle.png', 1, 1)
        self.move_buffer = [0, 0]
        self.obj_type = 'Player'
        self.speed = 70  # Points per second
        self.sprite = pygame.image.load('player_sprite.png').convert()
        self.sprite_size = self.sprite.get_rect().size
        self.has_opened_window = False

    def check_events(self, time_delta):
        for i in self.events.events_set:
            self.events.reaction_dict[i](time_delta)

        self.events.clear()

    def change_pos(self):
        self.cords[0] += self.move_buffer[0]
        self.cords[1] += self.move_buffer[1]
        self.move_buffer = [0, 0]

    def move(self, x, y):
        self.move_buffer[0] += x
        self.move_buffer[1] += y

    def go(self, time_delta):
        self.animation = self.animation_dict['go_forward']
        amount_of_pixels_to_move = self.speed / 1000 * time_delta
        x_move, y_move = 0, 0

        if 'forward' in self.events.go_events:
            y_move -= amount_of_pixels_to_move
        if 'backward' in self.events.go_events:
            y_move += amount_of_pixels_to_move
        if 'left' in self.events.go_events:
            x_move -= amount_of_pixels_to_move
        if 'right' in self.events.go_events:
            x_move += amount_of_pixels_to_move

        if x_move and y_move:
            x_move /= math.sqrt(2)
            y_move /= math.sqrt(2)

        self.move(x_move, y_move)

    def draw(self, time_delta):
        cur_image = self.animation.get_next_sprite(time_delta)
        image_cords = (self.cords[0] - self.sprite_size[0] // 2,
                       self.cords[1] - self.sprite_size[1] // 2)
        win.blit(cur_image, image_cords)

    def update(self, time_delta):
        self.check_events(time_delta)
        self.change_pos()
        self.draw(time_delta)
Ejemplo n.º 14
0
class Tank:
    '''Draws and handles tank actions.'''

    def __init__(self, world, x, y, facing, color):
        self.set_position(x,y)
        self.facing = facing
        self.color = color

        self.offset_dt = (0,0)
        self.offset = (0,0)

        self.state = TankState.IDLE
        self.driving_forward = True
        self.animation = None
        self.brain = Brain(self)
        self.world = world
        self.bullet = None
        self.shots = 0

        # speed is per second
        self.speed = 100
        self.reduced_speed = self.speed *0.5

        self.load_resources()

    def __str__(self):
        return "%s tank" % self.color

    def __repr__(self):
        return "Tank(%s)" % self.color

    def rect(self):
        '''Returns a Rect suitable for collision'''
        x,y = self.world.world_to_screen(self.x, self.y)
        x += self.offset[0]
        y += self.offset[1] - self.world.half_stack
        return Rect(x, y, self.world.tile_size[0], -self.world.tile_size[1])

    def get_facing(self):
        '''Returns the facing as Facing.LEFT, Facing.RIGHT, etc'''
        return self.facing

    def get_facing_vector(self):
        '''Returns the facing as (dx,dy)'''
        return FACING_TO_VEC[self.facing]

    def get_position(self):
        '''Returns position (x,y) as a tuple'''
        return (self.x, self.y)

    def get_warp_destination(self):
        '''Used in World to get the tank's warp coordinates.'''
        return (self.warp_x, self.warp_y)

    def set_position(self, x, y):
        self.x = x
        self.y = y
        self.warp_x = None
        self.warp_y = None

    def load_resources(self):

        def load(dir):
            pack = "tank"
            col = self.color
            return pyglet.resource.image('%s/%s_%s.png' % (pack, col, dir))

        self.up = load('up')
        self.down = load('down')
        self.left = load('left')
        self.right = load('right')

        # build images
        img = pyglet.resource.image('tank/bullet.png')
        img.anchor_x = img.width * 0.5
        img.anchor_y = img.height * 0.5

        # lookup table for bullet facing
        self.bullet_facing_img = {
            Facing.UP:    img.get_transform(rotate=0),
            Facing.RIGHT: img.get_transform(rotate=90),
            Facing.DOWN:  img.get_transform(rotate=180),
            Facing.LEFT:  img.get_transform(rotate=270),
        }

        # lookup table for tank facing
        self.tank_facing_img = {
            Facing.UP:    self.up,
            Facing.RIGHT: self.right,
            Facing.DOWN:  self.down,
            Facing.LEFT:  self.left,
        }

    def blit(self, x, y, z):
        x += self.offset[0]
        y += self.offset[1]
        self.tank_facing_img[self.facing].blit(x, y, z)
        #self.rect().debug_draw()

    def read_command(self):
        '''Pop a command from the brain memory and interpret it'''
        if self.brain:
            command = self.brain.pop()

            if command in (Command.FORWARD, Command.BACKWARD):
                self.state = TankState.MOVING
                self.offset_dt = FACING_TO_VEC[self.facing]

                self.driving_forward = (command is Command.FORWARD)

                end = self.world.tile_size[0]
                if self.facing in (Facing.DOWN, Facing.UP):
                    end = self.world.tile_size[1]

                self.animation = Animation(0, abs(end), 1.0)

            if command in (Facing.UP, Facing.DOWN, Facing.LEFT, Facing.RIGHT):
                self.state = TankState.TURNING
                self.turn_to = command # bleh

                time_to_turn = 1.0
                if command in (Facing.UP, Facing.DOWN):
                    if self.facing in (Facing.LEFT, Facing.RIGHT):
                        time_to_turn = 0.5

                else:
                    if self.facing in (Facing.UP, Facing.DOWN):
                        time_to_turn = 0.5

                if command == self.facing:
                    time_to_turn = 0

                self.animation = Animation(0, time_to_turn, 1.0)

            if command is Command.SHOOT and self.bullet is None:
                #print self.color, 'is SHOOTING'
                self.state = TankState.SHOOTING

    def stop(self):
        '''Stop current state and return to idle.'''
        self.offset = (0,0)
        self.animation = None
        self.state = TankState.IDLE

    def update(self, dt):
        '''Process tank state, with respect to time'''
        if self.animation:
            self.animation.update(dt)

        if self.state is TankState.IDLE:
            self.read_command()

        if self.state is TankState.TURNING:
            if self.animation.done:
                self.state = TankState.IDLE
                self.facing = self.turn_to
            return

        if self.state is TankState.SHOOTING:
            self.shots += 1
            self.bullet = Bullet(self)
            self.world.add_bullet(self.bullet)
            self.state = TankState.IDLE
            return

        if self.state is TankState.MOVING:
            o = self.offset
            dt = self.offset_dt
            anim = self.animation
            world = self.world

            sign = 1 if self.driving_forward else -1

            if anim.done: # done moving, warp to final destination
               # set up a warp
               self.warp_x = self.x + dt[0]*sign
               self.warp_y = self.y + dt[1]*sign

               world.warp(self)
               self.set_position(self.warp_x, self.warp_y)
               self.stop()

            else: # still moving
                target = world.get_tile(self.x+dt[0]*sign, self.y+dt[1]*sign)
                current = world.get_tile(self.x, self.y)

                # look for blocking items
                if target[0] in world.blocking_tiles or target[1] in world.blocking_items:
                    self.stop()
                    print self.color, 'tried to drive into item, stopping'
                    return

                # don't move off map
                if target[0] is None:
                    self.stop()
                    print self.color, 'tried to drive off map, stopping'
                    return

                # don't collide with another tank
                if target[1] in world.tanks:
                    self.stop()
                    print self.color, 'tried to ram another tank, stopping'
                    return

                # move speed adjustment
                jitter = (0,0)
                ri = random.randint
                progress = anim.unit()
                anim.speed = self.speed
                if current[0] is world.dirt and progress < 0.5:
                    anim.speed = self.reduced_speed
                    jitter = (ri(-1,1),ri(0,2))
                if target[0] is world.dirt and progress > 0.5:
                    anim.speed = self.reduced_speed
                    jitter = (ri(-1,1),ri(0,2))

                self.offset = (dt[0]*anim.value*sign+jitter[0],
                               -dt[1]*anim.value*sign+jitter[1])


    def is_idle(self):
        return self.state is TankState.IDLE

    def kill(self):
        print "BANG! %s is dead." % self.color

        self.state = TankState.DEAD

        if self.brain:
            self.brain.detach()
            self.world.detonate(self)