"""\ndef getitem(self, index):\n # import pdb; pdb.set_trace()\n record = self.video_list[index]\n # check this is a legit video folder\n file_name = self.image_tmpl.format(1)\n full_path = os.path.join(record.path, file_name)\n\n while not os.path.exists(full_path):\n print('################## Not Found:', os.path.join(record.path, file_name))\n index = np.random.randint(len(self.video_list))\n record = self.video_list[index]\n file_name = self.image_tmpl.format(1)\n full_path = os.path.join(record.path, file_name)\n\n if not self.test_mode: # training or validation set\n if self.random_shift: # training set\n segment_indices = self._sample_indices(record)\n else: # validation set\n segment_indices = self._get_val_indices(record)\n else: # test set\n # for mulitple clip test, use random sampling;\n # for single clip test, use middle sampling\n if self.multi_clip_test:\n segment_indices = self._sample_indices(record)\n else:\n segment_indices = self._get_test_indices(record)\n return self.get(record, segment_indices)\n"""这段代码定义了一个类的方法__getitem__,该方法用于获取对象的索引值对应的元素。在这个特定的代码中,它被用于获取self.video_list列表中索引为index的元素。\n\n首先,通过self.video_list[index]获取索引为index的元素,并将其赋值给变量record。\n\n接下来,通过self.image_tmpl.format(1)获取文件名,并将其赋值给变量file_name。然后,通过os.path.join(record.path, file_name)获取完整的文件路径,并将其赋值给变量full_path。\n\n接下来,使用while not os.path.exists(full_path):循环判断文件路径是否存在。如果不存在,则输出一条提示信息,并随机生成一个索引值index。然后,通过self.video_list[index]获取索引为index的元素,并将其赋值给变量record。再次通过self.image_tmpl.format(1)获取文件名,并将其赋值给变量file_name。最后,通过os.path.join(record.path, file_name)获取新的完整文件路径,并将其赋值给变量full_path。\n\n当文件路径存在时,根据不同的模式(训练模式、验证模式或测试模式)以及是否使用随机采样(random_shift)的方法,获取片段的索引segment_indices。\n\n最后,调用self.get(record, segment_indices)方法,并返回其结果。

__getitem__ 方法详解:Python 列表索引访问与数据处理

原文地址: https://www.cveoy.top/t/topic/quHs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录