COCO转YoloV5数据集,适用于YoloV5、ScaledYoloV4

举报
AI浩 发表于 2021/12/22 23:35:03 2021/12/22
【摘要】 import jsonimport globimport osimport shutilfrom pathlib import Pathimport numpy as npfrom tqdm import tqdmdef make_folders(path='../out/'): # Create folders if o...

  
  1. import json
  2. import glob
  3. import os
  4. import shutil
  5. from pathlib import Path
  6. import numpy as np
  7. from tqdm import tqdm
  8. def make_folders(path='../out/'):
  9. # Create folders
  10. if os.path.exists(path):
  11. shutil.rmtree(path) # delete output folder
  12. os.makedirs(path) # make new output folder
  13. os.makedirs(path + os.sep + 'labels') # make new labels folder
  14. os.makedirs(path + os.sep + 'images') # make new labels folder
  15. return path
  16. def convert_coco_json(json_dir='../coco/annotations/'):
  17. dir = make_folders(path='out/') # output directory
  18. jsons = glob.glob(json_dir + '*.json')
  19. coco80 = coco91_to_coco80_class()
  20. # Import json
  21. for json_file in sorted(jsons):
  22. fn = 'out/labels/%s/' % Path(json_file).stem.replace('instances_', '') # folder name
  23. os.mkdir(fn)
  24. with open(json_file) as f:
  25. data = json.load(f)
  26. # Create image dict
  27. images = {'%g' % x['id']: x for x in data['images']}
  28. # Write labels file
  29. for x in tqdm(data['annotations'], desc='Annotations %s' % json_file):
  30. if x['iscrowd']:
  31. continue
  32. img = images['%g' % x['image_id']]
  33. h, w, f = img['height'], img['width'], img['file_name']
  34. # The Labelbox bounding box format is [top left x, top left y, width, height]
  35. box = np.array(x['bbox'], dtype=np.float64)
  36. box[:2] += box[2:] / 2 # xy top-left corner to center
  37. box[[0, 2]] /= w # normalize x
  38. box[[1, 3]] /= h # normalize y
  39. if (box[2] > 0.) and (box[3] > 0.): # if w > 0 and h > 0
  40. with open(fn + Path(f).stem + '.txt', 'a') as file:
  41. file.write('%g %.6f %.6f %.6f %.6f\n' % (coco80[x['category_id'] - 1], *box))
  42. def coco91_to_coco80_class(): # converts 80-index (val2014) to 91-index (paper)
  43. # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
  44. # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n')
  45. # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n')
  46. # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco
  47. # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet
  48. x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, None, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, None, 24, 25, None,
  49. None, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, None, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  50. 51, 52, 53, 54, 55, 56, 57, 58, 59, None, 60, None, None, 61, None, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
  51. None, 73, 74, 75, 76, 77, 78, 79, None]
  52. return x
  53. convert_coco_json()

 

文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。

原文链接:wanghao.blog.csdn.net/article/details/111472706

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。