in data/video_iterator.py [0:0]
def getitem_from_raw_video(self, index):
# get current video info
v_id, copy_id, label, vid_subpath, frame_count = self.video_list[index]
video_path = os.path.join(self.video_prefix, vid_subpath)
video = None
successful = False
try:
video = self.video.open(vid_path=video_path)
# cache video info for faster speed
if frame_count < 0:
frame_count = video.count_frames()
if frame_count < 1:
raise IOError("video ({}) does not have any frames".format(video_path))
self.video_list[index][-1] = frame_count
# extract frames, try 5 times
num_attempt = 5
for i_trial in range(0, num_attempt):
try:
# dynamic sampling
sampled_idxs = self.sampler.sampling(range_max=frame_count, v_id=v_id, copy_id=copy_id)
# extracting frames
sampled_frames = video.extract_frames(idxs=sampled_idxs, force_color=self.force_color)
break
except IOError as e:
# raise error at the last time
if i_trial == (num_attempt-1):
raise e
successful = sampled_frames is not None
except IOError as e:
if video:
video.close()
logging.warning("VideoIter:: >> I/O error({0}): {1}".format(e.errno, e.strerror))
if not successful:
assert (self.backup_item is not None), "Sampling failed, backup inavailable. Terminated!"
logging.warning("VideoIter:: >> sampling failed, use backup item!")
video = self.video.open(vid_path=self.backup_item['video_path'])
sampled_frames = video.extract_frames(idxs=self.backup_item['sampled_idxs'])
elif self.tolerant_corrupted_video:
if (self.backup_item is None) or (self.rng.rand() < 0.1):
self.backup_item = {'video_path': video_path, 'sampled_idxs': sampled_idxs}
video.close()
# [(H, W, C), ...] -> (H, W, N*C)
video_clip = np.concatenate(sampled_frames, axis=2)
# apply video augmentation
if self.video_transform is not None:
if v_id % 100 == 0:
self.video_transform.set_random_state(seed=v_id+int(time.time()))
video_clip = self.video_transform(video_clip, idx=v_id, copy_id=copy_id)
return video_clip, label, vid_subpath