Ejemplo n.º 1
0
def loadBatchSurreal_fromString(file_string, image_size=128, num_frames=2, \
                                keypoints_num=24, bases_num=10, chamfer_scale=0.5):

    filename, t = file_string.split("#")
    output = dict()
    output[0] = read_syn_to_bin2(filename, int(t))
    #print "start!!@@@@@"
    scipy.io.savemat('tf_test1.mat', {'output': output[0]})
    output[1] = read_syn_to_bin2(filename, int(t) + 1)
    #output[0] = read_syn_to_bin(filename, int(t))
    #output[1] = read_syn_to_bin(filename, int(t) + 1)

    data_pose = np.zeros((num_frames, keypoints_num * 3))
    data_T = np.zeros((num_frames, 3))
    data_R = np.zeros((num_frames, 6))
    data_beta = np.zeros((num_frames, bases_num))
    data_J = np.zeros((num_frames, keypoints_num, 3))
    data_J_2d = np.zeros((num_frames, keypoints_num, 2))
    data_image = np.zeros((num_frames, image_size, image_size, 3))
    data_seg = np.zeros((num_frames, image_size, image_size))
    small_image_size = int(chamfer_scale * image_size)
    data_chamfer = np.zeros((num_frames, small_image_size, small_image_size))
    data_f = np.zeros((num_frames, 2))
    data_c = np.zeros((num_frames, 2))
    data_resize_scale = np.zeros((num_frames))
    data_gender = np.zeros(())
    # Cropping
    old_2d_center = output[0]['c']  #np.array([(320 - 1)/2.0, (240-1)/2.0])
    # Use keypoint 0 in frame1 as center
    J_2d = output[0]['J_2d']
    new_2d_center = np.round(J_2d[0, :]) + 0.5 * np.ones((2))
    s = 1.2  #1.3 + 0.1 * np.random.rand()
    crop_size = np.round(
        s * np.max(np.abs(J_2d - np.reshape(new_2d_center, [1, 1, -1]))))
    new_image_size = int(2 * crop_size)
    x_min = int(math.ceil(new_2d_center[0] - crop_size))
    x_max = int(math.floor(new_2d_center[0] + crop_size))
    y_min = int(math.ceil(new_2d_center[1] - crop_size))
    y_max = int(math.floor(new_2d_center[1] + crop_size))
    resize_scale = float(image_size) / (crop_size * 2.0)
    data_resize_scale[:] = resize_scale
    new_origin = np.array([x_min, y_min])
    data_c[:, :] = np.reshape(old_2d_center - new_origin, [-1, 2])

    for frame_id in range(num_frames):
        data_pose[frame_id, :] = output[frame_id]['pose']
        data_gender = int(output[frame_id]['gender'])
        data_R[frame_id, :3] = np.sin(output[frame_id]['R'])
        data_R[frame_id, 3:6] = np.cos(output[frame_id]['R'])
        #data_beta[frame_id, :] = output[frame_id]['beta']
        data_f[frame_id, :] = output[frame_id]['f']
        #data_c[frame_id, :] = output[frame_id]['c']
        data_T[frame_id, :] = output[frame_id]['T']
        data_J[frame_id, :, :] = output[frame_id]['J']
        data_J_2d[frame_id, :, :] = resize_scale * (
            output[frame_id]['J_2d'] - np.reshape(new_origin, [1, -1]))

        # crop image
        image = output[frame_id]['image']
        #print image
        h, w, _ = image.shape
        img_x_min = max(x_min, 0)
        img_x_max = min(x_max, w - 1)
        img_y_min = max(y_min, 0)
        img_y_max = min(y_max, h - 1)
        crop_image = np.zeros((new_image_size, new_image_size, 3),
                              dtype=np.float32)
        crop_image[max(0, -y_min):max(0, -y_min) + img_y_max - img_y_min + 1, \
                   max(0, -x_min):max(0, -x_min) + img_x_max - img_x_min + 1, :] \
                   = image[img_y_min:img_y_max + 1, img_x_min:img_x_max +1, :]
        data_image[frame_id, :, :, :] = scipy.misc.imresize(
            crop_image, [image_size, image_size])
        seg_float = output[frame_id]['seg'].astype(np.float32)
        crop_seg = np.zeros((new_image_size, new_image_size, 3),
                            dtype=np.float32)
        crop_seg[max(0, -y_min):max(0, -y_min) + img_y_max - img_y_min + 1, \
                 max(0, -x_min):max(0, -x_min) + img_x_max - img_x_min + 1] \
                 = np.expand_dims(seg_float[img_y_min:img_y_max + 1, \
                                  img_x_min:img_x_max +1], 2)
        seg = scipy.misc.imresize(crop_seg, [image_size, image_size])
        seg[seg < 0.5] = 0
        seg[seg >= 0.5] = 1

        #print np.max(output['seg'][sample_id, :, :])
        data_seg[frame_id, :, :] = seg[:, :, 0]
        data_chamfer[frame_id, :, :], _, _ = get_chamfer(
            seg[:, :, 0], chamfer_scale)


#  return data_pose, data_T, data_R, data_J, data_J_2d,\
#         data_f, data_c, data_gender
#print "resize",data_resize_scale*3.0
    return data_pose, data_T, data_R, data_beta, data_J, data_J_2d, data_image/255.0,\
           data_seg, data_f/3.0, data_chamfer, data_c/3.0, data_gender, data_resize_scale*3.0
Ejemplo n.º 2
0
    def next(self):
        if self.batch_id >= self.num_batches:
            self.reset()

        images_perm = self.perm[int(self.batch_id *
                                    self.batch_size):int((self.batch_id + 1) *
                                                         self.batch_size)]
        #batch_images = self.images[images_perm,:,:,:]
        batch_pose = np.zeros(
            (self.batch_size, self.num_frames, self.keypoints_num, 3),
            dtype=np.float32)
        batch_T = np.zeros((self.batch_size, self.num_frames, 3),
                           dtype=np.float32)
        batch_R = np.zeros((self.batch_size, self.num_frames, 6),
                           dtype=np.float32)
        batch_gender = np.zeros((self.batch_size), dtype=np.int32)
        batch_beta = np.zeros(
            (self.batch_size, self.num_frames, self.bases_num),
            dtype=np.float32)
        batch_J = np.zeros(
            (self.batch_size, self.num_frames, self.keypoints_num, 3),
            dtype=np.float32)
        batch_J_2d = np.zeros(
            (self.batch_size, self.num_frames, self.keypoints_num, 2),
            dtype=np.float32)
        batch_image = np.zeros(
            (self.batch_size, self.num_frames, self.h, self.w, 3),
            dtype=np.float32)
        batch_seg = np.zeros(
            (self.batch_size, self.num_frames, self.h, self.w),
            dtype=np.float32)
        batch_chamfer = np.zeros(
            (self.batch_size, self.num_frames, self.small_h, self.small_w),
            dtype=np.float32)
        batch_f = np.zeros((self.batch_size, self.num_frames, 2),
                           dtype=np.float32)
        batch_c = np.zeros((self.batch_size, self.num_frames, 2),
                           dtype=np.float32)
        batch_resize_scale = np.zeros((self.batch_size, self.num_frames),
                                      dtype=np.float32)

        old_2d_center = np.array([(320 - 1) / 2.0, (240 - 1) / 2.0])

        for i in range(self.batch_size):
            id_ = images_perm[i]
            #batch_labels_2d[i,:,0] = self.labels_2d[id_, 0:self.keypoints_num]
            #batch_labels_2d[i,:,1] = self.labels_2d[id_, self.keypoints_num:self.keypoints_num*2]
            batch_pose[i, :, :, :] = self.data_pose[id_, :, :, :]
            batch_gender[i] = self.data_gender[id_]
            batch_R[i, :, :3] = np.sin(self.data_R[id_, :, :])
            batch_R[i, :, 3:6] = np.cos(self.data_R[id_, :, :])
            batch_beta[i, :, :] = self.data_beta[id_, :, :]
            batch_f[i, :, :] = self.data_f[id_, :, :]
            batch_T[i, :, :] = self.data_T[id_, :, :]
            batch_J[i, :, :, :] = self.data_J[id_, :, :, :]

            J_2d = self.data_J_2d[id_, :, :, :]
            new_2d_center = np.round(J_2d[0, 0, :] + 10 * (np.random.uniform(
                (2)) - 1)) + 0.5 * np.ones((2))
            crop_size = np.round(
                1.2 *
                np.max(np.abs(J_2d - np.reshape(new_2d_center, [1, 1, -1]))))
            new_image_size = int(2 * crop_size)
            x_min = int(math.ceil(new_2d_center[0] - crop_size))
            x_max = int(math.floor(new_2d_center[0] + crop_size))
            y_min = int(math.ceil(new_2d_center[1] - crop_size))
            y_max = int(math.floor(new_2d_center[1] + crop_size))
            resize_scale = self.h / (crop_size * 2.0)
            batch_resize_scale[i, :] = resize_scale
            # frame_id * 2
            new_origin = np.array([x_min, y_min])
            batch_c[i, :, :] = np.reshape(old_2d_center - new_origin, [-1, 2])
            batch_J_2d[i, :, :, :] = resize_scale * (
                self.data_J_2d[id_, :, :, :] -
                np.reshape(new_origin, [1, 1, -1]))
            image = self.data_image[id_, :, :, :, :].astype(np.float32)
            seg_float = self.data_seg[id_, :, :, :].astype(np.float32)

            img_x_min = max(x_min, 0)
            img_x_max = min(x_max, 359)
            img_y_min = max(y_min, 0)
            img_y_max = min(y_max, 239)
            #print new_2d_center
            #print crop_size
            print(img_x_min, img_x_max, img_y_min, img_y_max)
            print(x_min, x_max, y_min, y_max)

            for frame_id in range(self.num_frames):

                crop_image = np.zeros((new_image_size, new_image_size, 3),
                                      dtype=np.float32)
                crop_image[max(0, -y_min):max(0, -y_min) + img_y_max - img_y_min + 1, \
                           max(0, -x_min):max(0, -x_min) + img_x_max - img_x_min + 1, :] \
                           = image[frame_id, img_y_min:img_y_max + 1, img_x_min:img_x_max +1, :]
                batch_image[i, frame_id, :, :, :] = scipy.misc.imresize(
                    crop_image, [self.h, self.w])
                crop_seg = np.zeros((new_image_size, new_image_size, 3),
                                    dtype=np.float32)
                crop_seg[max(0, -y_min):max(0, -y_min) + img_y_max - img_y_min + 1, \
                         max(0, -x_min):max(0, -x_min) + img_x_max - img_x_min + 1] \
                         = np.expand_dims(seg_float[frame_id, img_y_min:img_y_max + 1, \
                                          img_x_min:img_x_max +1], 2)
                seg = scipy.misc.imresize(crop_seg, [self.h, self.w])
                seg[seg < 0.5] = 0
                seg[seg >= 0.5] = 1

                batch_seg[i, frame_id, :, :] = seg[:, :, 0]

                # calculate chamfer dist
                #for img_id in range(self.num_frames):
                batch_chamfer[i, frame_id, :, :], _, _ = get_chamfer(
                    seg[:, :, 0], self.chamfer_scale)

        self.batch_id += 1
        return batch_pose, batch_T, batch_R, batch_beta, batch_J, batch_J_2d, batch_image/255.0,\
               batch_seg, batch_f, batch_chamfer, batch_c, batch_gender, batch_resize_scale
Ejemplo n.º 3
0
def loadBatchSurreal_fromString(file_string,
                                image_size=128,
                                num_frames=2,
                                keypoints_num=24,
                                bases_num=10,
                                chamfer_scale=0.5,
                                is_gait=False):
    filename, t = file_string.split("#")
    output = dict()
    output[0] = read_syn_to_bin(filename, int(t))
    output[1] = read_syn_to_bin(filename, int(t) + 1)

    data_pose = np.zeros((num_frames, keypoints_num * 3))
    data_T = np.zeros((num_frames, 3))
    data_R = np.zeros((num_frames, 6))
    data_beta = np.zeros((num_frames, bases_num))
    data_J = np.zeros((num_frames, keypoints_num, 3))
    data_J_2d = np.zeros((num_frames, keypoints_num, 2))
    data_J_reconstruct_2d = np.zeros((num_frames, keypoints_num, 2))
    data_image = np.zeros((num_frames, image_size, image_size, 3))
    data_seg = np.zeros((num_frames, image_size, image_size))
    small_image_size = int(chamfer_scale * image_size)
    data_chamfer = np.zeros((num_frames, small_image_size, small_image_size))
    data_f = np.zeros((num_frames, 2))
    data_c = np.zeros((num_frames, 2))
    data_resize_scale = np.zeros((num_frames))
    data_gender = np.zeros(())
    # Cropping
    w, h = (320, 180)
    old_2d_center = np.array([(w - 1) / 2.0, (h - 1) / 2.0])

    # Use keypoint 0 in frame1 as center
    J_2d = output[0]['J_2d']

    new_2d_center = np.round(J_2d[0, :] + 10 *
                             (np.random.uniform((2)) - 1)) + 0.5 * np.ones((2))
    s = 1.2  #+ 0.1 * np.random.rand()

    crop_size = np.round(
        s * np.max(np.abs(J_2d - np.reshape(new_2d_center, [1, 1, -1]))))
    new_image_size = int(2 * crop_size)
    x_min = int(math.ceil(new_2d_center[0] - crop_size))
    x_max = int(math.floor(new_2d_center[0] + crop_size))
    y_min = int(math.ceil(new_2d_center[1] - crop_size))
    y_max = int(math.floor(new_2d_center[1] + crop_size))
    resize_scale = float(image_size) / (crop_size * 2.0)
    data_resize_scale[:] = resize_scale
    new_origin = np.array([x_min, y_min])
    data_c[:, :] = np.reshape(old_2d_center - new_origin, [-1, 2])

    for frame_id in range(num_frames):
        data_pose[frame_id, :] = output[frame_id]['pose']
        data_gender = int(output[frame_id]['gender'])
        data_R[frame_id, :3] = np.sin(output[frame_id]['R'])
        data_R[frame_id, 3:6] = np.cos(output[frame_id]['R'])
        data_beta[frame_id, :] = output[frame_id]['beta']
        data_f[frame_id, :] = output[frame_id]['f']
        data_T[frame_id, :] = output[frame_id]['T']
        data_J[frame_id, :, :] = output[frame_id]['J']
        data_J_2d[frame_id, :, :] = resize_scale * (
            output[frame_id]['J_2d'] - np.reshape(new_origin, [1, -1]))
        data_J_reconstruct_2d[frame_id, :, :] = resize_scale * (
            output[frame_id]['J_reconstruct_2d'] -
            np.reshape(new_origin, [1, -1]))

        # crop image
        image = output[frame_id]['image']
        h, w, _ = image.shape
        img_x_min = max(x_min, 0)
        img_x_max = min(x_max, w - 1)
        img_y_min = max(y_min, 0)
        img_y_max = min(y_max, h - 1)

        crop_image = np.zeros((new_image_size, new_image_size, 3),
                              dtype=np.float32)
        crop_image[max(0, -y_min):max(0, -y_min) + img_y_max - img_y_min + 1, \
                   max(0, -x_min):max(0, -x_min) + img_x_max - img_x_min + 1, :] \
                   = image[img_y_min:img_y_max + 1, img_x_min:img_x_max +1, :]
        data_image[frame_id, :, :, :] = scipy.misc.imresize(
            crop_image, [image_size, image_size])
        #draw_2d_joints_surreal(data_image[frame_id, :, :, :], data_J_2d[frame_id, :, :].astype('int32'), name='/home/local/tmp/new/src'+str(t)+str(frame_id)+'.jpg')

        seg_float = output[frame_id]['seg'].astype(np.float32)
        crop_seg = np.zeros((new_image_size, new_image_size, 3),
                            dtype=np.float32)
        crop_seg[max(0, -y_min):max(0, -y_min) + img_y_max - img_y_min + 1, \
                 max(0, -x_min):max(0, -x_min) + img_x_max - img_x_min + 1] \
                 = np.expand_dims(seg_float[img_y_min:img_y_max + 1, img_x_min:img_x_max +1], 2)
        seg = scipy.misc.imresize(crop_seg, [image_size, image_size])
        seg[seg < 0.5] = 0
        seg[seg >= 0.5] = 1

        data_seg[frame_id, :, :] = seg[:, :, 0]
        data_chamfer[frame_id, :, :], _, _ = get_chamfer(
            seg[:, :, 0], chamfer_scale)
    return data_pose, data_T, data_R, data_beta, data_J, data_J_2d, data_J_reconstruct_2d, data_image / 255.0, data_seg, data_f, data_chamfer, data_c, data_gender, data_resize_scale