|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from pycocotools.coco import COCO |
|
|
|
coco = COCO("zhiyuan_objv2_train.json") |
|
cats = coco.loadCats(coco.getCatIds()) |
|
nms = [cat["name"] for cat in cats] |
|
print("COCO categories: \n{}\n".format(" ".join(nms))) |
|
for categoryId, cat in enumerate(nms): |
|
catIds = coco.getCatIds(catNms=[cat]) |
|
imgIds = coco.getImgIds(catIds=catIds) |
|
print(cat) |
|
|
|
for im in coco.loadImgs(imgIds): |
|
width, height = im["width"], im["height"] |
|
path = im["file_name"].split("/")[-1] |
|
try: |
|
with open("labels/train/" + path.replace(".jpg", ".txt"), "a+") as file: |
|
annIds = coco.getAnnIds(imgIds=im["id"], catIds=catIds, iscrowd=None) |
|
for a in coco.loadAnns(annIds): |
|
x, y, w, h = a['bbox'] |
|
x, y = x + w / 2, y + h / 2 |
|
file.write(f"{categoryId} {x / width:.5f} {y / height:.5f} {w / width:.5f} {h / height:.5f}\n") |
|
|
|
except Exception as e: |
|
print(e) |
|
|