|
|
import json
|
|
|
import os
|
|
|
|
|
|
|
|
|
def convert_to_coco(json_data):
|
|
|
|
|
|
coco_data = {
|
|
|
"info": {},
|
|
|
"licenses": [],
|
|
|
"images": [],
|
|
|
"annotations": [],
|
|
|
"categories": []
|
|
|
}
|
|
|
|
|
|
|
|
|
coco_data["info"] = {
|
|
|
"description": "Converted from custom format to COCO format",
|
|
|
"url": "http://cocodataset.org",
|
|
|
"version": "1.0",
|
|
|
"year": 2024,
|
|
|
"contributor": "Anonymous",
|
|
|
"date_created": "2024-06-29"
|
|
|
}
|
|
|
|
|
|
|
|
|
image_data = {
|
|
|
"id": 1,
|
|
|
"file_name": json_data["filename"],
|
|
|
"width": int(json_data["size"]["width"]),
|
|
|
"height": int(json_data["size"]["height"]),
|
|
|
"license": None,
|
|
|
"flickr_url": None,
|
|
|
"coco_url": None,
|
|
|
"date_captured": None
|
|
|
}
|
|
|
coco_data["images"].append(image_data)
|
|
|
|
|
|
|
|
|
annotation_id = 1
|
|
|
for obj in json_data["object"]:
|
|
|
annotation = {
|
|
|
"id": annotation_id,
|
|
|
"image_id": image_data["id"],
|
|
|
"category_id": None,
|
|
|
"segmentation": [],
|
|
|
"area": 0,
|
|
|
"bbox": [
|
|
|
float(obj["bndbox"]["xmin"]),
|
|
|
float(obj["bndbox"]["ymin"]),
|
|
|
float(obj["bndbox"]["xmax"]) - float(obj["bndbox"]["xmin"]),
|
|
|
float(obj["bndbox"]["ymax"]) - float(obj["bndbox"]["ymin"])
|
|
|
],
|
|
|
"iscrowd": 0,
|
|
|
"attributes": obj.get("attributes", [])
|
|
|
}
|
|
|
coco_data["annotations"].append(annotation)
|
|
|
annotation_id += 1
|
|
|
|
|
|
return coco_data
|
|
|
|
|
|
|
|
|
json_dir = "/path/to/your/json/files/"
|
|
|
|
|
|
|
|
|
all_coco_data = []
|
|
|
|
|
|
|
|
|
for filename in os.listdir(json_dir):
|
|
|
if filename.endswith(".json"):
|
|
|
json_path = os.path.join(json_dir, filename)
|
|
|
with open(json_path, 'r') as f:
|
|
|
json_data = json.load(f)
|
|
|
|
|
|
|
|
|
coco_data = convert_to_coco(json_data)
|
|
|
|
|
|
|
|
|
all_coco_data.append(coco_data)
|
|
|
|
|
|
|
|
|
merged_coco_data = {
|
|
|
"info": all_coco_data[0]["info"],
|
|
|
"licenses": all_coco_data[0]["licenses"],
|
|
|
"images": [],
|
|
|
"annotations": [],
|
|
|
"categories": all_coco_data[0]["categories"]
|
|
|
}
|
|
|
|
|
|
|
|
|
for coco_data in all_coco_data:
|
|
|
merged_coco_data["images"].extend(coco_data["images"])
|
|
|
merged_coco_data["annotations"].extend(coco_data["annotations"])
|
|
|
|
|
|
|
|
|
merged_json = json.dumps(merged_coco_data, indent=4)
|
|
|
print(merged_json)
|
|
|
|
|
|
|
|
|
output_path = os.path.join(json_dir, "merged_coco_data.json")
|
|
|
with open(output_path, 'w') as f:
|
|
|
json.dump(merged_coco_data, f, indent=4)
|
|
|
|
|
|
print(f"Merged COCO data saved to {output_path}")
|
|
|
|