You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

yolo_to_coco.py 4.0 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import json
  3. import random
  4. import time
  5. from PIL import Image
  6. import csv
  7. coco_format_save_path = './coco' # 要生成的coco格式标签所在文件夹
  8. yolo_format_classes_path = 'annotations.csv' # 类别文件,用csv文件表示,一行一个类
  9. yolo_format_annotation_path = './dataset/mask/labels/val' # yolo格式标签所在文件夹
  10. img_pathDir = './dataset/mask/images/val' # 图片所在文件夹
  11. # 类别设置
  12. categories = []
  13. class_names = ['with_mask', 'without_mask']
  14. for label in class_names:
  15. categories.append({'id': class_names.index(label), 'name': label, 'supercategory': ""})
  16. write_json_context = dict() # 写入.json文件的大字典
  17. write_json_context['licenses'] = [{'name': "", 'id': 0, 'url': ""}]
  18. write_json_context['info'] = {'contributor': "", 'date_created': "", 'description': "", 'url': "", 'version': "", 'year': ""}
  19. write_json_context['categories'] = categories
  20. write_json_context['images'] = []
  21. write_json_context['annotations'] = []
  22. # 接下来的代码主要添加'images'和'annotations'的key值
  23. imageFileList = os.listdir(img_pathDir)
  24. # 遍历该文件夹下的所有文件,并将所有文件名添加到列表中
  25. img_id = 0 # 图片编号
  26. anno_id = 0 # 标注标号
  27. for i, imageFile in enumerate(imageFileList):
  28. if '_' not in imageFile:
  29. img_id += 1
  30. imagePath = os.path.join(img_pathDir, imageFile) # 获取图片的绝对路径
  31. image = Image.open(imagePath) # 读取图片
  32. W, H = image.size # 获取图片的高度宽度
  33. img_context = {} # 使用一个字典存储该图片信息
  34. # img_name=os.path.basename(imagePath)
  35. img_context['id'] = img_id # 每张图像的唯一ID索引
  36. img_context['width'] = W
  37. img_context['height'] = H
  38. img_context['file_name'] = imageFile
  39. img_context['license'] = 0
  40. img_context['flickr_url'] = ""
  41. img_context['color_url'] = ""
  42. img_context['date_captured'] = ""
  43. write_json_context['images'].append(img_context) # 将该图片信息添加到'image'列表中
  44. txtFile = imageFile.split('.')[0] + '.txt' # 获取该图片获取的txt文件
  45. with open(os.path.join(yolo_format_annotation_path, txtFile), 'r') as fr:
  46. lines = fr.readlines() # 读取txt文件的每一行数据,lines2是一个列表,包含了一个图片的所有标注信息
  47. for j, line in enumerate(lines):
  48. anno_id += 1 # 标注的id从1开始
  49. bbox_dict = {} # 将每一个bounding box信息存储在该字典中
  50. class_id, x, y, w, h = line.strip().split(' ') # 获取每一个标注框的详细信息
  51. class_id, x, y, w, h = int(class_id), float(x), float(y), float(w), float(h) # 将字符串类型转为可计算的int和float类型
  52. # 坐标转换
  53. xmin = (x - w / 2) * W
  54. ymin = (y - h / 2) * H
  55. xmax = (x + w / 2) * W
  56. ymax = (y + h / 2) * H
  57. w = w * W
  58. h = h * H
  59. height, width = abs(ymax - ymin), abs(xmax - xmin)
  60. # bounding box的坐标信息
  61. bbox_dict['id'] = anno_id # 每个标注信息的索引
  62. bbox_dict['image_id'] = img_id # 当前图像的ID索引
  63. bbox_dict['category_id'] = class_id # 类别信息
  64. bbox_dict['segmentation'] = [[xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax]]
  65. bbox_dict['area'] = height * width
  66. bbox_dict['bbox'] = [xmin, ymin, w, h] # 注意目标类别要加一
  67. bbox_dict['iscrowd'] = 0
  68. bbox_dict['attributes'] = ""
  69. write_json_context['annotations'].append(bbox_dict) # 将每一个由字典存储的bounding box信息添加到'annotations'列表中
  70. name = os.path.join(coco_format_save_path, "annotations" + '.json')
  71. with open(name, 'w') as fw: # 将字典信息写入.json文件中
  72. json.dump(write_json_context, fw, indent=4, ensure_ascii=False)

随着人工智能和大数据的发展,任一方面对自动化工具有着一定的需求,在当下疫情防控期间,使用mindspore来实现yolo模型来进行目标检测及语义分割,对视频或图片都可以进行口罩佩戴检测和行人社交距离检测,来对公共场所的疫情防控来实行自动化管理。