# fastlabel json to coco json
```
import os
import os.path
import json
def x1y1x2y2_to_xywh(value):
coco_bbox = [value[0], value[1], round(value[2] - value[0], 2), round(value[3] - value[1], 2)]
return coco_bbox
def main():
# Get the path to the input file
input_path = os.path.join(os.path.dirname(__file__), './json/I2U-001_1.json')
# Open the input file
with open(input_path, 'r') as input_file:
# Read the input file
input_data = json.load(input_file)
annnotation_id = 0
image_id = 0
coco_data = {
"info": {
"description": "Your dataset description",
"url": "URL to your dataset",
"version": "1.0",
"year": 2023,
"contributor": "Your name",
"date_created": "2023-10-18"
},
"licenses": [
{
"id": 1,
"name": "Your License Name",
"url": "URL to your license"
}
],
"categories": [
{
"id": 1,
"name": "led_green",
"supercategory": "LEDs"
},
{
"id": 2,
"name": "led_orange",
"supercategory": "LEDs"
},
{
"id": 3,
"name": "led_red",
"supercategory": "LEDs"
},
{
"id": 4,
"name": "led_blue",
"supercategory": "LEDs"
}
],
"images": [],
"annotations": []
}
for i in range(1, 2001):
annnotation_id += 1
image_id += 1
image_info = {
"id": image_id,
"width": 1080,
"height": 1920,
"file_name": input_path,
"license": 1,
"date_captured": "2023-10-18T17:56:27Z"
}
coco_data["images"].append(image_info)
for key in input_data[0]['annotations']:
for frame, value in key['points'].items():
if i == int(frame):
coco_bbox = x1y1x2y2_to_xywh(value['value'])
annotation_info = {
"id": i,
"image_id": image_id,
"category_id": 1,
"iscrowd": 0,
"segmentation": [],
"area": 0,
"bbox": coco_bbox
}
# print('------')
# print(annotation_info)
coco_data["annotations"].append(annotation_info)
coco_json = json.dumps(coco_data, indent=4)
# Print or save the JSON data as needed
print(coco_json)
if __name__ == '__main__':
main()
```
```
{"images": [{"id": 14, "dataset_id": 3, "category_ids": [], "path": "./frame/led_on/video_img_280.jpg", "width": 1280, "height": 720, "file_name": "video_img_280.jpg", "annotated": false, "annotating": [], "num_annotations": 0, "metadata": {}, "deleted": false, "milliseconds": 0, "events": [], "regenerate_thumbnail": false}, {"id": 15, "dataset_id": 3, "category_ids": [], "path": "./frame/led_on/video_img_135.jpg", "width": 1280, "height": 720, "file_name": "video_img_135.jpg", "annotated": false, "annotating": [], "num_annotations": 0, "metadata": {}, "deleted": false, "milliseconds": 0, "events": [], "regenerate_thumbnail": false}], "categories": [{"id": 2, "name": "led_green", "supercategory": "", "color": "#75ca25", "metadata": {}, "keypoint_colors": []}, {"id": 19, "name": "led_orange", "supercategory": "", "color": "#ff6600", "metadata": {}, "keypoint_colors": []}], "annotations": [{"id": 145, "image_id": 14, "category_id": 2, "segmentation": [[236.7, 460.5, 236.7, 473.0, 223.4, 473.0, 223.4, 460.5]], "area": 168, "bbox": [223.0, 461.0, 14.0, 12.0], "iscrowd": false, "isbbox": true, "color": "#44d876", "metadata": {"id": "server2_disk3"}}, {"id": 146, "image_id": 14, "category_id": 2, "segmentation": [[992.1, 127.7, 992.1, 137.2, 981.3, 137.2, 981.3, 127.7]], "area": 99, "bbox": [981.0, 128.0, 11.0, 9.0], "iscrowd": false, "isbbox": true, "color": "#ba2fe9", "metadata": {"id": "server1_power1"}}, {"id": 147, "image_id": 14, "category_id": 2, "segmentation": [[988.5, 438.0, 988.5, 455.4, 970.4, 455.4, 970.4, 438.0]], "area": 323, "bbox": [970.0, 438.0, 19.0, 17.0], "iscrowd": false, "isbbox": true, "color": "#1eb856", "metadata": {"id": "server2_power1"}}, {"id": 148, "image_id": 14, "category_id": 2, "segmentation": [[1075.6, 475.7, 1075.6, 486.4, 1066.0, 486.4, 1066.0, 475.7]], "area": 100, "bbox": [1066.0, 476.0, 10.0, 10.0], "iscrowd": false, "isbbox": true, "color": "#93d912", "metadata": {"id": "server2_network1"}}, {"id": 149, "image_id": 14, "category_id": 19, "segmentation": [[934.8, 221.6, 934.8, 212.9, 946.7, 212.9, 946.7, 221.6]], "area": 108, "bbox": [935.0, 213.0, 12.0, 9.0], "iscrowd": false, "isbbox": true, "color": "#ce71e8", "metadata": {"id": "server1_network1"}}, {"id": 140, "image_id": 15, "category_id": 2, "segmentation": [[458.7, 451.0, 458.7, 470.0, 443.6, 470.0, 443.6, 451.0]], "area": 285, "bbox": [444.0, 451.0, 15.0, 19.0], "iscrowd": false, "isbbox": true, "color": "#58c2d7", "metadata": {"id": "server2_disk1"}}, {"id": 141, "image_id": 15, "category_id": 2, "segmentation": [[775.0, 459.9, 775.0, 475.3, 758.5, 475.3, 758.5, 459.9]], "area": 240, "bbox": [759.0, 460.0, 16.0, 15.0], "iscrowd": false, "isbbox": true, "color": "#6522db", "metadata": {"id": "server2_disk2"}}, {"id": 142, "image_id": 15, "category_id": 2, "segmentation": [[1071.3, 460.0, 1071.3, 476.8, 1059.7, 476.8, 1059.7, 460.0]], "area": 187, "bbox": [1060.0, 460.0, 11.0, 17.0], "iscrowd": false, "isbbox": true, "color": "#5d01bc", "metadata": {"id": "server2_disk3"}}, {"id": 143, "image_id": 15, "category_id": 2, "segmentation": [[459.0, 518.9, 459.0, 532.7, 442.2, 532.7, 442.2, 518.9]], "area": 238, "bbox": [442.0, 519.0, 17.0, 14.0], "iscrowd": false, "isbbox": true, "color": "#ca6920", "metadata": {"id": "server2_disk4"}}, {"id": 144, "image_id": 15, "category_id": 2, "segmentation": [[772.4, 526.5, 772.4, 539.5, 758.4, 539.5, 758.4, 526.5]], "area": 182, "bbox": [758.0, 527.0, 14.0, 13.0], "iscrowd": false, "isbbox": true, "color": "#996119", "metadata": {"id": "server2_disk5"}}]}```
import os
import os.path
import json
import glob
def x1y1x2y2_to_xywh(value):
coco_bbox = [value[0], value[1], round(value[2] - value[0], 2), round(value[3] - value[1], 2)]
return coco_bbox
def main():
# Get the path to the input file
#input_path = os.path.join(os.path.dirname(__file__), './json/I2U-001_1.json')
# Open the input file
#with open(input_path, 'r') as input_file:
# Read the input file
#input_data = json.load(input_file)
image_id = 0
coco_data = {
"info": {
"description": "FY2022 FastLabel led annotation",
"url": "",
"version": "1.0",
"year": 2023,
"contributor": "",
"date_created": "2023-10-18"
},
"licenses": [
{
"id": 1,
"name": "",
"url": ""
}
],
"categories": [
{
"id": 1,
"name": "led_green",
"supercategory": "LEDs"
},
{
"id": 2,
"name": "led_orange",
"supercategory": "LEDs"
},
{
"id": 3,
"name": "led_red",
"supercategory": "LEDs"
},
{
"id": 4,
"name": "led_blue",
"supercategory": "LEDs"
}
],
"images": [],
"annotations": []
}
json_dir_path = "./json"
image_dir_path = "./data"
input_data = None
file_list = glob.glob(os.path.join(json_dir_path, "*.json"))
for json_path in file_list:
print(json_path)
with open(json_path, 'r') as f:
input_data = json.load(f)
frame_count = input_data[0]['frameCount']
file_name = os.path.basename(file_path)
frame_num = 0
for i in range(1, frame_count+1):
frame_num += 1
image_id += 1
image_path = os.path.join(image_dir_path, file_name+ "_" + v +".jpg")
image_info = {
"id": image_id,
"width": 1080,
"height": 1920,
"file_name": json_path,
"license": 1,
"date_captured": "2023-10-18T17:56:27Z"
}
coco_data["images"].append(image_info)
for key in input_data[0]['annotations']:
annotation_subject = key['value']
if "server" in annotation_subject:
continue
if key['value'] == "led_green":
category_id = 1
if key['value'] == "led_orange":
category_id = 2
if key['value'] == "led_red":
category_id = 3
if key['value'] == "led_blue":
category_id = 4
for frame, value in key['points'].items():
if i == int(frame):
coco_bbox = x1y1x2y2_to_xywh(value['value'])
annotation_info = {
"id": i,
"image_id": image_id,
"category_id": category_id,
"iscrowd": 0,
"segmentation": [],
"area": 0,
"bbox": coco_bbox
}
# print('------')
# print(annotation_info)
coco_data["annotations"].append(annotation_info)
# print(coco_data)
with open('./output/led.json', 'w') as f:
json.dump(coco_data, f, indent=2)
if __name__ == '__main__':
main()