Пример #1
0
    def __call__(self, example):
        image = example['image']
        im_w, im_h = image.size

        if im_w < self.w or im_h < self.h:
            if im_h < im_w:
                scale = self.h / im_h
                image = image.resize((int(0.5 + im_w * scale), self.h))
            else:
                scale = self.w / im_w
                image = image.resize((self.w, int(0.5 + im_h * scale)))

            new_boxes = []
            for box in example['boxes']:
                new_boxes.append(
                    BoundingBox(box.left * scale, box.top * scale,
                                box.right * scale, box.bottom * scale,
                                image.size[0], image.size[1], box.label))
            boxes = new_boxes

            return {
                'image': image,
                'boxes': boxes,
                'labels': example['labels']
            }
        return example
Пример #2
0
def update_task(path, tasks, label_map):
    t = pd.read_csv(path, sep='\t', dtype=str)
    if t.values.shape[0] == 0:
        return
    dataset_name = path.split('/')[5]
    start = path.find('annotations') + len('annotations') + 1
    # print(path, start, path[start:])
    task = os.path.join(dataset_name, os.path.splitext(path[start:])[0])
    # print(task)
    boxes = []
    labels = []
    ext = t.ext[0] if 'ext' in t else 'jpg'
    impath = os.path.join(DIR, dataset_name, 'images', path[start:].replace('.tsv', '.' + ext))

#    img = cv2.imread(impath)
#    height, width = img.shape[:2]
    for _, row in t.iterrows():
        if ((float(row.xbr) - float(row.xtl)) >=cfg.width) or ((float(row.ybr) - float(row.ytl))) >= cfg.height:
            continue
        class_ = row['class']
        if class_ not in label_map:
            label = 97
        else:
            label = label_map[class_]
        box = BoundingBox(
            max(0, float(row.xtl)),
            max(0, float(row.ytl)),
            min(10000, float(row.xbr)),
            min(10000, float(row.ybr)),
            -1,
            -1,
            label)
        boxes.append(box)
        labels.append(label)

    if len(boxes) == 0:
        return
    impath = os.path.join(DIR, dataset_name, 'images', path[start:].replace('.tsv', '.' + ext))
    if not os.path.exists(impath):
        print(impath)
        1/0
    tasks[task] = {
        'impath': impath,
        'boxes': boxes,
        'labels': [int(l) for l in labels],
    }
Пример #3
0
    def build_annotations(self):
        box_dict = {}
        annos = json.loads(open(self.anno_file).read())

        for id in annos['imgs']:
            boxes = []
            for sign_dict in annos['imgs'][id]['objects']:
                label = sign_dict['category']

                left = int(sign_dict['bbox']['xmin'])
                right = int(sign_dict['bbox']['xmax'])
                top = int(sign_dict['bbox']['ymin'])
                bottom = int(sign_dict['bbox']['ymax'])
                box = BoundingBox(left, top, right, bottom, cfg.width,
                                  cfg.height, self.labels.index(label))
                boxes.append(box)
            if len(boxes) > 0:

                box_dict[annos['imgs'][id]['path'].split('/')[-1]] = boxes
        return box_dict
Пример #4
0
    def __getitem__(self, idx):
        task = self.task_dict[self.tasks[idx]]
        image = Image.fromarray(cv2.imread(task['impath'])[...,::-1])
        boxes = task['boxes']
        labels = task['labels']

        width, height = image.size

        new_boxes = []
        for box in boxes:
            box = BoundingBox(
            max(0, float(box.left)),
            max(0, float(box.top)),
            min(width, float(box.right)),
            min(height, float(box.bottom)), -1, -1, 0)
            new_boxes.append(box)
        boxes = new_boxes


        example = {
            'image': image,
            'boxes': boxes,
            'labels': labels
        }

        if not self.test:
            example = self.pil_transform(example)

        if self.transform is not None:
            example = self.transform(example)

        if True:

            pass

        return example
Пример #5
0
    def __call__(self, example):
        image = example['image']
        boxes = example['boxes']
        labels = example['labels']
        im_w, im_h = image.size

        try:

            #         print(im_w, im_h)
            box = np.random.choice(boxes)
            from copy import deepcopy
            orig = deepcopy(box)

            if (box.right - box.left) > self.w:
                x_from = max(0, box.left - self.s)
                x_to = x_from + 1
            else:

                x_from = max(0, box.right - self.w)
                x_to = min(im_w - self.w, box.left) + 1

    #         print(x_from, x_to)
            x = np.random.randint(int(x_from), int(x_to))

            if (box.bottom - box.top) > self.h:
                y_from = max(0, box.top - self.s)
                y_to = y_from + 1
            else:
                y_from = max(0, box.bottom - self.h)
                y_to = min(im_h - self.h, box.top) + 1

    #         print(y_from, y_to)
            y = np.random.randint(int(y_from), int(y_to))
        except:
            print('bbox:', box.left, box.top, box.right, box.bottom)
            print('im_h: {}, im_w: {}'.format(im_h, im_w))
            print('x:')
            print('x_from; max(0,', box.right - self.w)
            print('x_to: min', im_w - self.w, box.left)
            print(x_from, x_to)
            print('y:')
            print('y_from: max(0,', box.bottom - self.h)
            print('y_to: min', im_h - self.h, box.top)
            print(y_from, y_to)
            sys.stdout.flush()
        crop = [x, y, x + self.w, y + self.h]

        image = image.crop(crop)

        new_boxes = []
        new_labels = []
        for box, label in zip(boxes, labels):
            iom = RandomCrop._iom(crop,
                                  [box.left, box.top, box.right, box.bottom])
            #             print(iom)

            if iom < self.min_visibility:
                continue

            new_boxes.append(
                BoundingBox(max(0, box.left - x), max(0, box.top - y),
                            min(self.w - 1, box.right - x),
                            min(self.h - 1, box.bottom - y), self.w, self.h,
                            label))
            new_labels.append(label)
        if len(new_boxes) == 0:
            print(new_boxes)
            box = orig
            print('bbox:', box.left, box.top, box.right, box.bottom)
            print('im_h: {}, im_w: {}'.format(im_h, im_w))
            print('x:')
            print('x_from; max(0,', box.right - self.w)
            print('x_to: min', im_w - self.w, box.left)
            print(x_from, x_to)
            print('y:')
            print('y_from: max(0,', box.bottom - self.h)
            print('y_to: min', im_h - self.h, box.top)
            print(y_from, y_to)
            sys.stdout.flush()

        return {'image': image, 'boxes': new_boxes, 'labels': new_labels}