This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

The environment

  • window 10 64bit
  • coco yolo

preface

MS COCO data set introduced COCO data set, COCO is to write the corresponding annotation information of all pictures in a JSON file, as follows

Therefore, to convert the data set of COCO format into YOLO format, essentially is to parse the JSON file, extract the annotation information of the corresponding picture, and write it into the TXT file

In field

Here is use our familiar roboflow platform mask data set, the download address is public.roboflow.com/object-dete…

Download in COCO format

After unzipping, go to the train folder and you can see the JSON file and image file

Create the Python script coco2yolo.py in the sibling directory and type in the following code, with a few comments embedded in the code

Import OS import JSON from TQDM import TQDM import argparse def convert(size, box): "size: width and height (w,h) box format X,y,w,h x_center/image_width y_center/image_height width/image_width height/image_height ''' dw = 1. / (size[0]) dh = 1. / (size[1]) x = box[0] + box[2] / 2.0 y = box[1] + box[3] / 2.0 w = box[3] h = box[3] x = x * dw w = w * dw y = y * dh h =  h * dh return (x, y, w, h) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--json_file', default='test.json', type=str, help="coco file path") parser.add_argument('--save_dir', default='labels', type=str, Parse_args () data = json.load(open(arg.json_file, 'r')) Create if not os.path.exists(arg.save_dir): Os.makedirs (arg.save_dir) id_map = {} TXT with open(os.path.join(arg.save_dir, 'classes.txt'), 'w') as f: for i, category in enumerate(data['categories']): f.write(f"{category['name']}\n") id_map[category['id']] = i for img in tqdm(data['images']): # images; Filename = img["file_name"] img_width = img["width"] img_height = img["height"] img_id = img["id"] Head, tail = os.path.splitext(filename) # Txt_name = head + ". TXT "f_txt = open(os.path.join(arg.save_dir, txt_name), 'w') for ann in data['annotations']: if ann['image_id'] == img_id: Box = convert((img_width, img_height), Ann ["bbox"]) A total of five fields f_txt. Write (" % s % s % s \ n % s % s "% (id_map [Ann [" category_id"]], box [0], box [1], the box [2], box [3])) f_txt. Close ()Copy the code

Execute the above code

python coco2yolo.py --json_file _annotations.coco.json --save_dir labels
Copy the code

Once you’re done, you should see the Labels folder and TXT labels in the labels folder

Related reading

  • Conversion between VOC and YOLO data formats