1. 程式人生 > >資料集製作之txt轉xml

資料集製作之txt轉xml

問題描述:

  • 現有images圖片,txt檔案包含四種類別bounding box 座標,需要將txt中兩種類別座標提取出來並轉化成xml檔案。
  • images資料夾中有18184張圖片,txt資料夾中只有16907個txt檔案(即有一千多張圖片是沒有標註的,需要將這些圖片刪除)
# ! /usr/bin/python
# -*- coding:UTF-8 -*-
import os, sys
import glob
from PIL import Image
# 影象儲存位置
src_img_dir = "./1028data/images"
# 影象的 ground truth 的 txt 檔案存放位置
src_txt_dir = "./1028data/annotations"
# 生成xml檔案存放位置
src_xml_dir = "./1028data/xml"

img_Lists = glob.glob(src_img_dir + '/*.jpg')
img_basenames = []  # e.g. 100.jpg
for item in img_Lists:
    img_basenames.append(os.path.basename(item))
img_names = []  # e.g. 100
for item in img_basenames:
    temp1, temp2 = os.path.splitext(item)
    img_names.append(temp1)

for img in img_names:
    im = Image.open((src_img_dir + '/' + img + '.jpg'))
    width, height = im.size  #xml檔案中需要width和height資訊,這裡通過Image庫計算出來
    # open the corresponding txt file,由於圖片數量和txt數量不一致,所以對於有些圖片,沒有對應的txt檔案,所以這邊要用try
    try:
        gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines() #把txt檔案裡每一行提取出來,我的txt有兩行
    except:
        continue  #跳過這次迴圈,進入下一張圖片迴圈

    # write in xml file
    # os.mknod(src_xml_dir + '/' + img + '.xml')
    xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>VOC2007</folder>\n')
    xml_file.write('    <filename>' + str(img) + '.jpg' + '</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')

    # write the region of image on xml file
    num_obj = int(gt[0])
    print('num_obj: ',num_obj)
    # assert 0
    for i in range(num_obj):
        i += 1

        spt = gt[i].split(' ')  # 這裡如果txt裡面是以逗號‘,’隔開的,那麼就改為spt = img_each_label.split(',')。
       
        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str('pedestrian') + '</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(spt[1]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(spt[2]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(float(spt[3]) + float(spt[1])) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(float(spt[4]) + float(spt[2])) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str('headshoulder') + '</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(spt[11]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(spt[12]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(float(spt[13]) + float(spt[11])) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(float(spt[14]) + float(spt[12])) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')
    xml_file.write('</annotation>')
    print('finish {}'.format(img))

將images資料夾中與txt不匹配的圖片刪去,遍歷txt資料夾,將對應的images複製到另一個資料夾中(這個方法有點笨)


# ! /usr/bin/python
# -*- coding:UTF-8 -*-
import os,shutil
import glob
from PIL import Image

src_img_dir = "./1028data/images"
#  影象的 ground truth 的 txt 檔案存放位置
src_txt_dir = "./1028data/annotations"
src_xml_dir = "./1028data/xml"
# 新的images資料夾
topath = './1028data/new'

xml_Lists = glob.glob(src_xml_dir + '/*.xml')
print(xml_Lists)

xml_basenames = []
for items in xml_Lists:
    xml_basenames.append(os.path.basename(items))
print(xml_basenames)

xml_names = []
for items in xml_basenames:
    temp1, temp2 = os.path.splitext(items)
    xml_names.append(temp1)
#
print('xml_names',xml_names)
for xml in xml_names:
    print('xml:',xml)
    # raw_img = Image.open(src_img_dir + '/' + xml + '.jpg')
    try:
        #raw_img = Image.open(src_img_dir + '/' + xml + '.jpg')
        #print('open {}'.format(raw_img))
        shutil.copy(src_img_dir + '/' + xml + '.jpg', topath + '/' +xml+ '.jpg')  #主要就是這句起復製圖片作用
    except:
        continue