convertRate[size[0]] = std_size[0] / size[0] for target in args.targets: imageAnns, Anns, categories = loadAnns(basedir, target) idlist = {} os.makedirs(os.path.join(args.output, target), exist_ok=True) for imageann in tqdm(imageAnns, desc=f"{target} : "): idlist[imageann["id"]] = convertRate[imageann["width"]] imageann["height"] = int( imageann["height"] * convertRate[imageann["width"]] + 0.5 ) imageann["width"] = int( imageann["width"] * convertRate[imageann["width"]] + 0.5 ) img = cv2.imread(os.path.join(basedir, target, imageann["file_name"])) img = cv2.resize(img, (imageann["width"], imageann["height"])) cv2.imwrite(os.path.join(args.output, target, imageann["file_name"]), img) for ann in Anns: ann["bbox"] = np.array(ann["bbox"]) * idlist[ann["image_id"]] ann["bbox"] = np.array(ann["bbox"]).tolist() os.makedirs(os.path.join(args.output, "annotations"), exist_ok=True) saveAnns( os.path.join(args.output, "annotations", f"instances_{target}.json"), {"images": imageAnns, "annotations": Anns, "categories": categories}, )
images, anns, classes = loadAnns(args.basedir, args.target) assert len(classes) == 11, "all data annotation set should be needed" if args.category == "date": allowcatnum = [11] classes = [classes[10]] classes[0]["id"] = 1 elif args.category == "number": allowcatnum = list(range(1, 11)) classes = classes[0:10] new_anns = [] if args.category != "all": for ann in anns: if ann["category_id"] in allowcatnum: if args.category == "date": ann["category_id"] = 1 new_anns.append(ann) os.makedirs(args.output, exist_ok=True) saveAnns( os.path.join(args.output, "instances_{}.json".format(args.target)), { "images": images, "annotations": new_anns, "categories": classes }, )
for method in copymethod: img, new_dateann = copydateimg_with_ann(img, dateanns[method[0]], dateanns[method[1]], method[2]) new_dateanns.append(new_dateann) cv2.imwrite(os.path.join(args.outdir, args.target, image["file_name"]), img) anns = [] annidx = 30000 for dateann in new_dateanns: for ann in dateann["child"]: annidx += 1 ann["id"] = annidx anns.append(ann) del dateann["child"] anns.append(dateann) os.makedirs(os.path.join(args.outdir, "annotations"), exist_ok=True) saveAnns( os.path.join(args.outdir, f"annotations/instances_{args.target}.json"), { "images": images, "annotations": anns, "categories": categories })