|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +''' |
| 4 | +@File : prepare_ovdg_dataset2.py |
| 5 | +@Version : 1.0 |
| 6 | +@Time : 2024/09/13 14:01:26 |
| 7 | +@E-mail : daodao123@sjtu.edu.cn |
| 8 | +@Introduction : None |
| 9 | +''' |
| 10 | + |
| 11 | +import os |
| 12 | +import json |
| 13 | +import imagesize |
| 14 | +import argparse |
| 15 | + |
| 16 | +from data_classes import DOTA2NWPU as Data2NWPU |
| 17 | + |
| 18 | + |
| 19 | +if __name__ == '__main__': |
| 20 | + parser = argparse.ArgumentParser(description="Prepare NWPU45 dataset.") |
| 21 | + parser.add_argument("--data_dir", type=str, required=True, help="Path to the nwpu45 dataset directory") |
| 22 | + parser.add_argument("--save_path", type=str, required=True, help="Path where the json file will be saved") |
| 23 | + args = parser.parse_args() |
| 24 | + |
| 25 | + data = {"annotations": [], |
| 26 | + "images": [], |
| 27 | + "categories": []} |
| 28 | + cname2cid = {cname: cid for cid, cname in enumerate(Data2NWPU.keys())} |
| 29 | + data["categories"] = [{'id': cid, 'name': cname} for cname, cid in cname2cid.items()] |
| 30 | + |
| 31 | + img_names = [] |
| 32 | + dirname2cname = {} |
| 33 | + for cname, dir in Data2NWPU.items(): |
| 34 | + if dir is not None: |
| 35 | + dirname2cname[dir] = cname |
| 36 | + files = os.listdir(os.path.join(args.data_dir, dir)) |
| 37 | + files = [os.path.join(dir, f) for f in files] |
| 38 | + img_names.extend(files) |
| 39 | + |
| 40 | + for id, fname in enumerate(img_names): |
| 41 | + width, height = imagesize.get(os.path.join(args.data_dir, fname)) |
| 42 | + texts = f"a photo of a {os.path.dirname(fname)}." |
| 43 | + data["images"].append({"id": id, |
| 44 | + "file_name": fname, |
| 45 | + "height": height, |
| 46 | + "width": width, |
| 47 | + "caption": texts, |
| 48 | + "category_id": cname2cid[dirname2cname[os.path.dirname(fname)]]}) |
| 49 | + |
| 50 | + os.makedirs(os.path.dirname(args.save_path), exist_ok=True) |
| 51 | + |
| 52 | + with open(args.save_path, 'w') as f: |
| 53 | + json.dump(data, f, indent=4) |
0 commit comments