python生成coco格式

x33g5p2x  于2022-02-14 转载在 Python  
字(8.7k)|赞(0)|评价(0)|浏览(725)

yolo生成labelme格式标签:

  1. import json
  2. import cv2
  3. class MyEncoder(json.JSONEncoder):
  4. def default(self, obj):
  5. if isinstance(obj, np.integer):
  6. return int(obj)
  7. elif isinstance(obj, np.floating):
  8. return float(obj)
  9. elif isinstance(obj, np.ndarray):
  10. return obj.tolist()
  11. else:
  12. return super(MyEncoder, self).default(obj)
  13. base_name= os.path.basename(file)
  14. orgimg = cv2.imread(file) # BGR
  15. img0 = copy.deepcopy(orgimg)
  16. assert orgimg is not None, 'Image Not Found ' + image_path
  17. h0, w0 = orgimg.shape[:2] # orig hw
  18. annotation = {}
  19. annotation['version'] ="4.5.6"
  20. annotation['flags'] = {}
  21. annotation['shapes'] = []
  22. annotation['imagePath'] = base_name
  23. annotation['imageData'] = None
  24. annotation['imageHeight'] = h0
  25. annotation['imageWidth'] = w0
  26. boxes=[]
  27. for j in range(det.size()[0]):
  28. xywh = (xyxy2xywh(det[j, :4].view(1, 4)) / gn).view(-1).tolist()
  29. conf = det[j, 4].cpu().numpy()
  30. landmarks = (det[j, 5:15].view(1, 10) / gn_lks).view(-1).tolist()
  31. class_num = det[j, 15].cpu().numpy()
  32. orgimg = show_results(orgimg, xywh, conf, landmarks, class_num)
  33. data={"label":'head'}
  34. data['points']=[det[j, :2].cpu().data.numpy().tolist(),det[j, 2:4].cpu().data.numpy().tolist()]
  35. data['group_id']=None
  36. data['shape_type']='rectangle'
  37. data['flags']={}
  38. annotation['shapes'].append(data)
  39. # json.dump(annotation, open(save_json_path, 'w'), indent=4, cls=MyEncoder) # indent=4 更加美观显示
  40. save_json_path=json_to+'/'+base_name[:-4]+".json"
  41. json.dump(annotation, open(save_json_path, 'w'), indent=4,cls=MyEncoder) # indent=4 更加美观显示

以下内容转自:

https://blog.csdn.net/qq_43211132/article/details/109508645

  1. import os
  2. import random
  3. import shutil
  4. import sys
  5. import json
  6. import glob
  7. import xml.etree.ElementTree as ET
  8. """
  9. You only need to set the following three parts
  10. 1.val_files_num : num of validation samples from your all samples
  11. 2.test_files_num = num of test samples from your all samples
  12. 3.voc_annotations : path to your VOC dataset Annotations
  13. """
  14. val_files_num = 457
  15. #test_files_num = 0
  16. voc_annotations = '/Applications/小仙女/tiquxml/zhengjingde/val/' # remember to modify the path
  17. split = voc_annotations.split('/')
  18. coco_name = split[-3]
  19. del split[-3]
  20. del split[-2]
  21. del split[-1]
  22. del split[0]
  23. # print(split)
  24. main_path = ''
  25. for i in split:
  26. main_path += '/' + i
  27. main_path = main_path + '/'
  28. # print(main_path)
  29. coco_path = os.path.join(main_path, coco_name + '_COCO/')
  30. coco_images = os.path.join(main_path, coco_name + '_COCO/images')
  31. coco_json_annotations = os.path.join(main_path, coco_name + '_COCO/annotations/')
  32. xml_val = os.path.join(main_path, 'xml', 'xml_val/')
  33. xml_test = os.path.join(main_path, 'xml/', 'xml_test/')
  34. xml_train = os.path.join(main_path, 'xml/', 'xml_train/')
  35. voc_images = os.path.join(main_path, coco_name, 'JPEGImages/')
  36. # from https://www.php.cn/python-tutorials-424348.html
  37. def mkdir(path):
  38. path = path.strip()
  39. path = path.rstrip("\\")
  40. isExists = os.path.exists(path)
  41. if not isExists:
  42. os.makedirs(path)
  43. print(path + ' ----- folder created')
  44. return True
  45. else:
  46. print(path + ' ----- folder existed')
  47. return False
  48. # foler to make, please enter full path
  49. mkdir(coco_path)
  50. mkdir(coco_images)
  51. mkdir(coco_json_annotations)
  52. mkdir(xml_val)
  53. mkdir(xml_test)
  54. mkdir(xml_train)
  55. # voc images copy to coco images
  56. for i in os.listdir(voc_images):
  57. img_path = os.path.join(voc_images + i)
  58. shutil.copy(img_path, coco_images)
  59. # voc images copy to coco images
  60. for i in os.listdir(voc_annotations):
  61. img_path = os.path.join(voc_annotations + i)
  62. shutil.copy(img_path, xml_train)
  63. print("\n\n %s files copied to %s" % (val_files_num, xml_val))
  64. for i in range(val_files_num):
  65. if len(os.listdir(xml_train)) > 0:
  66. random_file = random.choice(os.listdir(xml_train))
  67. # print("%d) %s"%(i+1,random_file))
  68. source_file = "%s/%s" % (xml_train, random_file)
  69. if random_file not in os.listdir(xml_val):
  70. shutil.move(source_file, xml_val)
  71. else:
  72. random_file = random.choice(os.listdir(xml_train))
  73. source_file = "%s/%s" % (xml_train, random_file)
  74. shutil.move(source_file, xml_val)
  75. else:
  76. print('The folders are empty, please make sure there are enough %d file to move' % (val_files_num))
  77. break
  78. '''
  79. for i in range(test_files_num):
  80. if len(os.listdir(xml_train)) > 0:
  81. random_file = random.choice(os.listdir(xml_train))
  82. # print("%d) %s"%(i+1,random_file))
  83. source_file = "%s/%s" % (xml_train, random_file)
  84. if random_file not in os.listdir(xml_test):
  85. shutil.move(source_file, xml_test)
  86. else:
  87. random_file = random.choice(os.listdir(xml_train))
  88. source_file = "%s/%s" % (xml_train, random_file)
  89. shutil.move(source_file, xml_test)
  90. else:
  91. print('The folders are empty, please make sure there are enough %d file to move' % (val_files_num))
  92. break
  93. '''
  94. print("\n\n" + "*" * 27 + "[ Done ! Go check your file ]" + "*" * 28)
  95. # !/usr/bin/python
  96. # pip install lxml
  97. START_BOUNDING_BOX_ID = 1
  98. PRE_DEFINE_CATEGORIES = None
  99. # If necessary, pre-define category and its id
  100. # PRE_DEFINE_CATEGORIES = {"aeroplane": 1, "bicycle": 2, "bird": 3, "boat": 4,
  101. # "bottle":5, "bus": 6, "car": 7, "cat": 8, "chair": 9,
  102. # "cow": 10, "diningtable": 11, "dog": 12, "horse": 13,
  103. # "motorbike": 14, "person": 15, "pottedplant": 16,
  104. # "sheep": 17, "sofa": 18, "train": 19, "tvmonitor": 20}
  105. """
  106. main code below are from
  107. https://github.com/Tony607/voc2coco
  108. """
  109. def get(root, name):
  110. vars = root.findall(name)
  111. return vars
  112. def get_and_check(root, name, length):
  113. vars = root.findall(name)
  114. if len(vars) == 0:
  115. raise ValueError("Can not find %s in %s." % (name, root.tag))
  116. if length > 0 and len(vars) != length:
  117. raise ValueError(
  118. "The size of %s is supposed to be %d, but is %d."
  119. % (name, length, len(vars))
  120. )
  121. if length == 1:
  122. vars = vars[0]
  123. return vars
  124. def get_filename_as_int(filename):
  125. try:
  126. filename = filename.replace("\\", "/")
  127. filename = os.path.splitext(os.path.basename(filename))[0]
  128. return filename
  129. except:
  130. raise ValueError("Filename %s is supposed to be an integer." % (filename))
  131. def get_categories(xml_files):
  132. """Generate category name to id mapping from a list of xml files.
  133. Arguments:
  134. xml_files {list} -- A list of xml file paths.
  135. Returns:
  136. dict -- category name to id mapping.
  137. """
  138. classes_names = []
  139. for xml_file in xml_files:
  140. tree = ET.parse(xml_file)
  141. root = tree.getroot()
  142. for member in root.findall("object"):
  143. classes_names.append(member[0].text)
  144. classes_names = list(set(classes_names))
  145. classes_names.sort()
  146. return {name: i for i, name in enumerate(classes_names)}
  147. def convert(xml_files, json_file):
  148. json_dict = {"images": [], "type": "instances", "annotations": [], "categories": []}
  149. if PRE_DEFINE_CATEGORIES is not None:
  150. categories = PRE_DEFINE_CATEGORIES
  151. else:
  152. categories = get_categories(xml_files)
  153. bnd_id = START_BOUNDING_BOX_ID
  154. for xml_file in xml_files:
  155. tree = ET.parse(xml_file)
  156. root = tree.getroot()
  157. path = get(root, "path")
  158. if len(path) == 1:
  159. filename = os.path.basename(path[0].text)
  160. elif len(path) == 0:
  161. filename = get_and_check(root, "filename", 1).text
  162. else:
  163. raise ValueError("%d paths found in %s" % (len(path), xml_file))
  164. ## The filename must be a number
  165. image_id = get_filename_as_int(filename)
  166. size = get_and_check(root, "size", 1)
  167. width = int(get_and_check(size, "width", 1).text)
  168. height = int(get_and_check(size, "height", 1).text)
  169. image = {
  170. "file_name": filename,
  171. "height": height,
  172. "width": width,
  173. "id": image_id,
  174. }
  175. json_dict["images"].append(image)
  176. ## Currently we do not support segmentation.
  177. # segmented = get_and_check(root, 'segmented', 1).text
  178. # assert segmented == '0'
  179. for obj in get(root, "object"):
  180. category = get_and_check(obj, "name", 1).text
  181. if category not in categories:
  182. new_id = len(categories)
  183. categories[category] = new_id
  184. category_id = categories[category]
  185. bndbox = get_and_check(obj, "bndbox", 1)
  186. xmin = int(get_and_check(bndbox, "xmin", 1).text) - 1
  187. ymin = int(get_and_check(bndbox, "ymin", 1).text) - 1
  188. xmax = int(get_and_check(bndbox, "xmax", 1).text)
  189. ymax = int(get_and_check(bndbox, "ymax", 1).text)
  190. assert xmax > xmin
  191. assert ymax > ymin
  192. o_width = abs(xmax - xmin)
  193. o_height = abs(ymax - ymin)
  194. ann = {
  195. "area": o_width * o_height,
  196. "iscrowd": 0,
  197. "image_id": image_id,
  198. "bbox": [xmin, ymin, o_width, o_height],
  199. "category_id": category_id,
  200. "id": bnd_id,
  201. "ignore": 0,
  202. "segmentation": [],
  203. }
  204. json_dict["annotations"].append(ann)
  205. bnd_id = bnd_id + 1
  206. for cate, cid in categories.items():
  207. cat = {"supercategory": "none", "id": cid, "name": cate}
  208. json_dict["categories"].append(cat)
  209. os.makedirs(os.path.dirname(json_file), exist_ok=True)
  210. json_fp = open(json_file, "w")
  211. json_str = json.dumps(json_dict)
  212. json_fp.write(json_str)
  213. json_fp.close()
  214. xml_val_files = glob.glob(os.path.join(xml_val, "*.xml"))
  215. #xml_test_files = glob.glob(os.path.join(xml_test, "*.xml"))
  216. xml_train_files = glob.glob(os.path.join(xml_train, "*.xml"))
  217. convert(xml_val_files, coco_json_annotations + 'val2017.json')
  218. #convert(xml_test_files, coco_json_annotations + 'test2017.json')
  219. convert(xml_train_files, coco_json_annotations + 'train2017.json')

相关文章