imgaug_support.py 8.4 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
import numpy as np

#def execute_imgaug(augmenter, im, bboxes=None, polygons=None, segment_map=None):
#    import imgaug.augmentables.polys as polys
#    import imgaug.augmentables.bbs as bbs
#
#    # 预处理,将bboxes, polygons转换成imgaug格式
#    aug_bboxes = None
#    if bboxes is not None:
#        aug_bboxes = list()
#        for i in range(len(bboxes)):
#            x1 = bboxes[i, 0]-1
#            y1 = bboxes[i, 1]
#            x2 = bboxes[i, 2]
#            y2 = bboxes[i, 3]
#            aug_bboxes.append(bbs.BoundingBox(x1, y1, x2, y2))
#
#    aug_polygons = None
#    lod_info = list()
#    if polygons is not None:
#        aug_polygons = list()
#        for i in range(len(polygons)):
#            num = len(polygons[i])
#            lod_info.append(num)
#            for j in range(num):
#                points = np.reshape(polygons[i][j], (-1, 2))
#                aug_polygons.append(polys.Polygon(points))
#
#    aug_segment_map = None
#    if segment_map is not None:
#        if len(segment_map.shape) == 2:
#            h, w = segment_map.shape
#            aug_segment_map = np.reshape(segment_map, (1, h, w, 1))
#        elif len(segment_map.shape) == 3:
#            h, w, c = segment_map.shape
#            aug_segment_map = np.reshape(segment_map, (1, h, w, c))
#        else:
#            raise Exception("Only support 2-dimensions for 3-dimensions for segment_map")
#
#    aug_im = im.astype('uint8')
#    aug_im, aug_bboxes, aug_polygons, aug_seg_map = augmenter.augment(image=aug_im, bounding_boxes=aug_bboxes,
#                                                                  polygons=aug_polygons,
#                                                                  segmentation_maps=aug_segment_map)
#
#    if aug_polygons is not None:
#        assert len(aug_bboxes) == len(lod_info), "length of bboxes should be equal to the length of polygons"
#
#    if aug_bboxes is not None:
#        # 裁剪掉在图像之外的bbox和polygon
#        for i in range(len(aug_bboxes)):
#            aug_bboxes[i] = aug_bboxes[i].clip_out_of_image(aug_im)
#        if aug_polygons is not None:
#            for i in range(len(aug_polygons)):
#                aug_polygons[i] = aug_polygons[i].clip_out_of_image(aug_im)
#
#        # 过滤掉无效的bbox和polygon,并转换为训练数据格式
#        converted_bboxes = list()
#        converted_polygons = list()
#        poly_index = 0
#        for i in range(len(aug_bboxes)):
#            # 过滤width或height不足1像素的框
#            if aug_bboxes[i].width < 1 or aug_bboxes[i].height < 1:
#                continue
#            if aug_polygons is None:
#                converted_bboxes.append([aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2, aug_bboxes[i].y2])
#                continue
#
#            # 如若有polygons,将会继续执行下面代码
#            polygons_this_box = list()
#            for ps in aug_polygons[poly_index:poly_index+lod_info[i]]:
#                if len(ps) == 0:
#                    continue
#                for p in ps:
#                    # 没有3个point的polygon被过滤
#                    if len(p.exterior) < 3:
#                        continue
#                    polygons_this_box.append(p.exterior.flatten().tolist())
#            poly_index += lod_info[i]
#
#            if len(polygons_this_box) == 0:
#                continue
#            converted_bboxes.append([aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2, aug_bboxes[i].y2])
#            converted_polygons.append(polygons_this_box)
#        if len(converted_bboxes) == 0:
#			# 若完成增强后,没有可用的目标,则仍然返回原图进行训练
#            aug_im = im
#            converted_bboxes = bboxes
#            converted_polygons = polygons
#
#    result = [aug_im.astype('float32')]
#    if bboxes is not None:
#        result.append(np.array(converted_bboxes))
#    if polygons is not None:
#        result.append(converted_polygons)
#    if segment_map is not None:
#        n, h, w, c = aug_seg_map.shape
#        if len(segment_map.shape) == 2:
#            aug_seg_map = np.reshape(aug_seg_map, (h, w))
#        elif len(segment_map.shape) == 3:
#            aug_seg_map = np.reshape(aug_seg_map, (h, w, c))
#        result.append(aug_seg_map)
#    return result

import numpy as np


def execute_imgaug(augmenter, im, bboxes=None, polygons=None,
                   segment_map=None):
    # 预处理,将bboxes, polygons转换成imgaug格式
    import imgaug.augmentables.polys as polys
    import imgaug.augmentables.bbs as bbs

    aug_im = im.astype('uint8')

    aug_bboxes = None
    if bboxes is not None:
        aug_bboxes = list()
        for i in range(len(bboxes)):
            x1 = bboxes[i, 0] - 1
            y1 = bboxes[i, 1]
            x2 = bboxes[i, 2]
            y2 = bboxes[i, 3]
            aug_bboxes.append(bbs.BoundingBox(x1, y1, x2, y2))

    aug_polygons = None
    lod_info = list()
    if polygons is not None:
        aug_polygons = list()
        for i in range(len(polygons)):
            num = len(polygons[i])
            lod_info.append(num)
            for j in range(num):
                points = np.reshape(polygons[i][j], (-1, 2))
                aug_polygons.append(polys.Polygon(points))

    aug_segment_map = None
    if segment_map is not None:
        if len(segment_map.shape) == 2:
            h, w = segment_map.shape
            aug_segment_map = np.reshape(segment_map, (1, h, w, 1))
        elif len(segment_map.shape) == 3:
            h, w, c = segment_map.shape
            aug_segment_map = np.reshape(segment_map, (1, h, w, c))
        else:
            raise Exception(
                "Only support 2-dimensions for 3-dimensions for segment_map")

    aug_im, aug_bboxes, aug_polygons, aug_seg_map = augmenter.augment(
        image=aug_im,
        bounding_boxes=aug_bboxes,
        polygons=aug_polygons,
        segmentation_maps=aug_segment_map)

    aug_im = aug_im.astype('float32')

    if aug_polygons is not None:
        assert len(aug_bboxes) == len(
            lod_info
        ), "Number of aug_bboxes should be equal to number of aug_polygons"

    if aug_bboxes is not None:
        # 裁剪掉在图像之外的bbox和polygon
        for i in range(len(aug_bboxes)):
            aug_bboxes[i] = aug_bboxes[i].clip_out_of_image(aug_im)
        if aug_polygons is not None:
            for i in range(len(aug_polygons)):
                aug_polygons[i] = aug_polygons[i].clip_out_of_image(aug_im)

        # 过滤掉无效的bbox和polygon,并转换为训练数据格式
        converted_bboxes = list()
        converted_polygons = list()
        poly_index = 0
        for i in range(len(aug_bboxes)):
            # 过滤width或height不足1像素的框
            if aug_bboxes[i].width < 1 or aug_bboxes[i].height < 1:
                continue
            if aug_polygons is None:
                converted_bboxes.append([
                    aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2,
                    aug_bboxes[i].y2
                ])
                continue

            # 如若有polygons,将会继续执行下面代码
            polygons_this_box = list()
            for ps in aug_polygons[poly_index:poly_index + lod_info[i]]:
                if len(ps) == 0:
                    continue
                for p in ps:
                    # 没有3个point的polygon被过滤
                    if len(p.exterior) < 3:
                        continue
                    polygons_this_box.append(p.exterior.flatten().tolist())
            poly_index += lod_info[i]

            if len(polygons_this_box) == 0:
                continue
            converted_bboxes.append([
                aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2,
                aug_bboxes[i].y2
            ])
            converted_polygons.append(polygons_this_box)
        if len(converted_bboxes) == 0:
            aug_im = im
            converted_bboxes = bboxes
            converted_polygons = polygons

    result = [aug_im]
    if bboxes is not None:
        result.append(np.array(converted_bboxes))
    if polygons is not None:
        result.append(converted_polygons)
    if segment_map is not None:
        n, h, w, c = aug_seg_map.shape
        if len(segment_map.shape) == 2:
            aug_seg_map = np.reshape(aug_seg_map, (h, w))
        elif len(segment_map.shape) == 3:
            aug_seg_map = np.reshape(aug_seg_map, (h, w, c))
        result.append(aug_seg_map)
    return result