Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_data(opt, video_name, transform):
decord_vr = decord.VideoReader(video_name, width=opt.new_width, height=opt.new_height)
duration = len(decord_vr)
opt.skip_length = opt.new_length * opt.new_step
segment_indices, skip_offsets = sample_indices(opt, duration)
if opt.video_loader:
if opt.slowfast:
clip_input = video_TSN_decord_slowfast_loader(opt, video_name, decord_vr, duration, segment_indices, skip_offsets)
else:
clip_input = video_TSN_decord_batch_loader(opt, video_name, decord_vr, duration, segment_indices, skip_offsets)
clip_input = transform(clip_input)
if opt.slowfast:
sparse_sampels = len(clip_input) // (opt.num_segments * opt.num_crop)
clip_input = np.stack(clip_input, axis=0)
def read_data(opt, video_name, transform):
decord_vr = decord.VideoReader(video_name, width=opt.new_width, height=opt.new_height)
duration = len(decord_vr)
opt.skip_length = opt.new_length * opt.new_step
segment_indices, skip_offsets = sample_indices(opt, duration)
if opt.video_loader:
if opt.slowfast:
clip_input = video_TSN_decord_slowfast_loader(opt, video_name, decord_vr, duration, segment_indices, skip_offsets)
else:
clip_input = video_TSN_decord_batch_loader(opt, video_name, decord_vr, duration, segment_indices, skip_offsets)
clip_input = transform(clip_input)
if opt.slowfast:
sparse_sampels = len(clip_input) // (opt.num_segments * opt.num_crop)
clip_input = np.stack(clip_input, axis=0)
parser.add_argument('--gpu', type=int, default=-1, help='context to run, use --gpu=-1 to use cpu only')
parser.add_argument('--file', type=str, default='/tmp/testsrc_h264_100s_default.mp4', help='Test video')
parser.add_argument('--seed', type=int, default=666, help='numpy random seed for random access indices')
parser.add_argument('--random-frames', type=int, default=300, help='number of random frames to run')
parser.add_argument('--width', type=int, default=320, help='resize frame width')
parser.add_argument('--height', type=int, default=240, help='resize frame height')
args = parser.parse_args()
test_video = args.file
if args.gpu > -1:
ctx = de.gpu(args.gpu)
else:
ctx = de.cpu()
vr = de.VideoReader(test_video, ctx, width=args.width, height=args.height)
cnt = 0
tic = time.time()
while True:
try:
frame = vr.next()
except StopIteration:
break
cnt += 1
print(cnt, ' frames, elapsed time for sequential read: ', time.time() - tic)
np.random.seed(args.seed) # fix seed for all random tests
acc_indices = np.arange(len(vr))
np.random.shuffle(acc_indices)
if args.random_frames > len(vr):
warnings.warn('Number of random frames reduced to {} to fit test video'.format(len(vr)))
args.random_frames = len(vr)
parser = argparse.ArgumentParser("Decord benchmark")
parser.add_argument('--gpu', type=int, default=-1, help='context to run, use --gpu=-1 to use cpu only')
parser.add_argument('--file', type=str, default='/tmp/testsrc_h264_100s_default.mp4', help='Test video')
parser.add_argument('--seed', type=int, default=666, help='numpy random seed for random access indices')
parser.add_argument('--random-frames', type=int, default=300, help='number of random frames to run')
parser.add_argument('--width', type=int, default=320, help='resize frame width')
parser.add_argument('--height', type=int, default=240, help='resize frame height')
args = parser.parse_args()
test_video = args.file
if args.gpu > -1:
ctx = de.gpu(args.gpu)
else:
ctx = de.cpu()
vr = de.VideoReader(test_video, ctx, width=args.width, height=args.height)
cnt = 0
tic = time.time()
while True:
try:
frame = vr.next()
except StopIteration:
break
cnt += 1
print(cnt, ' frames, elapsed time for sequential read: ', time.time() - tic)
np.random.seed(args.seed) # fix seed for all random tests
acc_indices = np.arange(len(vr))
np.random.shuffle(acc_indices)
if args.random_frames > len(vr):
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../python'))
import decord as de
parser = argparse.ArgumentParser("Decord benchmark")
parser.add_argument('--gpu', type=int, default=-1, help='context to run, use --gpu=-1 to use cpu only')
parser.add_argument('--file', type=str, default='/tmp/testsrc_h264_100s_default.mp4', help='Test video')
parser.add_argument('--seed', type=int, default=666, help='numpy random seed for random access indices')
parser.add_argument('--random-frames', type=int, default=300, help='number of random frames to run')
parser.add_argument('--width', type=int, default=320, help='resize frame width')
parser.add_argument('--height', type=int, default=240, help='resize frame height')
args = parser.parse_args()
test_video = args.file
if args.gpu > -1:
ctx = de.gpu(args.gpu)
else:
ctx = de.cpu()
vr = de.VideoReader(test_video, ctx, width=args.width, height=args.height)
cnt = 0
tic = time.time()
while True:
try:
frame = vr.next()
except StopIteration:
break
cnt += 1
print(cnt, ' frames, elapsed time for sequential read: ', time.time() - tic)
np.random.seed(args.seed) # fix seed for all random tests
acc_indices = np.arange(len(vr))
# pip install decord
########################################################################
# Usage
# -----
#
# We provide some usage cases here to get you started. For complete API, please refer to official documentation.
################################################################
# Suppose we want to read a video. Let's download the example video first.
from gluoncv import utils
url = 'https://github.com/bryanyzhu/tiny-ucf101/raw/master/abseiling_k400.mp4'
video_fname = utils.download(url)
from decord import VideoReader
vr = VideoReader(video_fname)
################################################################
# If we want to load the video in a specific dimension so that it can be fed into a CNN for processing,
vr = VideoReader(video_fname, width=320, height=256)
################################################################
# Now we have loaded the video, if we want to know how many frames are there in the video,
duration = len(vr)
print('The video contains %d frames' % duration)
################################################################
# If we want to access frame at index 10,
frame = vr[9]
def __getitem__(self, idx):
"""
Return:
clips (torch.tensor), label (int)
"""
record = self.video_records[idx]
video_reader = decord.VideoReader(
"{}.{}".format(os.path.join(self.video_dir, record.path), self.video_ext),
# TODO try to add `ctx=decord.ndarray.gpu(0) or .cuda(0)`
)
record._num_frames = len(video_reader)
offsets = self._sample_indices(record)
clips = np.array([self._get_frames(video_reader, o) for o in offsets])
if self.num_segments == 1:
# [T, H, W, C] -> [C, T, H, W]
return self.transforms(torch.from_numpy(clips[0])), record.label
else:
# [S, T, H, W, C] -> [S, C, T, H, W]
return (
torch.stack([
self.transforms(torch.from_numpy(c)) for c in clips
def __getitem__(self, idx):
record = self.video_infos[idx]
if self.use_decord:
video_reader = decord.VideoReader('{}.{}'.format(
osp.join(self.img_prefix, record.path), self.video_ext))
record.num_frames = len(video_reader)
else:
video_reader = mmcv.VideoReader('{}.{}'.format(
osp.join(self.img_prefix, record.path), self.video_ext))
record.num_frames = len(video_reader)
if self.test_mode:
segment_indices, skip_offsets = self._get_test_indices(record)
else:
segment_indices, skip_offsets = self._sample_indices(
record) if self.random_shift else self._get_val_indices(record)
data = dict(
num_modalities=DC(to_tensor(len(self.modalities))),
gt_label=DC(to_tensor(record.label), stack=True, pad_dims=None))
#
# We provide some usage cases here to get you started. For complete API, please refer to official documentation.
################################################################
# Suppose we want to read a video. Let's download the example video first.
from gluoncv import utils
url = 'https://github.com/bryanyzhu/tiny-ucf101/raw/master/abseiling_k400.mp4'
video_fname = utils.download(url)
from decord import VideoReader
vr = VideoReader(video_fname)
################################################################
# If we want to load the video in a specific dimension so that it can be fed into a CNN for processing,
vr = VideoReader(video_fname, width=320, height=256)
################################################################
# Now we have loaded the video, if we want to know how many frames are there in the video,
duration = len(vr)
print('The video contains %d frames' % duration)
################################################################
# If we want to access frame at index 10,
frame = vr[9]
print(frame.shape)
################################################################
# For deep learning, usually we want to get multiple frames at once. Now you can use ``get_batch`` function,
# Suppose we want to get a 32-frame video clip by skipping one frame in between,
# Now we want to compare its speed with Opencv VideoCapture to demonstrate its efficiency.
# Let's load the same video and get all the frames randomly using both decoders to compare their performance.
# We will run the loading for 11 times: use the first one as warming up, and average the rest 10 runs as the average speed.
import cv2
import time
import numpy as np
frames_list = np.arange(duration)
np.random.shuffle(frames_list)
# Decord
for i in range(11):
if i == 1:
start_time = time.time()
decord_vr = VideoReader(video_fname)
frames = decord_vr.get_batch(frames_list)
end_time = time.time()
print('Decord takes %4.4f seconds.' % ((end_time - start_time)/10))
# OpenCV
for i in range(11):
if i == 1:
start_time = time.time()
cv2_vr = cv2.VideoCapture(video_fname)
for frame_idx in frames_list:
cv2_vr.set(1, frame_idx)
_, frame = cv2_vr.read()
cv2_vr.release()
end_time = time.time()
print('OpenCV takes %4.4f seconds.' % ((end_time - start_time)/10))