Example #1
0
    def preprocess_data(self, batch) :
        #in batch[0] are all the vidis. They are the same for each fpv elements
        #in batch[1] are all the frames. A group of fpv is one video

        fpv=self.nb_frames

        data_array = batch[0]
        num_videos = int(len(data_array)/fpv)
        x = np.zeros((num_videos, fpv,
                      self.crop_size[0], self.crop_size[1], self.nchannels),
                     dtype='float32')
        y = np.empty(num_videos, dtype='int64')
        for i in xrange(num_videos) :
            if self.translate_labels:
                y[i] = translate[batch[1][i*fpv]]
            else:
                y[i] = batch[1][i*fpv]
            do_flip = self.rng.rand(1)[0]
            bbox = self.crop()

            for j in xrange(fpv):
                data = data_array[i*fpv+j]
                #this data was stored in uint8
                data = StringIO(data.tostring())
                data.seek(0)
                img = Image.open(data)
                if (img.size[0] != self.input_size[1] and
                    img.size[1] != self.input_size[0]):
                    img = img.resize((int(self.input_size[1]),
                                      int(self.input_size[0])),
                                     Image.ANTIALIAS)
                img = img.crop(bbox)
                img = img.resize((int(self.crop_size[1]),
                                  int(self.crop_size[0])),
                                 Image.ANTIALIAS)
                # cv2.imshow('img', np.array(img))
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
                img = (np.array(img).astype(np.float32) / 255.0) * self.scale

                if self.nchannels == 1:
                    img = img[:, :, None]
                if self.swap_rgb and self.nchannels == 3:
                    img = img[:, :, [2, 1, 0]]
                x[i, j, :, :, :] = img[:, :, :]

                ### Flip
                if self.flip == 'flip' or (self.flip == 'random'
                                           and do_flip > 0.5):
                    new_image = np.empty_like(x[i, j, :, :, :])
                    for c in xrange(self.nchannels):
                        new_image[:,:,c] = np.fliplr(x[i, j, :, :, c])
                    x[i, j, :, :, :] = new_image

            #import cv2
            #cv2.imshow('img', x[i, 0, :, :, :])
            #cv2.waitKey(0)
            #cv2.destroyAllWindows()
        return (x, y)
Example #2
0
    def preprocess_data(self, batch):
        #in batch[0] are all the vidis. They are the same for each fpv elements
        #in batch[1] are all the frames. A group of fpv is one video

        fpv = self.nb_frames

        data_array = batch[0]
        num_videos = int(len(data_array) / fpv)
        x = np.zeros((num_videos, fpv, self.crop_size[0], self.crop_size[1],
                      self.nchannels),
                     dtype='float32')
        y = np.empty(num_videos, dtype='int64')
        for i in xrange(num_videos):
            if self.translate_labels:
                y[i] = translate[batch[1][i * fpv]]
            else:
                y[i] = batch[1][i * fpv]
            do_flip = self.rng.rand(1)[0]
            bbox = self.crop()

            for j in xrange(fpv):
                data = data_array[i * fpv + j]
                #this data was stored in uint8
                data = StringIO(data.tostring())
                data.seek(0)
                img = Image.open(data)
                if (img.size[0] != self.input_size[1]
                        and img.size[1] != self.input_size[0]):
                    img = img.resize(
                        (int(self.input_size[1]), int(self.input_size[0])),
                        Image.ANTIALIAS)
                img = img.crop(bbox)
                img = img.resize(
                    (int(self.crop_size[1]), int(self.crop_size[0])),
                    Image.ANTIALIAS)
                # cv2.imshow('img', np.array(img))
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
                img = (np.array(img).astype(np.float32) / 255.0) * self.scale

                if self.nchannels == 1:
                    img = img[:, :, None]
                if self.swap_rgb and self.nchannels == 3:
                    img = img[:, :, [2, 1, 0]]
                x[i, j, :, :, :] = img[:, :, :]

                ### Flip
                if self.flip == 'flip' or (self.flip == 'random'
                                           and do_flip > 0.5):
                    new_image = np.empty_like(x[i, j, :, :, :])
                    for c in xrange(self.nchannels):
                        new_image[:, :, c] = np.fliplr(x[i, j, :, :, c])
                    x[i, j, :, :, :] = new_image

            #import cv2
            #cv2.imshow('img', x[i, 0, :, :, :])
            #cv2.waitKey(0)
            #cv2.destroyAllWindows()
        return (x, y)