Esempio n. 1
0
    def crop_for_train(self, over_samples=None):
        """训练集裁剪

        Args:
            over_samples (dict): {类别: 重采样次数, ...}
        """
        for i in range(len(self.dataset)):
            data = self.dataset[i]
            # 索引或迭代dataset必须提供包含image字段和anns字段信息
            anns = data['anns']
            if len(anns) == 0:
                print('{} has no label'.format(data['image']))
            img = cvtools.imread(data['image'])
            self.crop_method.crop(img, anns)
            # croped可为空,即没有任何裁剪,同时原始图亦不保留
            cropped = self.crop_method.match_anns(anns)

            # 过采样扩展,对少样本类别过采样
            if over_samples is not None:
                add_croped = self.over_sample(img, anns, over_samples)
                cropped.update(add_croped)

            self.crops.append(cropped)
            print('crop image %d of %d: %s' %
                  (i, len(self.dataset), osp.basename(data['image'])))

        # 打印和清空统计信息
        print(self.crop_method.get_stats())
        self.crop_method.reset_stats()
        if hasattr(self.crop_method, 'stats_crop'):
            print(self.crop_method.stats_crop)
            self.crop_method.stats_crop = {}
Esempio n. 2
0
 def vis_box(self, save_root, has_box=True, has_segm=True, has_crop=True):
     for i, image_name in enumerate(self.imgs):
         print('Visualize image %d of %d: %s' %
               (i, len(self.imgs), image_name))
         # read image
         image_file = osp.join(self.img_prefix, image_name)
         img = cvtools.imread(image_file)
         if img is None:
             print('{} is None.'.format(image_file))
             continue
         imgToObjs = self.img_to_objs[i]
         for crop_i, img_coor in enumerate(imgToObjs):
             if has_crop:
                 img = cvtools.draw_boxes_texts(
                     img, img_coor, colors='random', line_width=2)
             objs = imgToObjs[img_coor]
             if has_box:
                 boxes = [obj['bbox'] for obj in objs]
                 img = cvtools.draw_boxes_texts(
                     img, boxes, colors=[(255, 0, 0)]*len(boxes), box_format='x1y1wh')
             if has_segm:
                 segms = [obj['segmentation'][0] for obj in objs]
                 img = cvtools.draw_boxes_texts(
                     img, segms, box_format='polygen')
         to_file = osp.join(save_root, 'images', image_name)
         cvtools.imwrite(img, to_file)
Esempio n. 3
0
 def augment_one_image(line):
     nonlocal annts_lines
     line = line.strip().split()
     file_path = line[0]
     boxes = []
     classes = []
     for label_str in line[1:]:
         bbox_cls_str = label_str.split(',')
         boxes.append([float(i) for i in bbox_cls_str][0:4])
         classes.append(int(bbox_cls_str[4]))
     boxes = np.array(boxes)
     classes = np.array(classes)
     new_name = osp.splitext(file_path.split(os.sep)[-1])[0]
     new = save_path + new_name.replace('.jpg', '') + '_{index}.jpg'
     for im_index in range(1, 5):    # 每张图片增强出4张
         new_image_name = new.format(index=im_index)
         if not os.path.isfile(new_image_name):
             im = cvtools.imread(file_path)
             img, boxes_trans, classes_trans = transfer(im, boxes,
                                                        classes)
             boxes_trans = boxes_trans.astype(np.int32)
             classes_trans = classes_trans.astype(np.int32)
             # print('save %s...' % new_image_name)
             cv2.imwrite(new_image_name, img)
             annts_lines += new_image_name + ' '
             for box, cls in zip(boxes_trans, classes_trans):
                 annts_lines += ','.join(map(str, box)) + \
                                ',' + str(cls) + ' '
             annts_lines += '\n'
Esempio n. 4
0
    def crop_with_label(self, save_root):
        image_ids = self.COCO.getImgIds()
        image_ids.sort()
        roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))
        if cvtools._DEBUG:
            roidb = roidb[:10]
        for i, entry in enumerate(roidb):
            image_name = entry['file_name']
            image_file = osp.join(self.img_prefix, image_name)
            ann_ids = self.COCO.getAnnIds(imgIds=entry['id'], iscrowd=None)
            ann = self.COCO.loadAnns(ann_ids)
            if len(ann) == 0:
                print('{} ann is None.'.format(image_name))
                continue

            # read image
            img = cvtools.imread(image_file)
            if img is None:
                print('{} is None.'.format(image_file))
                continue

            print('crop image %d of %d: %s' %
                  (i, len(roidb), image_name))

            # crop image
            crop_imgs, starts = self.crop(img, ann)

            # handling the box at the edge of the cropped image
            self.imgs.append(image_name)
            self.match_img_objs(crop_imgs, starts, ann)

        # save crop results
        self.save_crop_labeltxt(save_root)
Esempio n. 5
0
    def crop_with_label(self, save_root='./', iof_th=0.5):
        image_ids = self.COCO.getImgIds()
        image_ids.sort()
        if cvtools._DEBUG:
            roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))[:10]
        else:
            roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))
        print('{} images.'.format(len(roidb)))

        cvtools.makedirs(save_root + '/images')
        cvtools.makedirs(save_root + '/labelTxt+crop')

        stats = defaultdict(crop_objs=0,
                            total_objs=0,
                            missing_objs=0,
                            total_croped_images=0)
        for entry in tqdm(roidb):
            if cvtools._DEBUG:
                print('crop {}'.format(entry['file_name']))
            # read image
            image_name = entry['file_name']
            image_file = osp.join(self.img_prefix, image_name)
            img = cvtools.imread(image_file)
            if img is None:
                print('{} is None.'.format(image_file))
                continue

            # crop image
            crop_imgs, starts = self.crop(img)

            # handling the box at the edge of the cropped image
            ann_ids = self.COCO.getAnnIds(imgIds=entry['id'], iscrowd=None)
            img_to_objs, obj_to_num = self.deal_edged_boxes(ann_ids,
                                                            crop_imgs,
                                                            starts,
                                                            iof_th=iof_th)
            if img_to_objs is None:
                continue

            # stats
            for _, num in obj_to_num.items():
                stats['crop_objs'] += num
            stats['total_objs'] += len(ann_ids)
            stats['missing_objs'] += len(set(ann_ids) - set(obj_to_num.keys()))
            for obj in img_to_objs.values():
                if len(obj) > 0:
                    stats['total_croped_images'] += 1

            # save results
            # self.save_crop_labeltxt(image_name, img_to_objs, save_root)

        # save stats values
        total_images = len(roidb)
        stats['total_images'] = len(roidb)
        stats['objs_per_croped_image'] = stats['total_croped_images'] / float(
            total_images)
        stats['objs_per_image'] = stats['total_objs'] / float(total_images)
        cvtools.save_json(stats, to_file='stats.json')
Esempio n. 6
0
 def read_img_or_crop(self, entry):
     image_name = entry['file_name']
     image_file = osp.join(self.img_prefix, image_name)
     try:
         img = cvtools.imread(image_file)
     except FileNotFoundError:
         print('image {} is not found!'.format(image_file))
         img = None
     image_name = osp.splitext(image_name)[0]
     if 'crop' in entry:
         img = img[entry['crop'][1]:entry['crop'][3] + 1,
                   entry['crop'][0]:entry['crop'][2] + 1]
         image_name = '_'.join([image_name] + list(map(str, entry['crop'])))
     return img, image_name
Esempio n. 7
0
 def vis_instances(self,
                   save_root,
                   vis='bbox',
                   box_format='x1y1wh',
                   by_cat=False):
     if by_cat:
         self._vis_instances_by_cat(save_root, vis, box_format)
     image_ids = self.COCO.getImgIds()
     image_ids.sort()
     if cvtools._DEBUG:
         roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))[:10]
     else:
         roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))
     print('{} images.'.format(len(roidb)))
     cvtools.makedirs(save_root)
     for i, entry in enumerate(roidb):
         print('Visualize image %d of %d: %s' %
               (i, len(roidb), entry['file_name']))
         image_name = entry['file_name']
         image_file = osp.join(self.img_prefix, image_name)
         img = cvtools.imread(image_file)
         image_name = osp.splitext(image_name)[0]
         if 'crop' in entry:
             img = img[entry['crop'][1]:entry['crop'][3],
                       entry['crop'][0]:entry['crop'][2]]
             image_name = '_'.join([image_name] +
                                   list(map(str, entry['crop'])))
         if img is None:
             print('{} is None.'.format(image_file))
             continue
         ann_ids = self.COCO.getAnnIds(imgIds=entry['id'], iscrowd=None)
         objs = self.COCO.loadAnns(ann_ids)
         if len(objs) == 0:
             continue
         # Sanitize bboxes -- some are invalid
         for obj in objs:
             vis_obj = []
             if 'ignore' in obj and obj['ignore'] == 1:
                 continue
             if vis in obj:
                 vis_obj = obj[vis]
             class_name = self.COCO.cats[
                 obj['category_id']]['name'] if 'category_id' in obj else ''
             img = cvtools.draw_boxes_texts(img,
                                            vis_obj,
                                            class_name,
                                            box_format=box_format)
         # save in jpg format for saving storage
         cvtools.imwrite(img, osp.join(save_root, image_name + '.jpg'))
Esempio n. 8
0
 def vis_dets(self, img_name, result):
     dets = result['results']
     img = cvtools.imread(img_name)
     for cls_id, det_cls in dets.items():
         det_cls = det_cls[det_cls[:, -1] > 0.5]
         if len(det_cls) == 0: continue
         ids = poly_nms.poly_gpu_nms(det_cls, 0.15)
         det_cls = det_cls[ids]
         if cls_id == 15:
             img = cvtools.draw_boxes_texts(img, det_cls[:, :-1],
                                         line_width=2,
                                         box_format="polygon")
     img_name = osp.basename(img_name)
     to_file = osp.join('/code/CenterNet/exp/ctdet/vis', img_name)
     cvtools.imwrite(img, to_file)
Esempio n. 9
0
 def _vis_instances_by_cat(self,
                           save_root,
                           vis_cats=None,
                           vis='bbox',
                           box_format='x1y1wh'):
     catImgs = copy.deepcopy(self.COCO.catToImgs)
     catImgs = {cat: set(catImgs[cat]) for cat in catImgs}
     for cat_id, image_ids in catImgs.items():
         cat_name = self.COCO.cats[cat_id]['name']
         if vis_cats is not None and cat_name not in vis_cats:
             continue
         print('Visualize %s' % cat_name)
         if cvtools._DEBUG:
             roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))[:10]
         else:
             roidb = copy.deepcopy(self.COCO.loadImgs(image_ids))
         for i, entry in enumerate(roidb):
             print('Visualize image %d of %d: %s' %
                   (i, len(roidb), entry['file_name']))
             image_name = entry['file_name']
             image_file = osp.join(self.img_prefix, image_name)
             img = cvtools.imread(image_file)  # support chinese
             image_name = osp.splitext(image_name)[0]
             if 'crop' in entry:
                 img = img[entry['crop'][1]:entry['crop'][3],
                       entry['crop'][0]:entry['crop'][2]]
                 image_name = '_'.join([image_name] +
                                       list(map(str, entry['crop'])))
             if img is None:
                 print('{} is None.'.format(image_file))
                 continue
             ann_ids = self.COCO.getAnnIds(imgIds=entry['id'], iscrowd=None)
             objs = self.COCO.loadAnns(ann_ids)
             for obj in objs:
                 if obj['category_id'] != cat_id:
                     continue
                 if 'ignore' in obj and obj['ignore'] == 1:
                     continue
                 vis_obj = []
                 if vis in obj:
                     vis_obj = obj[vis]
                 class_name = [cat_name if 'category_id' in obj else '']
                 img = cvtools.draw_boxes_texts(img,
                                                vis_obj,
                                                class_name,
                                                box_format=box_format)
             # save in jpg format for saving storage
             cvtools.imwrite(img, osp.join(save_root, cat_name, image_name + '.jpg'))
Esempio n. 10
0
 def crop_for_test(self, save):
     from collections import defaultdict
     imgs = cvtools.get_images_list(self.img_prefix)
     self.test_dataset = defaultdict(list)
     for image_file in tqdm(imgs):
         if cvtools._DEBUG:
             print('crop {}'.format(image_file))
         image_name = osp.basename(image_file)
         img = cvtools.imread(image_file)  # support chinese
         if img is None:
             print('{} is None.'.format(image_file))
             continue
         crop_imgs, starts = self.crop(img)
         for crop_img, start in zip(crop_imgs, starts):
             crop_rect = start[0], start[1], start[0] + crop_img.shape[
                 1], start[1] + crop_img.shape[0]
             self.test_dataset[image_name].append(crop_rect)
     cvtools.save_json(self.test_dataset, save)
Esempio n. 11
0
 def crop_in_order_for_test(self, save, w=1920, h=1080, overlap=0.):
     assert 1920 >= w >= 800 and 1080 >= h >= 800 and 0.5 >= overlap >= 0.
     from collections import defaultdict
     imgs = cvtools.get_images_list(self.img_prefix)
     crop = CropInOder(width_size=w, height_size=h, overlap=overlap)
     self.test_dataset = defaultdict(list)
     for image_file in tqdm(imgs):
         if cvtools._DEBUG:
             print('crop {}'.format(image_file))
         image_name = osp.basename(image_file)
         img = cvtools.imread(image_file)  # support chinese
         if img is None:
             print('{} is None.'.format(image_file))
             continue
         crop_imgs, starts, _ = crop(img)
         for crop_img, start in zip(crop_imgs, starts):
             crop_rect = start[0], start[1], start[0] + crop_img.shape[
                 1], start[1] + crop_img.shape[0]
             self.test_dataset[image_name].append(crop_rect)
     cvtools.save_json(self.test_dataset, save)
Esempio n. 12
0
 def check_image(self, img_path):
     file = osp.join(self.root, img_path)
     if not cvtools.isfile_casesensitive(file):
         image_types = ['.jpg', '.png', '.jpeg', '.JPG', '.PNG', '.JPEG']
         for suffix in image_types:
             img_path = osp.splitext(img_path)[0] + suffix
             file = osp.join(self.root, img_path)
             if cvtools.isfile_casesensitive(file):
                 break
         if not cvtools.isfile_casesensitive(file):
             print("No images found in {}".format(osp.basename(img_path)))
             return None
     else:
         if self.read_test:
             try:
                 img = cvtools.imread(osp.join(self.root, img_path))
             except Exception as e:
                 print(e, 'filter images {}'.format(img_path))
                 return None
             if img is None:
                 print('image {} is None'.format(img_path))
                 return None
     return img_path
Esempio n. 13
0
    def crop_for_test(self, w, h, save=None):
        imgs = cvtools.get_images_list(self.img_prefix)
        if cvtools._DEBUG:
            imgs = imgs[:10]
        self.test_dataset = defaultdict(list)
        for i, image_file in enumerate(imgs):
            image_name = osp.basename(image_file)
            img = cvtools.imread(image_file)  # support chinese
            if img is None:
                print('{} is None.'.format(image_file))
                continue
            print('crop image %d of %d: %s' %
                  (i, len(imgs), image_name))

            crop_imgs, starts = sliding_crop(img, w, h)

            for crop_img, start in zip(crop_imgs, starts):
                crop_rect = start[0], start[1], \
                            start[0]+crop_img.shape[1], start[1]+crop_img.shape[0]
                self.test_dataset[image_name].append(crop_rect)
        if save is not None:
            cvtools.save_json(self.test_dataset, save)
        return self.test_dataset