Esempio n. 1
0
def test_mirror():
    image_file = 'P2305'
    image = cv.imread('data/rscup/images/{}.png'.format(image_file))
    labels = cvtools.readlines(
        'data/rscup/labelTxt/{}.txt'.format(image_file))[4:]
    bboxes = [
        list(map(float,
                 line.split()[:8])) for line in labels if len(line) > 1
    ]
    draw_img = cvtools.draw_boxes_texts(image.copy(),
                                        bboxes,
                                        box_format='polygon')
    cvtools.imwrite(draw_img, 'out/{}.png'.format(image_file))
    mirror = cvtools.RandomMirror()

    h_image, h_bboxes = horizontal_mirror(image.copy(), np.array(bboxes))
    # h_image = np.ascontiguousarray(h_image)
    draw_img = cvtools.draw_boxes_texts(h_image,
                                        h_bboxes,
                                        box_format='polygon')
    print((h_image == draw_img).all())
    cvtools.imwrite(draw_img, 'out/h_{}.png'.format(image_file))

    v_image, v_bboxes = vertical_mirror(image.copy(), np.array(bboxes))
    draw_img = cvtools.draw_boxes_texts(v_image,
                                        v_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(draw_img, 'out/v_{}.png'.format(image_file))

    hv_image, hv_bboxes = mirror(image.copy(), bboxes)
    draw_img = cvtools.draw_boxes_texts(hv_image,
                                        hv_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(draw_img, 'out/hv_{}.png'.format(image_file))
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 add_coco_bbox(self,
                   bbox,
                   cat,
                   conf=1,
                   show_txt=True,
                   img_id='default',
                   coor_len=4):
     bbox = np.array(bbox, dtype=np.int32)
     # cat = (int(cat) + 1) % 80
     cat = int(cat)
     # print('cat', cat, self.names[cat])
     c = self.colors[cat][0][0].tolist()
     if self.theme == 'white':
         c = (255 - np.array(c)).tolist()
     txt = '{}{:.1f}'.format(self.names[cat], conf)
     font = cv2.FONT_HERSHEY_SIMPLEX
     cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
     if coor_len == 8:
         self.imgs[img_id] = cvtools.draw_boxes_texts(self.imgs[img_id],
                                                      [bbox], [txt],
                                                      box_format='polygon')
     elif coor_len == 4:
         cv2.rectangle(self.imgs[img_id], (bbox[0], bbox[1]),
                       (bbox[2], bbox[3]), c, 2)
         if show_txt:
             cv2.rectangle(self.imgs[img_id],
                           (bbox[0], bbox[1] - cat_size[1] - 2),
                           (bbox[0] + cat_size[0], bbox[1] - 2), c, -1)
             cv2.putText(self.imgs[img_id],
                         txt, (bbox[0], bbox[1] - 2),
                         font,
                         0.5, (0, 0, 0),
                         thickness=1,
                         lineType=cv2.LINE_AA)
Esempio n. 4
0
 def save_crop_labeltxt(self, image_name, img_to_objs, save_root):
     crops = []
     img_name_no_suffix, img_suffix = osp.splitext(image_name)
     for crop_i, img_coor in enumerate(img_to_objs):
         crop_objs = img_to_objs[img_coor]
         if len(crop_objs) == 0:
             continue
         # write crop results to txt
         txt_name = '_'.join([img_name_no_suffix] + [str(crop_i)] +
                             list(map(str, img_coor))) + '.txt'
         txt_content = ''
         for crop_obj in crop_objs:
             if len(crop_obj) == 0:
                 continue
             polygen = np.array(crop_obj['segmentation'][0]).reshape(-1, 2)
             polygen = polygen - np.array(img_coor[:2]).reshape(-1, 2)
             line = list(map(str, polygen.reshape(-1)))
             cat = self.COCO.cats[crop_obj['category_id']]['name']
             diffcult = str(crop_obj['difficult'])
             line.append(cat)
             line.append(diffcult)
             txt_content += ' '.join(line) + '\n'
         cvtools.strwrite(txt_content,
                          osp.join(save_root, 'labelTxt+crop', txt_name))
     if len(crops) > 0:
         draw_img = cvtools.draw_boxes_texts(img,
                                             crops,
                                             line_width=3,
                                             box_format='x1y1x2y2')
         cvtools.imwrite(
             draw_img,
             osp.join(save_root, 'images', img_name_no_suffix + '.jpg'))
Esempio n. 5
0
def test_mirror():
    image_file = 'P0641'
    image = cv.imread(
        osp.join(current_path, 'data/DOTA/images/{}.png'.format(image_file)))
    labels = cvtools.readlines(
        osp.join(current_path, 'data/DOTA/labelTxt/{}.txt'.format(image_file)))
    labels = labels[2:]
    bboxes = [
        list(map(float,
                 line.split()[:8])) for line in labels if len(line) > 1
    ]
    draw_img = cvtools.draw_boxes_texts(image.copy(),
                                        bboxes,
                                        box_format='polygon')
    cvtools.imwrite(draw_img,
                    osp.join(current_path, 'out', '{}.png'.format(image_file)))
    mirror = cvtools.RandomMirror()

    # 测试水平镜像
    h_image, h_bboxes = horizontal_mirror(image.copy(), np.array(bboxes))
    # h_image = np.ascontiguousarray(h_image)
    draw_img = cvtools.draw_boxes_texts(h_image,
                                        h_bboxes,
                                        box_format='polygon')
    # print((h_image == draw_img).all())
    cvtools.imwrite(
        draw_img, osp.join(current_path, 'out', 'h_{}.png'.format(image_file)))

    # 测试垂直镜像
    v_image, v_bboxes = vertical_mirror(image.copy(), np.array(bboxes))
    draw_img = cvtools.draw_boxes_texts(v_image,
                                        v_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(
        draw_img, osp.join(current_path, 'out', 'v_{}.png'.format(image_file)))

    # 测试水平、垂直镜像
    hv_image, hv_bboxes = mirror(image.copy(), bboxes)
    draw_img = cvtools.draw_boxes_texts(hv_image,
                                        hv_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(
        draw_img, osp.join(current_path, 'out',
                           'hv_{}.png'.format(image_file)))
Esempio n. 6
0
def test_RandomRotate():
    image_file = 'P2305'
    image = cv.imread('data/rscup/images/{}.png'.format(image_file))
    labels = cvtools.readlines('data/rscup/labelTxt/{}.txt'.format(image_file))[4:]
    bboxes = [list(map(float, line.split()[:8])) for line in labels if len(line) > 1]
    draw_img = cvtools.draw_boxes_texts(image.copy(), bboxes, box_format='polygon')
    cvtools.imwrite(draw_img, '{}.png'.format(image_file))
    rotate = cvtools.RandomRotate()

    rotate_image, rotate_bboxes = rotate(image, bboxes, rotate='rotate_90')
    draw_img = cvtools.draw_boxes_texts(rotate_image, rotate_bboxes, box_format='polygon')
    cvtools.imwrite(draw_img, '90_{}.png'.format(image_file))

    rotate_image, rotate_bboxes = rotate(image, bboxes, rotate='rotate_270')
    draw_img = cvtools.draw_boxes_texts(rotate_image, rotate_bboxes, box_format='polygon')
    cvtools.imwrite(draw_img, '270_{}.png'.format(image_file))

    rotate_image, rotate_bboxes = rotate(image, bboxes, rotate='rotate_180')
    draw_img = cvtools.draw_boxes_texts(rotate_image, rotate_bboxes, box_format='polygon')
    cvtools.imwrite(draw_img, '180_{}.png'.format(image_file))
Esempio n. 7
0
def test_RandomRotate():
    image_file = 'P0126'
    image = cv.imread(
        osp.join(current_path, 'data/DOTA/images/{}.png'.format(image_file)))
    labels = cvtools.readlines(
        osp.join(current_path, 'data/DOTA/labelTxt/{}.txt'.format(image_file)))
    labels = labels[2:]
    bboxes = [
        list(map(float,
                 line.split()[:8])) for line in labels if len(line) > 1
    ]
    draw_img = cvtools.draw_boxes_texts(image.copy(),
                                        bboxes,
                                        box_format='polygon')
    cvtools.imwrite(draw_img,
                    osp.join(current_path, 'out', '{}.png'.format(image_file)))
    rotate = cvtools.RandomRotate()

    rotate_image, rotate_bboxes = rotate(image, bboxes, rotate='rotate_90')
    draw_img = cvtools.draw_boxes_texts(rotate_image,
                                        rotate_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(
        draw_img, osp.join(current_path, 'out',
                           '90_{}.png'.format(image_file)))

    rotate_image, rotate_bboxes = rotate(image, bboxes, rotate='rotate_270')
    draw_img = cvtools.draw_boxes_texts(rotate_image,
                                        rotate_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(
        draw_img, osp.join(current_path, 'out',
                           '270_{}.png'.format(image_file)))

    rotate_image, rotate_bboxes = rotate(image, bboxes, rotate='rotate_180')
    draw_img = cvtools.draw_boxes_texts(rotate_image,
                                        rotate_bboxes,
                                        box_format='polygon')
    cvtools.imwrite(
        draw_img, osp.join(current_path, 'out',
                           '180_{}.png'.format(image_file)))
Esempio n. 8
0
 def vis_objs(self, img, objs, vis='bbox', box_format='x1y1wh'):
     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)
     return img
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 = cv2.imdecode(np.fromfile(image_file, dtype=np.uint8),
                                cv2.IMREAD_COLOR)  # support chinese
             # img = cv2.imread(image_file)  # not 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 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. 11
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. 12
0
 def vis(self, img, bbox_result, vis_thr=0.5,
         to_file='vis.jpg'):
     bboxes = np.vstack(bbox_result)
     labels = [
         np.full(bbox.shape[0], i, dtype=np.int32)
         for i, bbox in enumerate(bbox_result)
     ]
     labels = np.concatenate(labels)
     inds = np.where(bboxes[:, -1] > vis_thr)[0]
     bboxes = bboxes[inds]
     labels = labels[inds]
     texts = [self.model.CLASSES[index]+'|'+str(round(bbox[-1], 2))
              for index, bbox in zip(labels, bboxes)]
     img = cvtools.draw_boxes_texts(
         img, bboxes[:, :-1], box_format='polygon', line_width=2)
     cvtools.imwrite(img, to_file)
Esempio n. 13
0
def vis_dets(ret, img_id, dataset, save_by_cat=False, split='val'):
    if split != 'test':
        img_info = dataset.coco.loadImgs([img_id])[0]
        ann_ids = dataset.coco.getAnnIds(imgIds=[img_id])
        anns = dataset.coco.loadAnns(ids=ann_ids)
        file_name = img_info['file_name']
    else:
        img_info = dataset.images(img_id)
        file_name = img_info['filename']
    colors = []
    img = read_dota_image(img_info, split)
    for cls_id, det_cls in ret.items():
        if len(det_cls) == 0: continue
        det_cls = det_cls[det_cls[:, -1] > 0.1]
        if len(det_cls) == 0: continue
        ids = poly_nms.poly_gpu_nms(det_cls.astype(np.float32), 0.15)
        det_cls = det_cls[ids]
        text = [
            dataset.class_name[cls_id] + str(round(score, 2))
            for score in det_cls[..., -1]
        ]
        img = cvtools.draw_boxes_texts(img,
                                       det_cls[:, :-1],
                                       texts=None,
                                       line_width=2,
                                       colors=[dataset.voc_color[cls_id - 1]] *
                                       len(det_cls),
                                       box_format="polygon")
    crop_str = list(map(str, img_info['crop']))
    filename = osp.splitext(file_name)[0]
    suffix = osp.splitext(file_name)[1]
    save_img_name = osp.join('_'.join([filename] + crop_str) + suffix)
    if save_by_cat and split != 'test':
        cats = {ann['category_id'] for ann in anns}
        for cat in cats:
            cat_name = dataset.coco.cats[cat]['name']
            file = osp.join(opt.debug_dir, cat_name, save_img_name)
            cvtools.imwrite(img, file)
    else:
        file = osp.join(opt.debug_dir, save_img_name)
        cvtools.imwrite(img, file)
Esempio n. 14
0
 def add_bbox_for_paper(self, bbox, cat, img_id='default', coor_len=4):
     bbox = np.array(bbox, dtype=np.int32)
     # cat = (int(cat) + 1) % 80
     cat = int(cat)
     # print('cat', cat, self.names[cat])
     # c = self.colors[cat][0][0].tolist()
     colors = [[249, 47, 47], [53, 253, 8], [7, 233, 252], [235, 8, 249],
               [8, 22, 249], [194, 249, 8], [249, 95, 8], [33, 8, 249],
               [249, 180, 8], [213, 249, 8], [8, 249, 172], [8, 114, 249],
               [8, 114, 249], [8, 114, 249], [8, 114, 249]]
     c = colors[cat][::-1]
     # if self.theme == 'white':
     #     c = (255 - np.array(c)).tolist()
     if coor_len == 8:
         self.imgs[img_id] = cvtools.draw_boxes_texts(self.imgs[img_id],
                                                      [bbox],
                                                      colors=[c],
                                                      line_width=2,
                                                      box_format='polygon')
     elif coor_len == 4:
         cv2.rectangle(self.imgs[img_id], (bbox[0], bbox[1]),
                       (bbox[2], bbox[3]), c, 2)
Esempio n. 15
0
    def _get_dota_item(self, index):
        img_id = self.images[index]
        img_info = self.coco.loadImgs(ids=[img_id])[0]
        file_name = img_info['file_name']

        filename = osp.splitext(file_name)[0]
        suffix = osp.splitext(file_name)[1]
        crop_str = list(map(str, img_info['crop']))
        crop_img_path = osp.join('/code/data/DOTA/crop800_80',
                                 '_'.join([filename] + crop_str) + suffix)
        if not osp.isfile(crop_img_path):
            img_path = os.path.join('/media/data/DOTA/trainval/images',
                                    file_name)
            img = cv2.imread(img_path)
            sx, sy, ex, ey = img_info['crop']
            img = img[sy:ey + 1, sx:ex + 1]
            cv2.imwrite(crop_img_path, img)
        else:
            img = cv2.imread(crop_img_path)

        ann_ids = self.coco.getAnnIds(imgIds=[img_id])
        # 如果不deep拷贝,修改了anns就修改了self.coco里的标签
        anns = copy.deepcopy(self.coco.loadAnns(ids=ann_ids))

        # if True:
        if self.opt.debug:
            segs = [ann['segmentation'][0] for ann in anns]
            cvtools.imwrite(
                cvtools.draw_boxes_texts(img.copy(),
                                         segs,
                                         box_format='polygon'),
                self.opt.debug_dir + '/{}'.format(file_name))

        if self.opt.flip:
            try:
                hv_flip = cvtools.RandomMirror(both=False)
                segs = [ann['segmentation'][0].copy() for ann in anns]
                for i, seg in enumerate(segs):
                    if len(seg) != 8:
                        segm_hull = cv2.convexHull(np.array(seg).reshape(
                            -1, 2).astype(np.float32),
                                                   clockwise=False)
                        xywha = cv2.minAreaRect(segm_hull)
                        segs[i] = cv2.boxPoints(xywha).reshape(-1).tolist()
                img, segs = hv_flip(img, segs)
                # if True:
                if self.opt.debug:
                    cvtools.imwrite(
                        cvtools.draw_boxes_texts(img.copy(),
                                                 segs,
                                                 box_format='polygon'),
                        self.opt.debug_dir + '/flip_{}'.format(file_name))
                for i in range(len(anns)):
                    anns[i]['segmentation'][0] = list(segs[i])
                    bbox = cv2.boundingRect(
                        np.array(segs[i], dtype=np.float32).reshape(-1, 2))
                    anns[i]['bbox'] = list(bbox)
            except Exception as e:
                print(e)
                return []

        if self.opt.rotate:
            rotate = cvtools.RandomRotate()
            segs = [ann['segmentation'][0].copy() for ann in anns]
            for i, seg in enumerate(segs):
                if len(seg) != 8:
                    segm_hull = cv2.convexHull(np.array(seg).reshape(
                        -1, 2).astype(np.float32),
                                               clockwise=False)
                    xywha = cv2.minAreaRect(segm_hull)
                    segs[i] = cv2.boxPoints(xywha).reshape(-1).tolist()
            img, segs = rotate(img, segs)
            # if True:
            if self.opt.debug:
                cvtools.imwrite(
                    cvtools.draw_boxes_texts(img.copy(),
                                             segs,
                                             box_format='polygon'),
                    self.opt.debug_dir + '/rotate_{}'.format(file_name))
            for i in range(len(anns)):
                anns[i]['segmentation'][0] = list(segs[i])
                bbox = cv2.boundingRect(
                    np.array(segs[i], dtype=np.float32).reshape(-1, 2))
                anns[i]['bbox'] = list(bbox)

        height, width = img.shape[0], img.shape[1]
        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
        # self.opt.input_h = self.opt.input_w = 32 * random.randint(12, 20)
        if self.opt.keep_res:
            input_h = (height | self.opt.pad) + 1
            input_w = (width | self.opt.pad) + 1
            s = np.array([input_w, input_h], dtype=np.float32)
        else:
            s = max(img.shape[0], img.shape[1]) * 1.0
            input_h, input_w = self.opt.input_h, self.opt.input_w

        # flipped = False
        if 'train' in self.split:
            if not self.opt.not_rand_crop:
                s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                w_border = self._get_border(128, img.shape[1])
                h_border = self._get_border(128, img.shape[0])
                c[0] = np.random.randint(low=w_border,
                                         high=img.shape[1] - w_border)
                c[1] = np.random.randint(low=h_border,
                                         high=img.shape[0] - h_border)
            else:
                sf = self.opt.scale
                cf = self.opt.shift
                c[0] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                c[1] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)

            # if np.random.random() < self.opt.flip:
            #     flipped = True
            #     img = img[:, ::-1, :]
            #     c[0] = width - c[0] - 1

        trans_input = get_affine_transform(c, s, 0, [input_w, input_h])
        inp = cv2.warpAffine(img,
                             trans_input, (input_w, input_h),
                             flags=cv2.INTER_LINEAR)

        gt_boxes = np.array(
            [cvtools.x1y1wh_to_x1y1x2y2(ann['bbox']) for ann in anns])
        img_box = cvtools.xywh_to_x1y1x2y2(np.array([[c[0], c[1], s, s]]))
        img_box[0, 0::2] = np.clip(img_box[0, 0::2], 0, width - 1)
        img_box[0, 1::2] = np.clip(img_box[0, 1::2], 0, height - 1)
        iofs = cvtools.bbox_overlaps(gt_boxes, img_box, mode='iof')
        ids = np.where(iofs > 0.7)[0]
        if len(ids) == 0: return []
        anns = [anns[ind] for ind in ids]

        # if True:
        if self.opt.debug:
            segs = [ann['segmentation'][0].copy()
                    for ann in anns]  # 复制一份,否则是原视图
            inp_draw = inp.copy()
            for k in range(len(segs)):
                seg = segs[k]
                for i in range(0, len(seg), 2):
                    seg[i:i + 2] = affine_transform(seg[i:i + 2], trans_input)
                    # seg[i] = np.clip(seg[i], 0, input_w - 1)
                    # seg[i + 1] = np.clip(seg[i + 1], 0, input_h - 1)
                segm_hull = cv2.convexHull(np.array(seg).reshape(-1, 2).astype(
                    np.float32),
                                           clockwise=False)
                xy, _, _ = cv2.minAreaRect(segm_hull)
                cv2.circle(inp_draw, (int(xy[0]), int(xy[1])),
                           radius=5,
                           color=(0, 0, 255),
                           thickness=-1)
            cvtools.imwrite(
                cvtools.draw_boxes_texts(inp_draw,
                                         segs,
                                         draw_start=False,
                                         box_format='polygon'),
                osp.join(self.opt.debug_dir, 'trans_' + file_name))

        inp = (inp.astype(np.float32) / 255.)
        if 'train' in self.split and not self.opt.no_color_aug:
            color_aug(self._data_rng, inp, self._eig_val, self._eig_vec)
        inp = (inp - self.mean) / self.std
        inp = inp.transpose(2, 0, 1)

        output_h = input_h // self.opt.down_ratio
        output_w = input_w // self.opt.down_ratio
        num_classes = self.num_classes
        trans_output = get_affine_transform(c, s, 0, [output_w, output_h])

        rets = []
        # out_size = []
        # for down_ratio in down_ratios:
        #     output_h = input_h // down_ratio
        #     output_w = input_w // down_ratio
        #     num_classes = self.num_classes
        #     out_size.append([output_w, output_h])
        #     # trans_output = get_affine_transform(c, s, 0, [output_w, output_h])
        #
        #     hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32)
        #     wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        #     if self.opt.a_method == 2:
        #         angle = np.full((self.max_objs, 1), 0.5, dtype=np.float32)
        #     else:
        #         angle = np.zeros((self.max_objs, 1), dtype=np.float32)
        #     dense_wh = np.zeros((2, output_h, output_w), dtype=np.float32)
        #     reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        #     ind = np.zeros((self.max_objs), dtype=np.int64)
        #     reg_mask = np.zeros((self.max_objs), dtype=np.uint8)
        #     cat_spec_wh = np.zeros((self.max_objs, num_classes * 2), dtype=np.float32)
        #     cat_spec_mask = np.zeros((self.max_objs, num_classes * 2), dtype=np.uint8)
        #
        #     # for k in range(num_objs):
        #     #     cls_id = int(self.cat_ids[anns[k]['category_id']])
        #     #     draw_heatmap(hm[cls_id], osp.join(self.opt.debug_dir, 'heatmap_' + str(cls_id) + '_' + file_name))
        #     ret = {'input': inp, 'hm': hm, 'reg_mask': reg_mask, 'ind': ind, 'wh': wh, 'a': angle}
        #     if self.opt.dense_wh:
        #         hm_a = hm.max(axis=0, keepdims=True)
        #         dense_wh_mask = np.concatenate([hm_a, hm_a], axis=0)
        #         ret.update({'dense_wh': dense_wh, 'dense_wh_mask': dense_wh_mask})
        #         del ret['wh']
        #     elif self.opt.cat_spec_wh:
        #         ret.update({'cat_spec_wh': cat_spec_wh, 'cat_spec_mask': cat_spec_mask})
        #         del ret['wh']
        #     if self.opt.reg_offset:
        #         ret.update({'reg': reg})
        #     # if self.opt.debug > 0 or not self.split == 'train':
        #     #     gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \
        #     #         np.zeros((1, 6), dtype=np.float32)
        #     #     meta = {'c': c, 's': s, 'gt_det': gt_det, 'img_id': img_id}
        #     #     ret['meta'] = meta
        #     rets.append(ret)
        #
        # draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \
        #     draw_umich_gaussian
        # if not self.opt.fpn:
        #     output_w, output_h = out_size[0]
        #     trans_output = get_affine_transform(c, s, 0, out_size[0])
        #
        # for k in range(num_objs):
        #     ann = anns[k]
        #     cls_id = int(self.cat_ids[ann['category_id']])
        #
        #     # 确定GT分配给哪个FPN层
        #     if self.opt.fpn:
        #         bbox = ann['bbox']
        #         fpn_k = int(math.log(224. / math.sqrt(bbox[2] * bbox[3]), 2))
        #         if fpn_k < 0:
        #             fpn_k = 0
        #         if fpn_k > 2:
        #             fpn_k = 2
        #         ret = rets[fpn_k]
        #         output_w, output_h = out_size[fpn_k]
        #         trans_output = get_affine_transform(c, s, 0, out_size[fpn_k])
        #
        #     segm = np.array(ann['segmentation'][0])
        #     # if flipped:
        #     #     for i in range(0, len(segm), 2):
        #     #         segm[i] = width - segm[i] - 1
        #     for i in range(0, len(segm), 2):
        #         segm[i:i + 2] = affine_transform(segm[i:i + 2], trans_output)
        #         segm[i] = np.clip(segm[i], 0, output_w - 1)
        #         segm[i + 1] = np.clip(segm[i + 1], 0, output_h - 1)
        #
        #     segm_hull = cv2.convexHull(segm.reshape(-1, 2).astype(np.float32),
        #                                clockwise=False)
        #     xy, (w, h), a = cv2.minAreaRect(segm_hull)
        #     hm = ret['hm']
        #     reg_mask = ret['reg_mask']
        #     ind = ret['ind']
        #     wh = ret['wh']
        #     angle = ret['a']
        #     if h > 0 and w > 0:
        #         a, w, h = convert_angle(a, w, h, self.opt.a_method)
        #         ct = np.array(xy, dtype=np.float32)
        #         ct_int = ct.astype(np.int32)
        #         radius = gaussian_radius((math.ceil(h), math.ceil(w)))
        #         radius = max(0, int(radius))
        #         radius = self.opt.hm_gauss if self.opt.mse_loss else radius
        #         # radius = np.array((h / 3., w / 3.), np.int32)
        #         draw_gaussian(hm[cls_id], ct_int, radius)
        #         wh[k] = 1. * w, 1. * h
        #         gt_a = a / 90.
        #         if self.opt.a_method == 2:
        #             gt_a = (a + 90.) / 180.
        #         angle[k] = gt_a
        #         ind[k] = ct_int[1] * output_w + ct_int[0]
        #         if 'reg' in ret:
        #             ret['reg'][k] = ct - ct_int
        #         reg_mask[k] = 1
        #         if 'cat_spec_wh' in ret:
        #             ret['cat_spec_wh'][k, cls_id * 2: cls_id * 2 + 2] = wh[k]
        #         if 'cat_spec_mask' in ret:
        #             ret['cat_spec_mask'][k, cls_id * 2: cls_id * 2 + 2] = 1
        #         if self.opt.dense_wh:
        #             draw_dense_reg(ret['dense_wh'], hm.max(axis=0), ct_int,
        #                            wh[k], radius)

        draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \
            draw_umich_gaussian

        hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        if self.opt.a_method == 2:
            angle = np.full((self.max_objs, 1), 0.5, dtype=np.float32)
        else:
            angle = np.zeros((self.max_objs, 1), dtype=np.float32)
        dense_wh = np.zeros((2, output_h, output_w), dtype=np.float32)
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)
        cat_spec_wh = np.zeros((self.max_objs, num_classes * 2),
                               dtype=np.float32)
        cat_spec_mask = np.zeros((self.max_objs, num_classes * 2),
                                 dtype=np.uint8)

        anns = re_anns(anns, trans_output, output_w, output_h)
        num_objs = min(len(anns), self.max_objs)

        # if True:
        if self.opt.debug:
            gt_img = cv2.warpAffine(img,
                                    trans_output, (output_w, output_h),
                                    flags=cv2.INTER_LINEAR)
            segs = [ann['segmentation'][0] for ann in anns]
            cvtools.imwrite(
                cvtools.draw_boxes_texts(gt_img,
                                         segs,
                                         draw_start=False,
                                         box_format='polygon'),
                osp.join(self.opt.debug_dir, 'gt_' + file_name))

        bad_num = 0
        gt_det = []
        for k in range(num_objs):
            ann = anns[k]
            cls_id = int(self.cat_ids[ann['category_id']])
            segm = np.array(ann['segmentation'][0])
            # if flipped:
            #     for i in range(0, len(segm), 2):
            #         segm[i] = width - segm[i] - 1
            # for i in range(0, len(segm), 2):
            #     segm[i:i + 2] = affine_transform(segm[i:i + 2], trans_output)
            #     segm[i] = np.clip(segm[i], 0, output_w - 1)
            #     segm[i + 1] = np.clip(segm[i + 1], 0, output_h - 1)

            segm_hull = cv2.convexHull(segm.reshape(-1, 2).astype(np.float32),
                                       clockwise=False)
            xy, (w, h), a = cv2.minAreaRect(segm_hull)
            if xy[0] > output_w or xy[0] < 0 or xy[1] > output_h or xy[1] < 0:
                # TODO:查明为何会出现这种情况。P0750
                # xy中y下出现负值或大于127
                # print(file_name, ann, segm, xy)
                bad_num += 1
                continue
            if h > 0 and w > 0:
                a, w, h = convert_angle(a, w, h, self.opt.a_method)
                ct = np.array(xy, dtype=np.float32)
                ct_int = ct.astype(np.int32)
                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = max(0, int(radius))
                radius = self.opt.hm_gauss if self.opt.mse_loss else radius
                # radius = np.array((h / 3., w / 3.), np.int32)
                draw_gaussian(hm[cls_id], ct_int, radius)
                wh[k] = 1. * w, 1. * h
                gt_a = a / 90.
                if self.opt.a_method == 2:
                    gt_a = (a + 90.) / 180.
                angle[k] = gt_a
                ind[k] = ct_int[1] * output_w + ct_int[0]
                reg[k] = ct - ct_int
                reg_mask[k] = 1
                cat_spec_wh[k, cls_id * 2:cls_id * 2 + 2] = wh[k]
                cat_spec_mask[k, cls_id * 2:cls_id * 2 + 2] = 1
                if self.opt.dense_wh:
                    draw_dense_reg(dense_wh, hm.max(axis=0), ct_int, wh[k],
                                   radius)
                gt_det.append(segm + [cls_id])
            else:
                bad_num += 1

        if bad_num == num_objs: return []
        ret = {
            'input': inp,
            'hm': hm,
            'reg_mask': reg_mask,
            'ind': ind,
            'wh': wh,
            'a': angle
        }
        if self.opt.dense_wh:
            hm_a = hm.max(axis=0, keepdims=True)
            dense_wh_mask = np.concatenate([hm_a, hm_a], axis=0)
            ret.update({'dense_wh': dense_wh, 'dense_wh_mask': dense_wh_mask})
            del ret['wh']
        elif self.opt.cat_spec_wh:
            ret.update({
                'cat_spec_wh': cat_spec_wh,
                'cat_spec_mask': cat_spec_mask
            })
            del ret['wh']
        if self.opt.reg_offset:
            ret.update({'reg': reg})
        if self.opt.debug > 0 or 'train' not in self.split:
            gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \
                np.zeros((1, 6), dtype=np.float32)
            meta = {'c': c, 's': s, 'gt_det': gt_det, 'img_id': img_id}
            ret['meta'] = meta
        rets.append(ret)
        return rets