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
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
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