Exemple #1
0
    def _py_func_vp(rgb_path, flow_path, label):

        rgb_path = rgb_path.decode ()
        flow_path = flow_path.decode()

        rgb_cap = cv2.VideoCapture(rgb_path)
        flow_cap = cv2.VideoCapture(flow_path)

        flow_len = flow_cap.get(cv2.CAP_PROP_FRAME_COUNT)
        rgb_len = rgb_cap.get(cv2.CAP_PROP_FRAME_COUNT)

        index = np.random.randint(0,min(rgb_len,flow_len) - 10)

        
        rgb_cap.set(cv2.CAP_PROP_POS_FRAMES,index)
        rgb_file = None
        while rgb_file is None:
            _ , image = rgb_cap.read()
            if image is not None:
                image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
                image = np.float32(image)
                image = cv2.resize(image,(340,256))
                rgb_file = DataAugmentation.Multiscale_crop(image)
                rgb_file = DataAugmentation.horizontal_flip(rgb_file)
                rgb_file = cv2.resize(rgb_file,(224,224))
                rgb_file = normalize(rgb_file)
                break

        flow_cap.set(cv2.CAP_PROP_FRAME_COUNT,index)
        image_list = []
        for i in range(10):
                f , img = flow_cap.read()
                if img is not None:
                    image_list.append(img[...,:2])
        
        image = np.concatenate(image_list,axis=-1)
        image = np.float32(image)
        image = cv2.resize(image,(340,256))
        flow_file = DataAugmentation.Multiscale_crop(image,is_flow=True)
        flow_file = DataAugmentation.horizontal_flip(flow_file,is_flow=True)
        flow_file = cv2.resize(flow_file,(224,224))
        flow_file = (flow_file / 255) * 2 - 1


        one_hot_label = np.zeros (101, dtype=np.float32)
        one_hot_label[label] = 1

        return rgb_file, flow_file, one_hot_label
Exemple #2
0
    def _py_func_rgb_vp(rgb_path, label):

        rgb_path = rgb_path.decode ()

        rgb_file = os.listdir (rgb_path)
        rgb_file = sorted(rgb_file)
        try:
            random_file = random.choice(rgb_file)
            rgb_file_path = os.path.join(rgb_path,random_file)
            img = cv2.imread(rgb_file_path)
            img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        except:
            raise ValueError('rgb_file_path',rgb_file_path,'cannot be read')

        img = cv2.resize(img,(340,256))
        img = DataAugmentation.Multiscale_crop(img)
        img = DataAugmentation.horizontal_flip(img)
        img = cv2.resize(img,(224,224))
        img = np.float32(img)
        img = normalize(img)

        assert img.shape == (224,224,3)

        if label is not None:
            one_hot_label = np.zeros (101, dtype=np.float32)
            one_hot_label[label] = 1

            return img, one_hot_label

        return img
Exemple #3
0
    def _py_func_rgb_vp(rgb_path, label):

        rgb_path = rgb_path.decode()

        rgb_cap = cv2.VideoCapture(rgb_path)
        rgb_len = rgb_cap.get(cv2.CAP_PROP_FRAME_COUNT)

        while 1:
            index = np.random.randint(0, rgb_len)
            rgb_cap.set(cv2.CAP_PROP_POS_FRAMES, index)
            _, image = rgb_cap.read()
            if image is not None:
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = np.float32(image)
                image = cv2.resize(image, (340, 256))
                rgb_file = DataAugmentation.Multiscale_crop(image)
                rgb_file = DataAugmentation.horizontal_flip(rgb_file)
                rgb_file = cv2.resize(rgb_file, (224, 224))
                if _preprocess_name == 'pytorch':
                    rgb_file = normalize(rgb_file)
                elif _preprocess_name == 'tf':
                    rgb_file = tf_preprocess(rgb_file)
                break

        rgb_cap.release()
        if label is not None:
            one_hot_label = np.zeros(101, dtype=np.float32)
            one_hot_label[label] = 1

            return rgb_file, one_hot_label

        return rgb_file
Exemple #4
0
    def _py_func_flow_vp(flow_path, label):

        flow_path = flow_path.decode ()

        flow_cap = cv2.VideoCapture (flow_path)
        flow_len = flow_cap.get (cv2.CAP_PROP_FRAME_COUNT)

        flag = True
        image_list = []
        try:
            index = np.random.randint (0, flow_len - _frame_counts)
        except:
            index = 0

        flow_cap.set (cv2.CAP_PROP_POS_FRAMES, index)
        for i in range(_frame_counts):
            flag, image = flow_cap.read ()
            if image is not None:
                image = np.float32 (image)
                image = cv2.resize (image, (340, 256))
                image_list.append (image[...,:2])
            if len (image_list) == _frame_counts or flag is False:
                break

        flow_cap.release ()
        image = np.stack (image_list, axis=0)
        assert 0 not in image.shape
        if _frame_counts != image.shape[0]:
            image = np.repeat(image,(_frame_counts//image.shape[0])+1,axis=0)[:_frame_counts,...]
        assert (_frame_counts,256,340,2) == image.shape
        # flow_file = DataAugmentation.randomCrop (image, 112, 112)
        # rgb_file = DataAugmentation.Multiscale_crop(image,is_flow=True)
        # flow_file = DataAugmentation.randomCrop(image,224,224)
        flow_file = DataAugmentation.borders25(image)
        flow_file = DataAugmentation.horizontal_flip (flow_file)
        flow_file = np.float32([cv2.resize(x,(224,224)) for x in flow_file])

        # image_list = []
        # for i in range(rgb_file.shape[0]):
        #     image_list.append(cv2.resize(flow_file[i],(224,224)))
        # flow_file = np.stack(image_list,axis=0)
        flow_file = np.float32(flow_file)
        flow_file = (flow_file / 255) * 2 - 1

        if label is not None:
            one_hot_label = np.zeros (101, dtype=np.float32)
            one_hot_label[label] = 1

            return flow_file, one_hot_label

        return flow_file
Exemple #5
0
    def _py_func_flow_vp(flow_path, label, _index_list=None):

        f_upath, f_vpath = flow_path
        flow_u_path = f_upath.decode()
        flow_v_path = f_vpath.decode()
        flow_file = os.listdir(flow_u_path)
        flow_file = sorted(flow_file)
        flow_len = len(flow_file)

        if _index_list is None:
            index_list = ucf_dataset.pick_index(flow_len - _new_length,
                                                _frame_counts,
                                                is_flow=True)
        else:
            index_list = _index_list

        flow_img = []
        for j in index_list:
            img_list = []
            for i in range(j, j + _new_length):
                img_u_path = os.path.join(flow_u_path, flow_file[i])
                img_v_path = os.path.join(flow_v_path, flow_file[i])

                img_u = cv2.imread(img_u_path, 0)
                img_v = cv2.imread(img_v_path, 0)
                if img_u is None or img_v is None:
                    continue
                img = np.stack([img_u, img_v], axis=-1)
                img_list.append(img)

            img = np.concatenate(img_list, axis=-1)
            img = cv2.resize(img, (340, 256))
            img = DataAugmentation.Multiscale_crop(img, is_flow=True)
            img = DataAugmentation.horizontal_flip(img)
            img = cv2.resize(img, (224, 224))
            img = np.float32(img)
            img = (img / 255 - 0.5) / 0.226
            flow_img.append(img)

        np.random.shuffle(flow_img)
        if label is not None:

            one_hot_label = np.zeros(101, dtype=np.float32)
            one_hot_label[label] = 1

            return flow_img, one_hot_label

        return flow_img
Exemple #6
0
    def _py_func_flow_vp(flow_path, label):

        flow_path = flow_path.decode ()

        # flow_file = _flow_vp (flow_path)
        flow_cap = cv2.VideoCapture(flow_path)
        flow_len = flow_cap.get(cv2.CAP_PROP_FRAME_COUNT)
        num_segments = 3
        segments_list = np.array_split(np.arange(0,flow_len),num_segments)

        flag = True
        image_list = []
        while num_segments:
            index = np.random.choice(segments_list[num_segments-1])
            flow_cap.set(cv2.CAP_PROP_POS_FRAMES,index)
            img_list = []
            for i in range(10):
                f , img = flow_cap.read()
                if img is not None:
                    img_list.append(img[...,:2])
            if len(img_list) != 10:
                continue

            image = np.concatenate(img_list,axis=-1)
            image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
            image = np.float32(image)
            image = cv2.resize(image,(340,256))
            image = DataAugmentation.Multiscale_crop(image,is_flow=True)
            image = DataAugmentation.horizontal_flip(image)
            image = cv2.resize(image,(224,224))
            image = (image / 255) * 2 - 1
            image_list.append(image)
            num_segments -= 1

        flow_file = image_list


        flow_cap.release()
        if label is not None:
            one_hot_label = np.zeros (101, dtype=np.float32)
            one_hot_label[label] = 1

            return flow_file, one_hot_label
        
        return flow_file
Exemple #7
0
    def _py_func_rgb_vp(rgb_path, label, _index_list=None):

        rgb_path = rgb_path.decode()

        rgb_cap = cv2.VideoCapture(rgb_path)
        rgb_len = rgb_cap.get(cv2.CAP_PROP_FRAME_COUNT)

        image_list = []

        if _index_list is None:
            index_list = ucf_dataset.pick_index(rgb_len,
                                                _frame_counts,
                                                is_flow=True)
        else:
            index_list = _index_list

        for index in index_list:
            rgb_cap.set(cv2.CAP_PROP_POS_FRAMES, index)
            flag, image = rgb_cap.read()
            if image is not None:
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = np.float32(image)
                image = cv2.resize(image, (340, 256))
                image = DataAugmentation.Multiscale_crop(image)
                image = cv2.resize(image, (224, 224))
                image = DataAugmentation.horizontal_flip(image)
                image = normalize(image)
                image_list.append(image)

        if len(image_list) != _frame_counts:
            image_list = np.concatenate([image_list] * _frame_counts,
                                        axis=0)[:_frame_counts]

        rgb_cap.release()
        np.random.shuffle(image_list)
        rgb_file = np.stack(image_list, axis=0)

        if label is not None:
            one_hot_label = np.zeros(101, dtype=np.float32)
            one_hot_label[label] = 1

            return rgb_file, one_hot_label

        return rgb_file
Exemple #8
0
    def _py_func_rgb_vp(rgb_path, label):

        rgb_path = rgb_path.decode()

        rgb_cap = cv2.VideoCapture(rgb_path)
        rgb_len = rgb_cap.get(cv2.CAP_PROP_FRAME_COUNT)

        image_list = []

        index = np.random.randint(0, rgb_len - _frame_counts)
        index_list = np.arange(index, index + _frame_counts)

        for index in index_list:
            rgb_cap.set(cv2.CAP_PROP_POS_FRAMES, index)
            flag, image = rgb_cap.read()
            if image is not None:
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = cv2.resize(image, (340, 256))
                image_list.append(image)

        if len(image_list) != _frame_counts:
            image_list = np.concatenate([image_list] * _frame_counts,
                                        axis=0)[:_frame_counts]

        assert image_list != []

        rgb_cap.release()
        image = np.stack(image_list, axis=0)
        rgb_file = DataAugmentation.randomCrop(image, 224, 224)
        rgb_file = DataAugmentation.horizontal_flip(rgb_file)

        rgb_file = np.float32(rgb_file)
        rgb_file = normalize(rgb_file)

        if label is not None:
            one_hot_label = np.zeros(101, dtype=np.float32)
            one_hot_label[label] = 1

            return rgb_file, one_hot_label

        return rgb_file
Exemple #9
0
    def _py_func_rgb_vp(rgb_path,  label):

        rgb_path = rgb_path.decode ()

        num_segments = 3
        # rgb_file = _rgb_vp (rgb_path)
        rgb_cap = cv2.VideoCapture(rgb_path)
        rgb_len = rgb_cap.get(cv2.CAP_PROP_FRAME_COUNT)
        segments_list = np.array_split(np.arange(0,rgb_len),num_segments)
        image_list = []
        while num_segments:
            index = np.random.choice(segments_list[num_segments-1])
            rgb_cap.set(cv2.CAP_PROP_POS_FRAMES,index)
            flag , image = rgb_cap.read()
            if image is None:
                continue 
            image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
            image = np.float32(image)
            image = cv2.resize(image,(340,256))
            image = DataAugmentation.Multiscale_crop(image)
            image = DataAugmentation.horizontal_flip(image)
            image = cv2.resize(image,(224,224))
            image = normalize(image)
            image_list.append(image)
            num_segments -= 1

        
        assert len(image_list) != num_segments

        rgb_cap.release()
        rgb_file = image_list

        if label is not None:
            one_hot_label = np.zeros (101, dtype=np.float32)
            one_hot_label[label] = 1

            return rgb_file,  one_hot_label
        
        return rgb_file
Exemple #10
0
    def _py_func_flow_vp(flow_path, label):

        flow_path = flow_path.decode ()

        flow_cap = cv2.VideoCapture(flow_path)
        flow_len = flow_cap.get(cv2.CAP_PROP_FRAME_COUNT)

        flag = True
        image_list = []
        while flag:
            try:
                index = np.random.randint(0,flow_len-10)
            except:
                raise ValueError('flow len is too short with %d frame' % flow_len,flow_path)
            flow_cap.set(cv2.CAP_PROP_POS_FRAMES,index)
            for i in range(10):
                f , img = flow_cap.read()
                if img is not None:
                    image_list.append(img[...,:2])
            if len(image_list) == 10:
                flag = False

        image = np.concatenate(image_list,axis=-1)
        flow_cap.release()
        image = np.float32(image)
        image = cv2.resize(image,(340,256))
        flow_file = DataAugmentation.Multiscale_crop(image,is_flow=True)
        flow_file = DataAugmentation.horizontal_flip(flow_file)
        flow_file = cv2.resize(flow_file,(224,224))
        flow_file = (flow_file / 255 - 0.5) / 0.226
        assert 0 not in flow_file.shape

        if label is not None:
            one_hot_label = np.zeros (101, dtype=np.float32)
            one_hot_label[label] = 1

            return flow_file, one_hot_label
        
        return flow_file
Exemple #11
0
    def _py_func_flow_vp(flow_path, label):

        f_upath, f_vpath = flow_path
        flow_u_path = f_upath.decode()
        flow_v_path = f_vpath.decode()
        flow_file = os.listdir(flow_u_path)
        flow_file = sorted(flow_file)
        index = np.random.randint(0, len(flow_file) - _new_length)
        img_list = []
        for i in range(index, index + _new_length):
            img_u_path = os.path.join(flow_u_path, flow_file[i])
            img_v_path = os.path.join(flow_v_path, flow_file[i])

            img_u = cv2.imread(img_u_path, 0)
            img_v = cv2.imread(img_v_path, 0)

            img = np.stack([img_u, img_v], axis=-1)
            img_list.append(img)

        img = np.concatenate(img_list, axis=-1)
        img = cv2.resize(img, (340, 256))
        img = DataAugmentation.Multiscale_crop(img, is_flow=True)
        img = DataAugmentation.horizontal_flip(img)
        img = cv2.resize(img, (224, 224))
        img = np.float32(img)
        if _preprocess_name == 'pytorch':
            img = (img / 255 - 0.5) / 0.226
        elif _preprocess_name == 'tf':
            img = tf_preprocess(img)

        if label is not None:

            one_hot_label = np.zeros(101, dtype=np.float32)
            one_hot_label[label] = 1

            return img, one_hot_label

        return img
Exemple #12
0
    def _py_func_flow_vp(flow_path, label):

        f_upath, f_vpath = flow_path
        flow_u_path = f_upath.decode()
        flow_v_path = f_vpath.decode()
        flow_file = os.listdir(flow_u_path)
        flow_file = sorted(flow_file)
        index = np.random.randint(0, len(flow_file) - _frame_counts)

        img_list = []
        for j in range(index, index + _frame_counts):
            img_u_path = os.path.join(flow_u_path, flow_file[j])
            img_v_path = os.path.join(flow_v_path, flow_file[j])

            img_u = cv2.imread(img_u_path, 0)
            img_v = cv2.imread(img_v_path, 0)
            if img_u is None or img_v is None:
                continue
            img = np.stack([img_u, img_v], axis=-1)
            img = cv2.resize(img, (340, 256))
            img_list.append(img)

        img = np.stack(img_list, axis=0)
        img = DataAugmentation.randomCrop(img, 224, 224)
        img = DataAugmentation.horizontal_flip(img)
        img = np.float32(img)
        img = (img / 255 - 0.5) / 0.226

        if label is not None:

            one_hot_label = np.zeros(101, dtype=np.float32)
            one_hot_label[label] = 1

            return img, one_hot_label

        return img
Exemple #13
0
    def _py_func_rgb_vp(rgb_path, label):

        rgb_path = rgb_path.decode ()

        # rgb_file = _rgb_vp (rgb_path)
        rgb_cap = cv2.VideoCapture (rgb_path)
        rgb_len = rgb_cap.get (cv2.CAP_PROP_FRAME_COUNT)

        image_list = []
        # while 1:
        try:
            index = np.random.randint (0, rgb_len-_frame_counts)
        except:
            index = 0

        rgb_cap.set (cv2.CAP_PROP_POS_FRAMES, index)
        while 1:
            flag, image = rgb_cap.read ()
            if image is not None:
                # image = np.float32 (image)
                image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
                image = cv2.resize (image, (340, 256))
                image_list.append(image)
            if len(image_list) == _frame_counts or flag is False:
                if len(image_list) == 0:
                    rgb_cap.set (cv2.CAP_PROP_POS_FRAMES, 0)    
                    continue
                break

        rgb_cap.release ()
        image = np.stack(image_list,axis=0)

        # some bug in rgb video,maybe you should use ffmpeg for image ,but read video is faster than read image due to IO spare
        assert 0 not in image.shape
        if _frame_counts != image.shape[0]:
            image = np.repeat(image,(_frame_counts//image.shape[0])+1,axis=0)[:_frame_counts,...]
        assert (_frame_counts,256,340,3) == image.shape

        if _crop == 'random':
            rgb_file = DataAugmentation.randomCrop(image,224,224)
            # print('random crop is used')
        else:
            rgb_file =  DataAugmentation.Multiscale_crop(image)
            # print('mutltisacle crop is used')
            rgb_file = np.float32([cv2.resize(x,(224,224)) for x in rgb_file])
        assert 0 not in rgb_file.shape
        rgb_file = DataAugmentation.horizontal_flip (rgb_file)

        rgb_file = np.float32(rgb_file)
        rgb_file = normalize (rgb_file)
        # rgb_file = (rgb_file / 255) * 2 - 1

        assert rgb_file is not None

        if label is not None:
            one_hot_label = np.zeros (101, dtype=np.float32)
            one_hot_label[label] = 1

            return rgb_file, one_hot_label

        return rgb_file