coco格式转yolo格式

json格式转txt格式

import json
import cv2
import os


save_folder_dir = r'xxx'#即将要保存的yolo格式文件
json_dir = r'/home/coco/coco.json'#已有的json文件
src_im_dir = r'/home/images/'#图片文件夹
def convert_bbox_coco2yolo(img_width, img_height, bbox):

    # YOLO bounding box format: [x_center, y_center, width, height]
    # (float values relative to width and height of image)
    x_tl, y_tl, w, h = bbox

    dw = 1.0 / img_width
    dh = 1.0 / img_height

    x_center = x_tl + w / 2.0
    y_center = y_tl + h / 2.0

    x = x_center * dw
    y = y_center * dh
    w = w * dw
    h = h * dh

    return [x, y, w, h]



with open(json_dir, "r") as f:
    data = json.load(f)

im_info = data['images']
an_info = data['annotations']

for im in im_info:

    file_name = im['file_name']
    print(file_name)
    im_dir = src_im_dir + file_name
    print(im_dir)
    print(111)
    image = cv2.imread(im_dir)
    print(image.shape)
    image_h = image.shape[0]
    image_w = image.shape[1]
    file_name = file_name.split('.')[0]

    im_id = im["id"]
    bbox = []
    category = []
    for an in an_info:
        # print(an)
        if an["image_id"] == im_id:
            bbox.append(an["bbox"])
            category.append(an["category_id"])

    with open(save_folder_dir + '/' + file_name + '.txt', 'w') as f:
        check = 1
        for cat, box in zip(category, bbox):
            b = convert_bbox_coco2yolo(image_w, image_h, box)
            f.write(str(cat) + ' ' + "{:.6f}".format(b[0]) + ' ' + "{:.6f}".format(b[1]) + ' ' + "{:.6f}".format(
                b[2]) + ' ' + "{:.6f}".format(b[3]))
            if check <= len(category) - 1:
                f.write('\n')
            check = check + 1


你可能感兴趣的:(yolov8改进,YOLO,python,深度学习)