<jupyter_start><jupyter_text>generate no patch val data<jupyter_code>datadir = "/home/xfr/rssid/data/val/images"
labeldir = "/home/xfr/rssid/data/val/labelTxt"
generate_nopatch(datadir, labeldir, "val")
def _pygenerate_noobj(boxes, masks, labels, width, height, chipsize, stride):
        chips = []
        boxes = np.array(boxes)
        masks = np.array(masks)
        # ensure coverage of image for worst case
        # corners
        chips.append([max(width - chipsize, 0), 0, width - 1, min(chipsize, height-1)])
        chips.append([0, max(height - chipsize, 0), min(chipsize, width-1), height-1])
        chips.append([max(width - chipsize, 0), max(height - chipsize, 0), width-1, height-1])
		
        for i in range(0, width - int(chipsize), stride):
            for j in range(0, height - int(chipsize), stride):
                x1 = i
                y1 = j
                x2 = i + chipsize - 1
                y2 = j + chipsize - 1
                chips.append([x1, y1, x2, y2])
		#width may not be divide by stride
        for j in range(0, height - int(chipsize), stride):
            x1 = max(width - chipsize - 1,0)
            y1 = j
            x2 = width - 1
            y2 = j + chipsize - 1
            chips.append([x1, y1, x2, y2])
		#the same as above
        for i in range(0, width - int(chipsize), stride):
            x1 = i
            y1 = max(height - chipsize - 1,0)
            x2 = i + chipsize - 1
            y2 = height - 1
            chips.append([x1, y1, x2, y2])

        chips = np.array(chips).astype(np.float)
        overlaps = bbox_overlaps_py(chips, boxes.astype(np.float))
        fchips = []
        masks_list = []
        boxes_list = []
        labels_list = []
        for j in range(len(chips)):
            nvids = np.where(overlaps[j, :] >= 0.7)[0]
            if(len(nvids) == 0):
                fchips.append(chips[j])
        return fchips
    
def scale_generate_noobj(datdir, labeldir, phase, SCALE):
    imgs = os.listdir(datadir)
    for img_id in tqdm(range(len(imgs))):
        img_path = os.path.join(datadir, imgs[img_id])
        label_path = os.path.join(labeldir, imgs[img_id].split(".")[0]+".txt")
        original_img = cv2.imread(img_path)
        h, w, _ = original_img.shape
        scales = copy.deepcopy(SCALE)
        fp = open(label_path).readlines()[2:]
        for scale in scales:
            img, scale_factor = mmcv.imrescale(original_img, scale, return_scale=True)
            H,W,_ = img.shape
            labels, masks, boxes = get_scale_info(fp, scale_factor)
            if(len(labels)<=0):
                continue
            if max(H,W) >= 512:
                fchips = _pygenerate_noobj(boxes, masks, labels, W, H, 512, 416)
                count = 0
                for chip in fchips:
                    count+=1
                    xmin, ymin, xmax, ymax = chip
                    filename = imgs[img_id].split(".")[0] + "_{}_{}_{}_".format(str(scale), str(xmin), str(ymin))+"part" +str(count) + ".jpg"
                    img2 = copy.deepcopy(img[int(ymin):int(ymax), int(xmin):int(xmax),:])
                    cv2.imwrite("./{}/".format(phase)+filename, img2)
datadir = "/home/xfr/rssid/data/train/images"
labeldir = "/home/xfr/rssid/data/train/labelTxt"
scale_generate_noobj(datadir, labeldir, "pre", [0.5, 1])<jupyter_output><empty_output><jupyter_text>aug special class<jupyter_code>CLASS=['tennis-court', 'container-crane', 'storage-tank', 'baseball-diamond', 'plane', 'ground-track-field', 'helicopter', 'airport', 'harbor', 'ship', 'large-vehicle', 'swimming-pool', 'soccer-ball-field', 'roundabout', 'basketball-court', 'bridge', 'small-vehicle', 'helipad','noise']
coco=COCO("/home/xfr/rssid/data/annotation/annos_rscup_train.json")
import mmcv
ret = {x:[] for x in CLASS}
imgIds = coco.getImgIds()
info = {}
for imgid in tqdm(imgIds):
    img = coco.loadImgs(imgid)[0]
    origin_img = cv2.imread("/home/xfr/rssid/data/train/images/"+img['file_name'])
    origin_name = img['file_name'].split(".")[0]
    annIds = coco.getAnnIds(imgIds=[imgid], iscrowd=None)
    anns = coco.loadAnns(annIds)
    count = 0
    for ann in anns:
        cls = CLASS[ann['category_id']]
        if(cls == "airport"):
            file_name ="{}_count_{}".format(origin_name, count)+".jpg"
            count += 1
            info[file_name] = {}
            xmin, ymin, w, h = ann["bbox"]
            poly = np.array(ann["segmentation"][0]).reshape(4,2)
            poly -= np.array([xmin, ymin])
            expand_factor = min(0.15, xmin/w, ymin/h)
            poly += np.array([expand_factor*w, expand_factor*h])
            xmin = xmin-expand_factor*w
            ymin = ymin- expand_factor*h
            xmax = xmin+(1+expand_factor*2)*w
            ymax = ymin+(1+expand_factor*2)*h
            info[file_name]["bbox"] = [0, 0, xmax-xmin, ymax - ymin]
            sub_img = origin_img[int(ymin):int(ymax), int(xmin):int(xmax),:]
            info[file_name]["seg"] = poly
            cv2.imwrite("/home/xfr/rssid/airport/"+file_name, sub_img)
mmcv.dump(info,"/home/xfr/rssid/info.pkl")

def aug_roi(img):
    aug = iaa.SomeOf(1, [
    iaa.AdditiveGaussianNoise(scale=0.1*255),
    iaa.Sharpen(alpha=0.5),
    iaa.Superpixels(p_replace=0.2, n_segments=256),
    iaa.GammaContrast((0.5, 1.5)),])
    img = aug.augment_image(img)
    return img


def RandomRotate(img, polys, boxes):
    degree_range = 180
    xmin, ymin, w, h = boxes
    xmax = xmin+w
    ymax = ymin+h
    if random.random() < 1:
        box = np.array([[xmin, ymin], [xmin,ymax],[xmax, ymax],[xmax,ymin]], np.float64)
        degree = (random.random()*2-1) * degree_range
        np_degree = -degree * np.pi /180
        h, w, _ = img.shape
        rotation_matrix = np.array([[np.cos(np_degree), np.sin(np_degree)],[-np.sin(np_degree), np.cos(np_degree)]])
        polys[ :, 0] -= w/2
        polys[ :, 1] -= h/2
        polys = np.dot(polys, rotation_matrix)
        
        box[ :, 0] -= w/2
        box[ :, 1] -= h/2
        box = np.dot(box, rotation_matrix)

        img = Image.fromarray(img)
        img = img.rotate(degree, Image.NEAREST, expand=1)
        img = np.array(img)
        h, w, _ = img.shape
        
        
        polys[:, 0] += w / 2
        polys[:, 1] += h / 2
        
        box[:, 0] += w / 2
        box[:, 1] += h / 2
        
        mask = np.zeros((h, w, 3))
        cv2.fillPoly(mask, np.array([box], np.int32),(1,1,1) )
        
        #cv2.fillPoly(mask, np.array([box], np.int32),(1,1,1) )
        
        xmin = np.min(polys[:, 0], axis=0)
        xmax = np.max(polys[:, 0], axis=0)
        ymin = np.min(polys[:, 1], axis=0)
        ymax = np.max(polys[:, 1], axis=0)

        boxes = [xmin, ymin, xmax-xmin, ymax-ymin]
        # import matplotlib.pyplot as plt
        # plt.imshow(img[:,:,::-1])
        # plt.show()
    else:
        box = np.array([[xmin, ymin], [xmin,ymax],[xmax, ymax],[xmax,ymin]], np.float64)
        mask = np.zeros((h, w, 3))
        cv2.fillPoly(mask, np.array([box], np.int32),(1,1,1) )
    return img, polys, boxes, mask


def merge(materials, objects, info):
    source_img = np.random.choice(materials, 1)[0]
    source_path = source + source_img
    back = cv2.imread(source_path)
    target = back.copy()
    H, W,_ = back.shape
    area = random.uniform(0.1, 0.5)*(H*W)
    des_img = np.random.choice(objects, 1)[0]
    des_path = des+des_img
    obj = cv2.imread(des_path)
    obj = aug_roi(obj)
    h,w,_ = obj.shape
    poly = copy.deepcopy(info[des_img]["seg"])
    obj_w = w
    obj_h = h
    img, poly, boxes, mask = RandomRotate(obj, poly, [0,0,obj_w,obj_h])
    h, w,_ = img.shape
    max_factor = min((H-2)/h, (W-2)/w)
    scale_factor = np.sqrt(area/(h*w))
    scale_factor = min(max_factor, scale_factor)
    img, scale_factor = mmcv.imrescale(img, scale_factor, return_scale=True)
    mask = mmcv.imrescale(mask, scale_factor)
    h, w,_ = img.shape
    poly*= scale_factor
    locx = np.random.randint(0, (W-w))
    locy = np.random.randint(0, (H-h))
    poly += np.array([locx, locy])
    box = np.array([locx, locy, w, h])
    center = (int(locx+w/2),int(locy+h/2))
    back[locy:locy+h, locx:locx+w, :] = back[locy:locy+h, locx:locx+w, :]* (1-mask) + img*mask
#     cv2.rectangle(back, (locx,locy),(int(locx+w), int(locy+h)),(0,0,255),2)
#     cv2.polylines(back, np.array([poly], np.int32), 1, (0, 0, 255), 3)
#     cv2.fillPoly(back, np.array([poly], np.int32),(0.,255,0) )
#     plt.imshow(back)
#     plt.show()
    
    return back, box, poly
import matplotlib.pyplot as plt
import mmcv
import os
from imgaug import augmenters as iaa




source = "/home/xfr/rssid/pre/"
des = "/home/xfr/rssid/airport/"
info = mmcv.load("/home/xfr/rssid/info.pkl")
materials = os.listdir("/home/xfr/rssid/pre")
objects = list(info.keys())
info2 = {
    "description": "rscup",
    "url": "http://cocodataset.org",
    "version": "1.0",
    "year": 2014,
    "contributor": "COCO Consortium",
    "date_created": "2017/09/01"
  }
license = [{ "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/", "id": 1, "name": "Attribution-NonCommercial-ShareAlike License"} ]
categories = []
for cls in CLASS:
    category = { "id" : class_to_ind[cls], "name" : cls, "supercategory" : "object",}
    categories.append(category)
print(categories)
annotations = {"info": info2, "images": [], "annotations": [], "categories":categories, "license":license}
obj_id = 0
ID = 0
for i in tqdm(range(2000)):
    img, box, poly = merge(materials, objects, info)
    xmin, ymin, w, h = box
    area = cv2.contourArea(np.array(np.array([poly], np.int32)))
    poly = poly.flatten()
    H, W, _, = img.shape
    filename = "{}.jpg".format(ID)
    image = { "license": 1,
                  "file_name": filename,
                  "coco_url": "xxx",
                  "height": H,
                  "width": W,
                  "date_captured": "2019-06-25",
                  "flickr_url": "xxx",
                  "id": ID
                }

    cv2.imwrite("./rscup/aug/airport/"+filename, img)
    annotations["images"].append(image)
    annotation = {"id" : obj_id, 
                "image_id" : ID, 
                "category_id" : class_to_ind["airport"], 
                "segmentation" : [poly], 
               "area" :  area, 
               "bbox" : [xmin, ymin, w, h], 
               "iscrowd" : 0,
            }
    obj_id += 1
    annotations["annotations"].append(annotation)
    ID += 1
with open('./rscup/aug/annos_rscup_airport'+'.json', 'w') as json_file:
    json.dump(annotations, json_file, cls=MyEncoder)
# get all images containing given categories, select one at random
import sys
sys.path.append('D:\API\cocoapi\PythonAPI')  # 你下载的 cocoapi 所在路径
import skimage.io as io
from pycocotools.coco import COCO   # 载入 cocoz
%matplotlib inline
coco=COCO("./aug/annos_rscup_airport.json")
catIds = coco.getCatIds(catNms=['airport'])
print(catIds)
imgIds = coco.getImgIds(catIds=catIds)
# imgIds = coco.getImgIds(imgIds=[335328])
img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
I = io.imread('./aug/airport/'+img['file_name'])
plt.imshow(I)
plt.axis('off')
print(img)
annIds = coco.getAnnIds(imgIds=[img['id']], iscrowd=None)
print(len(annIds))
anns = coco.loadAnns(annIds)
print(anns)
for ann in anns:
    bbox = ann['bbox']
    poly = np.array(ann["segmentation"][0]).reshape(4,2)
    x = [int(x) for x in bbox]
    cv2.rectangle(I, (x[0],x[1]), (x[0]+x[2],x[1]+x[3]), (0,255,0), 2)
    cv2.polylines(I, np.array([poly], np.int32), 1, (0, 0, 255), 3)
    #cv2.fillPoly(I, np.array([poly], np.int32),(0.,255,0) )
    cv2.putText(I, CLASS[ann['category_id']], (x[0],x[1]), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
plt.imshow(I)
#coco.showAnns(anns)<jupyter_output>loading annotations into memory...
Done (t=0.02s)
creating index...
index created!
[7]
{'license': 1, 'file_name': '665.jpg', 'coco_url': 'xxx', 'height': 511, 'width': 511, 'date_captured': '2019-06-25', 'flickr_url': 'xxx', 'id': 665}
1
[{'id': 665, 'image_id': 665, 'category_id': 7, 'segmentation': [[178.5214504197791, 135.15255017405934, 253.62007603690262, 390.19982049556205, 137.4237551740976, 424.4138075245667, 62.32512955697413, 169.36653720306407]], 'area': 32130.0, 'bbox': [34, 93, 248, 374], 'iscrowd': 0}]<jupyter_text>aug helipad<jupyter_code>CLASS=['tennis-court', 'container-crane', 'storage-tank', 'baseball-diamond', 'plane', 'ground-track-field', 'helicopter', 'airport', 'harbor', 'ship', 'large-vehicle', 'swimming-pool', 'soccer-ball-field', 'roundabout', 'basketball-court', 'bridge', 'small-vehicle', 'helipad']
coco=COCO("/home/xfr/rssid/data/annotation/annos_rscup_train.json")
import mmcv
ret = {x:[] for x in CLASS}
imgIds = coco.getImgIds()
info = {}
for imgid in tqdm(imgIds):
    img = coco.loadImgs(imgid)[0]
    origin_img = cv2.imread("/home/xfr/rssid/data/train/images/"+img['file_name'])
    origin_name = img['file_name'].split(".")[0]
    annIds = coco.getAnnIds(imgIds=[imgid], iscrowd=None)
    anns = coco.loadAnns(annIds)
    count = 0
    for ann in anns:
        cls = CLASS[ann['category_id']]
        if(cls == "helipad"):
            file_name ="{}_count_{}".format(origin_name, count)+".jpg"
            count += 1
            info[file_name] = {}
            xmin, ymin, w, h = ann["bbox"]
            poly = np.array(ann["segmentation"][0]).reshape(4,2)
            poly -= np.array([xmin, ymin])
            expand_factor = min(0.15, xmin/w, ymin/h)
            poly += np.array([expand_factor*w, expand_factor*h])
            xmin = xmin-expand_factor*w
            ymin = ymin- expand_factor*h
            xmax = xmin+(1+expand_factor*2)*w
            ymax = ymin+(1+expand_factor*2)*h
            info[file_name]["bbox"] = [0, 0, xmax-xmin, ymax - ymin]
            sub_img = origin_img[int(ymin):int(ymax), int(xmin):int(xmax),:]
            info[file_name]["seg"] = poly
            cv2.imwrite("/home/xfr/rssid/helipad/"+file_name, sub_img)
mmcv.dump(info,"/home/xfr/rssid/helipad.pkl")
def merge_helipad(materials, objects, info):
    source_img = np.random.choice(materials, 1)[0]
    source_path = source + source_img
    back = cv2.imread(source_path)
    target = back.copy()
    H, W,_ = back.shape
    poly_list = []
    box_list = []
    for i in range(np.random.randint(1,5)):
        area = random.uniform(0.005, 0.02)*(H*W)
        des_img = np.random.choice(objects, 1)[0]
        des_path = des+des_img
        obj = cv2.imread(des_path)
        obj = aug_roi(obj)
        h,w,_ = obj.shape
        poly = copy.deepcopy(info[des_img]["seg"])
        obj_w = w
        obj_h = h
        img, poly, boxes, mask = RandomRotate(obj, poly, [0,0,obj_w,obj_h])
        h, w,_ = img.shape
        max_factor = min((H-2)/h, (W-2)/w)
        scale_factor = np.sqrt(area/(h*w))
        scale_factor = min(max_factor, scale_factor)
        img, scale_factor = mmcv.imrescale(img, scale_factor, return_scale=True)
        mask = mmcv.imrescale(mask, scale_factor)
        h, w,_ = img.shape
        poly*= scale_factor
        locx = np.random.randint(0, (W-w))
        locy = np.random.randint(0, (H-h))
        poly += np.array([locx, locy])
        box = np.array([locx, locy, w, h])
        center = (int(locx+w/2),int(locy+h/2))
        back[locy:locy+h, locx:locx+w, :] = back[locy:locy+h, locx:locx+w, :]* (1-mask) + img*mask
        poly_list.append(poly)
        box_list.append(box)
#     for box, poly in zip(box_list, poly_list):
#         cv2.rectangle(back, (box[0],box[1]),(int(box[0]+box[2]), int(box[1]+box[3])),(0,0,255),2)
#         cv2.polylines(back, np.array([poly], np.int32), 1, (0, 0, 255), 3)
#     #cv2.fillPoly(back, np.array([poly], np.int32),(0.,255,0) )
#     plt.imshow(back)
#     plt.show()
    
    return back, box_list, poly_list

classname = "helipad"
source = "/home/xfr/rssid/pre/"
des = "/home/xfr/rssid/{}/".format(classname)
info = mmcv.load("/home/xfr/rssid/{}.pkl".format(classname))
materials = os.listdir("/home/xfr/rssid/pre")
objects = list(info.keys())

info2 = {
    "description": "rscup",
    "url": "http://cocodataset.org",
    "version": "1.0",
    "year": 2014,
    "contributor": "COCO Consortium",
    "date_created": "2017/09/01"
  }
license = [{ "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/", "id": 1, "name": "Attribution-NonCommercial-ShareAlike License"} ]
categories = []
for cls in CLASS:
    category = { "id" : class_to_ind[cls], "name" : cls, "supercategory" : "object",}
    categories.append(category)
print(categories)
annotations = {"info": info2, "images": [], "annotations": [], "categories":categories, "license":license}
obj_id = 0
ID = 0
for i in tqdm(range(1500)):
    img, box_list, poly_list = merge_helipad(materials, objects, info)
    H, W, _, = img.shape
    filename = "{}.jpg".format(ID)
    image = { "license": 1,
                  "file_name": filename,
                  "coco_url": "xxx",
                  "height": H,
                  "width": W,
                  "date_captured": "2019-06-25",
                  "flickr_url": "xxx",
                  "id": ID
                }
   
    annotations["images"].append(image)
    cv2.imwrite("./rscup/aug/{}/".format(classname)+filename, img)
    for box, poly in zip(box_list, poly_list):
        xmin, ymin, w, h = box
        area = cv2.contourArea(np.array(np.array([poly], np.int32)))
        poly = poly.flatten()
        annotation = {"id" : obj_id, 
                    "image_id" : ID, 
                    "category_id" : class_to_ind[classname], 
                    "segmentation" : [poly], 
                   "area" :  area, 
                   "bbox" : [xmin, ymin, w, h], 
                   "iscrowd" : 0,
                }
        obj_id += 1
        annotations["annotations"].append(annotation)
    ID += 1
with open('./rscup/aug/annos_rscup_{}'.format(classname)+'.json', 'w') as json_file:
    json.dump(annotations, json_file, cls=MyEncoder)
# get all images containing given categories, select one at random
import sys
sys.path.append('D:\API\cocoapi\PythonAPI')  # 你下载的 cocoapi 所在路径
import skimage.io as io
from pycocotools.coco import COCO   # 载入 cocoz
%matplotlib inline
coco=COCO("./rscup/aug/annos_rscup_container-crane.json")
catIds = coco.getCatIds(catNms=['container-crane'])
print(catIds)
imgIds = coco.getImgIds(catIds=catIds)
# imgIds = coco.getImgIds(imgIds=[335328])
img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
I = io.imread('./rscup/aug/container-crane/'+img['file_name'])
plt.imshow(I)
plt.axis('off')
print(img)
annIds = coco.getAnnIds(imgIds=[img['id']], iscrowd=None)
print(len(annIds))
anns = coco.loadAnns(annIds)
print(anns)
for ann in anns:
    bbox = ann['bbox']
    poly = np.array(ann["segmentation"][0]).reshape(4,2)
    x = [int(x) for x in bbox]
    cv2.rectangle(I, (x[0],x[1]), (x[0]+x[2],x[1]+x[3]), (0,255,0), 2)
    cv2.polylines(I, np.array([poly], np.int32), 1, (0, 0, 255), 3)
    #cv2.fillPoly(I, np.array([poly], np.int32),(0.,255,0) )
    cv2.putText(I, CLASS[ann['category_id']], (x[0],x[1]), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
plt.imshow(I)
#coco.showAnns(anns)<jupyter_output>loading annotations into memory...
Done (t=0.01s)
creating index...
index created!
[1]
{'license': 1, 'file_name': '490.jpg', 'coco_url': 'xxx', 'height': 511, 'width': 511, 'date_captured': '2019-06-25', 'flickr_url': 'xxx', 'id': 490}
1
[{'id': 726, 'image_id': 490, 'category_id': 1, 'segmentation': [[325.2679851591494, 259.47834509348445, 313.9845905060107, 254.1936890796159, 342.0928922883434, 206.31997525711682, 350.61797467334617, 211.46592568704423]], 'area': 615.0, 'bbox': [298, 197, 68, 73], 'iscrowd': 0}]<jupyter_text>aug container crane<jupyter_code>def generate_objects(classname):
    CLASS=['tennis-court', 'container-crane', 'storage-tank', 'baseball-diamond', 'plane', 'ground-track-field', 'helicopter', 'airport', 'harbor', 'ship', 'large-vehicle', 'swimming-pool', 'soccer-ball-field', 'roundabout', 'basketball-court', 'bridge', 'small-vehicle', 'helipad']
    coco=COCO("/home/xfr/rssid/data/annotation/annos_rscup_train.json")
    save_dir = "/home/xfr/rssid/{}/".format(classname)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    imgIds = coco.getImgIds()
    info = {}
    for imgid in tqdm(imgIds):
        img = coco.loadImgs(imgid)[0]
        origin_img = cv2.imread("/home/xfr/rssid/data/train/images/"+img['file_name'])
        origin_name = img['file_name'].split(".")[0]
        annIds = coco.getAnnIds(imgIds=[imgid], iscrowd=None)
        anns = coco.loadAnns(annIds)
        count = 0
        for ann in anns:
            cls = CLASS[ann['category_id']]
            if(cls == classname):
                file_name ="{}_count_{}".format(origin_name, count)+".jpg"
                count += 1
                info[file_name] = {}
                xmin, ymin, w, h = ann["bbox"]
                poly = np.array(ann["segmentation"][0]).reshape(4,2)
                poly -= np.array([xmin, ymin])
                expand_factor = min(0.15, xmin/w, ymin/h)
                poly += np.array([expand_factor*w, expand_factor*h])
                xmin = xmin-expand_factor*w
                ymin = ymin- expand_factor*h
                xmax = xmin+(1+expand_factor*2)*w
                ymax = ymin+(1+expand_factor*2)*h
                info[file_name]["bbox"] = [0, 0, xmax-xmin, ymax - ymin]
                sub_img = origin_img[int(ymin):int(ymax), int(xmin):int(xmax),:]
                info[file_name]["seg"] = poly
                cv2.imwrite("/home/xfr/rssid/{}/".format(classname)+file_name, sub_img)
    mmcv.dump(info,"/home/xfr/rssid/{}.pkl".format(classname))
generate_objects("container-crane")
def merge_object_background(materials, objects, info, source, des):
    source_img = np.random.choice(materials, 1)[0]
    source_path = source + source_img
    back = cv2.imread(source_path)
    target = back.copy()
    H, W,_ = back.shape
    poly_list = []
    box_list = []
    for i in range(np.random.randint(1,3)):
        area = random.uniform(0.01, 0.03)*(H*W)
        des_img = np.random.choice(objects, 1)[0]
        des_path = des+des_img
        print(des_path)
        obj = cv2.imread(des_path)
        obj = aug_roi(obj)
        h,w,_ = obj.shape
        poly = copy.deepcopy(info[des_img]["seg"])
        obj_w = w
        obj_h = h
        img, poly, boxes, mask = RandomRotate(obj, poly, [0,0,obj_w,obj_h])
        h, w,_ = img.shape
        max_factor = min((H-2)/h, (W-2)/w)
        scale_factor = np.sqrt(area/(h*w))
        scale_factor = min(max_factor, scale_factor)
        img, scale_factor = mmcv.imrescale(img, scale_factor, return_scale=True)
        mask = mmcv.imrescale(mask, scale_factor)
        h, w,_ = img.shape
        poly*= scale_factor
        locx = np.random.randint(0, (W-w))
        locy = np.random.randint(0, (H-h))
        poly += np.array([locx, locy])
        box = np.array([locx, locy, w, h])
        center = (int(locx+w/2),int(locy+h/2))
        back[locy:locy+h, locx:locx+w, :] = back[locy:locy+h, locx:locx+w, :]* (1-mask) + img*mask
        poly_list.append(poly)
        box_list.append(box)
    for box, poly in zip(box_list, poly_list):
        cv2.rectangle(back, (box[0],box[1]),(int(box[0]+box[2]), int(box[1]+box[3])),(0,0,255),2)
        cv2.polylines(back, np.array([poly], np.int32), 1, (0, 0, 255), 3)
    #cv2.fillPoly(back, np.array([poly], np.int32),(0.,255,0) )
#     plt.imshow(back)
#     plt.show()
    
    return back, box_list, poly_list

def aug_with_background(classname):
    if not os.path.exists("./rscup/aug/{}/".format(classname)):
        os.makedirs("./rscup/aug/{}/".format(classname))
    source = "/home/xfr/rssid/pre/"
    des = "/home/xfr/rssid/{}/".format(classname)
    info = mmcv.load("/home/xfr/rssid/{}.pkl".format(classname))
    materials = os.listdir("/home/xfr/rssid/pre")
    objects = list(info.keys())

    info2 = {
        "description": "rscup",
        "url": "http://cocodataset.org",
        "version": "1.0",
        "year": 2014,
        "contributor": "COCO Consortium",
        "date_created": "2017/09/01"
      }
    license = [{ "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/", "id": 1, "name": "Attribution-NonCommercial-ShareAlike License"} ]
    categories = []
    for cls in CLASS:
        category = { "id" : class_to_ind[cls], "name" : cls, "supercategory" : "object",}
        categories.append(category)
    print(categories)
    annotations = {"info": info2, "images": [], "annotations": [], "categories":categories, "license":license}
    obj_id = 0
    ID = 0
    for i in tqdm(range(500)):
        img, box_list, poly_list = merge_object_background(materials, objects, info, source, des)
        H, W, _, = img.shape
        filename = "{}.jpg".format(ID)
        image = { "license": 1,
                      "file_name": filename,
                      "coco_url": "xxx",
                      "height": H,
                      "width": W,
                      "date_captured": "2019-06-25",
                      "flickr_url": "xxx",
                      "id": ID
                    }

        annotations["images"].append(image)
        cv2.imwrite("./rscup/aug/{}/".format(classname)+filename, img)
        for box, poly in zip(box_list, poly_list):
            xmin, ymin, w, h = box
            area = cv2.contourArea(np.array(np.array([poly], np.int32)))
            poly = poly.flatten()
            annotation = {"id" : obj_id, 
                        "image_id" : ID, 
                        "category_id" : class_to_ind[classname], 
                        "segmentation" : [poly], 
                       "area" :  area, 
                       "bbox" : [xmin, ymin, w, h], 
                       "iscrowd" : 0,
                    }
            obj_id += 1
            annotations["annotations"].append(annotation)
        ID += 1
    with open('./rscup/aug/annos_rscup_{}'.format(classname)+'.json', 'w') as json_file:
        json.dump(annotations, json_file, cls=MyEncoder)
aug_with_background("container-crane")<jupyter_output>[{'id': 0, 'name': 'tennis-court', 'supercategory': 'object'}, {'id': 1, 'name': 'container-crane', 'supercategory': 'object'}, {'id': 2, 'name': 'storage-tank', 'supercategory': 'object'}, {'id': 3, 'name': 'baseball-diamond', 'supercategory': 'object'}, {'id': 4, 'name': 'plane', 'supercategory': 'object'}, {'id': 5, 'name': 'ground-track-field', 'supercategory': 'object'}, {'id': 6, 'name': 'helicopter', 'supercategory': 'object'}, {'id': 7, 'name': 'airport', 'supercategory': 'object'}, {'id': 8, 'name': 'harbor', 'supercategory': 'object'}, {'id': 9, 'name': 'ship', 'supercategory': 'object'}, {'id': 10, 'name': 'large-vehicle', 'supercategory': 'object'}, {'id': 11, 'name': 'swimming-pool', 'supercategory': 'object'}, {'id': 12, 'name': 'soccer-ball-field', 'supercategory': 'object'}, {'id': 13, 'name': 'roundabout', 'supercategory': 'object'}, {'id': 14, 'name': 'basketball-court', 'supercategory': 'object'}, {'id': 15, 'name': 'bridge', 'supercategory': 'object'}, {'id': 16, 'n[...]
{'fraction_non_alphanumeric': 0.18192895650247395, 'fraction_numerical': 0.035927922605420576, 'mean_word_length': 2.689782016348774, 'pattern_counts': {'":': 66, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '423479', 'license': 'Apache-2.0', 'n_tokens_mistral': 10787, 'n_tokens_neox': 10097, 'n_words': 1893, 'path': 'aug_chips.ipynb', 'repo_name': 'cizhenshi/Rscup2019-Object-Detection-Track-3th-Solution', 'size': '27082'}