def run(self):
     try:
         while True:
             item = None
             while item is None:
                 item = self.load_next_frame()
             self.queue.put(item)
     except:
         cprint('An Error Happended in run()', bcolors.FAIL)
         cprint(str("".join(traceback.format_exception(*sys.exc_info()))),
                bcolors.FAIL)
         self.queue.put(None)
         raise Exception("".join(
             traceback.format_exception(*sys.exc_info())))
    def load_items(self):
        self.db_items = []
        if 'image_sets' in self.params:
            for image_set in self.params['image_sets']:
                print(image_set)
                if image_set.startswith('pascal') or image_set.startswith(
                        'sbd'):
                    if image_set.startswith('pascal'):
                        pascal_db = imdb.PASCAL(self.params['pascal_path'],
                                                image_set[7:])
                    elif image_set.startswith('sbd'):
                        pascal_db = imdb.PASCAL(self.params['sbd_path'],
                                                image_set[4:])
                    #reads single image and all semantic classes are presented in the label

                    if self.params['output_type'] == 'single_image':
                        items = pascal_db.getItems(
                            self.params['pascal_cats'],
                            self.params['areaRng'],
                            read_mode=imdb.PASCAL_READ_MODES.SEMANTIC_ALL)
                    #reads pair of images from one semantic class and and with binary labels
                    elif self.params['output_type'] == 'image_pair':
                        items = pascal_db.getItems(
                            self.params['pascal_cats'],
                            self.params['areaRng'],
                            read_mode=imdb.PASCAL_READ_MODES.SEMANTIC)
                        items = self._remove_small_objects(items)
                    else:
                        raise Exception(
                            'Only single_image and image_pair mode are supported'
                        )
                    self.db_items.extend(items)
                else:
                    raise Exception
            cprint('Total of ' + str(len(self.db_items)) + ' db items loaded!',
                   bcolors.OKBLUE)

            #reads pair of images from one semantic class and and with binary labels
            if self.params['output_type'] == 'image_pair':
                items = self.db_items

                #In image_pair mode pair of images are sampled from the same semantic class
                clusters = imdb.PASCAL.cluster_items(self.db_items)

                #for set_id in clusters.keys():
                #    print clusters[set_id].length

                #db_items will be a list of tuples (set,j) in which set is the set that img_item belongs to and j is the index of img_item in that set
                self.db_items = []
                for item in items:
                    set_id = item.obj_ids[0]
                    imgset = clusters[set_id]
                    assert (
                        imgset.length > self.params['k_shot']
                    ), 'class ' + imgset.name + ' has only ' + imgset.length + ' examples.'
                    in_set_index = imgset.image_items.index(item)
                    self.db_items.append((imgset, in_set_index))
                cprint('Total of ' + str(len(clusters)) + ' classes!',
                       bcolors.OKBLUE)

        self.orig_db_items = copy.copy(self.db_items)

        assert (len(self.db_items) >
                0), 'Did not load anything from the dataset'
        #assert(not self.params.has_key('db_cycle') or len(self.db_items) >= self.params['db_cycle']), 'DB Cycle should can not be more than items in the database = ' + str(len(self.db_items))
        #it forces the update_seq_index function to shuffle db_items and set seq_index = 0
        self.seq_index = len(self.db_items)
    def load_frame(self, player, first_index, second_index):
        cprint(
            'Loading pair = ' + player.name + ', ' + str(first_index) + ', ' +
            str(second_index), bcolors.WARNING)
        if second_index in first_index:
            return None

        images1 = []
        labels1 = []
        image_path = []
        shape1 = self.first_shape
        for ind in first_index:
            frame1_dict = player.get_frame(ind)
            image1, label1, shape1 = self.__prepross(frame1_dict, shape1)
            images1.append(image1.transpose((2, 0, 1)))
            labels1.append(label1)
            image_path.append(frame1_dict['image_path'])
        item = dict(first_img=images1, image1_path=image_path)

        if second_index is not None:
            frame2_dict = player.get_frame(second_index)
            image2, label2, shape = self.__prepross(frame2_dict,
                                                    self.second_shape)
            item['second_img'] = [image2.transpose((2, 0, 1))]
            item['image2_path'] = [
                frame2_dict['image_path'],
            ]

        if self.deploy_mode:
            first_semantic_labels = []
            first_mask_orig = []
            first_img_orig = []
            for ind in first_index:
                a, b, c = self.__get_deploy_info(player, ind)
                first_semantic_labels.append(a)
                first_mask_orig.append(b)
                first_img_orig.append(c)

            deploy_info = dict(seq_name=player.name,
                               first_index=first_index,
                               first_img_orig=first_img_orig,
                               first_mask_orig=first_mask_orig,
                               first_semantic_labels=first_semantic_labels)

            if second_index is not None:
                second_semantic_labels, second_mask_orig, second_img_orig = self.__get_deploy_info(
                    player, second_index)
                deploy_info.update(
                    second_index=second_index,
                    second_img_orig=second_img_orig,
                    second_mask_orig=second_mask_orig,
                    second_semantic_labels=second_semantic_labels)

            item['deploy_info'] = deploy_info

        #create first_labels
        for i in range(len(self.first_label_params)):
            name, down_scale, offset = self.first_label_params[i]
            item[name] = []
            for label1 in labels1:
                nlabel1 = util.change_coordinates(label1, down_scale, offset)
                nlabel1 = (nlabel1 -
                           self.first_label_mean) * self.first_label_scale
                assert (self.__is_integer(nlabel1))
                item[name].append(nlabel1.reshape((1, ) + nlabel1.shape))

        if second_index is not None:
            #create second_labels
            for i in range(len(self.second_label_params)):
                name, down_scale, offset = self.second_label_params[i]
                nlabel2 = util.change_coordinates(label2, down_scale, offset)
                assert (self.__is_integer(nlabel2))
                item[name] = [nlabel2.reshape((1, ) + nlabel2.shape)]
        if self.has_cont:
            item['cont'] = [0] + [1] * (len(first_index) - 1)

        return item