| args = parser.parse_args() | args = parser.parse_args() | ||||
| # train | |||||
| json_file = args.json | json_file = args.json | ||||
| output = args.out | output = args.out | ||||
| # val | |||||
| # json_file = "/home/user/datasets/coco/annotations/instances_val2017.json" | |||||
| # output = "/home/user/datasets/coco/labels/val2017/" | |||||
| """ | |||||
| 将COCO instance数据集转为YOLO格式 | |||||
| """ | |||||
| class COCO2YOLO: | class COCO2YOLO: | ||||
| def __init__(self): | def __init__(self): | ||||
| self._check_file_and_dir(json_file, output) | self._check_file_and_dir(json_file, output) | ||||
| self.labels = json.load(open(json_file, 'r', encoding='utf-8')) | self.labels = json.load(open(json_file, 'r', encoding='utf-8')) | ||||
| self.coco_id_name_map = self._categories() | self.coco_id_name_map = self._categories() | ||||
| self.coco_name_list = list(self.coco_id_name_map.values()) | self.coco_name_list = list(self.coco_id_name_map.values()) | ||||
| # print(self.labels['images'][0]) | |||||
| # print(self.labels['categories'][0]) | |||||
| # print(self.labels['annotations'][0]) | |||||
| print("total images", len(self.labels['images'])) | print("total images", len(self.labels['images'])) | ||||
| print("total categories", len(self.labels['categories'])) | print("total categories", len(self.labels['categories'])) | ||||
| print("total labels", len(self.labels['annotations'])) | print("total labels", len(self.labels['annotations'])) | ||||
| return images_info | return images_info | ||||
| def _bbox_2_yolo(self, bbox, img_w, img_h): | def _bbox_2_yolo(self, bbox, img_w, img_h): | ||||
| # bbox矩形框, 左上角坐标 , 宽, 高 | |||||
| x, y, w, h = bbox[0], bbox[1], bbox[2], bbox[3] | x, y, w, h = bbox[0], bbox[1], bbox[2], bbox[3] | ||||
| centerx = bbox[0] + w / 2 | centerx = bbox[0] + w / 2 | ||||
| centery = bbox[1] + h / 2 | centery = bbox[1] + h / 2 |