天天看點

Json轉VOC格式的XML

Json轉VOC格式的XML

import os
import json
json_dir = r"D:\dataset\AI裁剪标注0720\json"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
    label_dir.append(os.path.join(json_dir,filename))

img_path = ""
# 顯示
for json_path in label_dir:
    import json
    f = open(json_path, 'r', encoding='utf-8')
    json_data = json.load(f)  # json.load() 這種方法是解析一個檔案中的資料
    # json.loads() 需要先将檔案,讀到一個變量作為字元串, 解析一個字元串中的數
    #print(json_data)
    #print(json_data[0]['image'])
    img_name = json_data[0]['image']
    #print(json_data[0]['annotations'][0]['coordinates'])
    #print(json_data[0]['annotations'][0]['label'])
    #print(json_data[0]['annotationas'][0]['coordinates']['x'])
    # x = int(json_data[0]['annotations'][0]['coordinates']['x'])

    #print(json_data[0]['annotations'][0]['coordinates']['y'])
    #y = int(json_data[0]['annotations'][0]['coordinates']['y'])
    #print(json_data[0]['annotations'][0]['coordinates']['width'])
    width = int(json_data[0]['annotations'][0]['coordinates']['width'])
    #print(json_data[0]['annotations'][0]['coordinates']['height'])
    height = int(json_data[0]['annotations'][0]['coordinates']['height'])

    #x_min = int(x-width/2)
    #y_min = int(y-height/2)
    # x_max =int( x+width/2)
    # y_max = int(y+height/2)

    # 可視化:
    # import numpy as np
    # import cv2
    # # 建立一張黑色的背景圖
    # img = cv2.imread(r"E:\BaiduNetdiskDownload\07 FCOS\FCOS_Pytorch_case\dataset\JPEGImages\0.png")
    #
    # # 畫一個綠色邊框的矩形,參數2:左上角坐标,參數3:右下角坐标
    #
    # cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (0, 255, 255), 1)
    #
    # winname = 'example'
    # cv2.namedWindow(winname)
    # cv2.imshow(winname, img)
    # cv2.waitKey(0)
    # cv2.destroyWindow(winname)


    xml_name = img_name.split('.')[0]
    with open("train.txt", "a") as f:
        f.writelines(xml_name+'\n')

    xml_file = open(('./Annotations\\' + xml_name + '.xml'), 'w')

    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>MyDataset</folder>\n')
    xml_file.write('    <filename>' + str(img_name) + '</filename>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(width) + '</width>\n')
    xml_file.write('        <height>' + str(height) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')

# 擷取box

    for single_anno in json_data[0]['annotations']:
            print(single_anno['coordinates']['x'])
            x = int(single_anno['coordinates']['x'])

            print(single_anno['coordinates']['y'])
            y = int(single_anno['coordinates']['y'])
            print(single_anno['coordinates']['width'])
            width = int(single_anno['coordinates']['width'])
            print(single_anno['coordinates']['height'])
            height = int(single_anno['coordinates']['height'])

            x_min = int(x - width / 2)
            y_min = int(y - height / 2)
            x_max = int(x + width / 2)
            y_max = int(y + height / 2)
            xml_file.write('    <object>\n')
            xml_file.write('        <name>' + 'backbone' + '</name>\n')
            xml_file.write('        <pose>Unspecified</pose>\n')
            xml_file.write('        <truncated>0</truncated>\n')
            xml_file.write('        <difficult>0</difficult>\n')
            xml_file.write('        <bndbox>\n')
            xml_file.write('            <xmin>' + str(x_min) + '</xmin>\n')
            xml_file.write('            <ymin>' + str(y_min) + '</ymin>\n')
            xml_file.write('            <xmax>' + str(x_max) + '</xmax>\n')
            xml_file.write('            <ymax>' + str(y_max) + '</ymax>\n')
            xml_file.write('        </bndbox>\n')
            xml_file.write('    </object>\n')

    xml_file.write('</annotation>\n')      

Json

XML