COCO 2017 自定義 darknet 訓練集 (二)

一、具備數據與依賴包

數據:地址

依賴包:地址

 

二、VOC 轉 XML (Train集)

例如:轉換 instances_train2017 instances_val2017 雷同

from pycocotools.coco import COCO
import os
import shutil
from tqdm import tqdm
import skimage.io as io
import matplotlib.pyplot as plt
import cv2
from PIL import Image, ImageDraw

savepath = "D:/openvino_test/VOCdevkit/VOC2017/"
datasets_list = ['train2017']  # 運行完之後再改爲 val2017 再運行一次
img_dir = savepath + 'train_images/'  #這 個路徑會把你處理的圖片拷貝進來,這裏我們只處理了train2017文件夾下的數據
anno_dir = savepath + 'train_annotations_xml/'  # 當前目錄下會生成annotations_xml文件夾存放xml
classes_names = ['car', 'bus', 'truck'] # 過濾我想要的的數據

dataDir = 'D:/openvino_test/VOCdevkit/COCO2017/'  # coco的數據集,train2017在該目錄下 
headstr = """\
<annotation>
    <folder>VOC</folder>
    <filename>%s</filename>
    <source>
        <database>My Database</database>
        <annotation>COCO</annotation>
        <image>flickr</image>
        <flickrid>NULL</flickrid>
    </source>
    <owner>
        <flickrid>NULL</flickrid>
        <name>company</name>
    </owner>
    <size>
        <width>%d</width>
        <height>%d</height>
        <depth>%d</depth>
    </size>
    <segmented>0</segmented>
"""
objstr = """\
    <object>
        <name>%s</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>%d</xmin>
            <ymin>%d</ymin>
            <xmax>%d</xmax>
            <ymax>%d</ymax>
        </bndbox>
    </object>
"""

tailstr = '''\
</annotation>
'''


def mkr(path):
    if os.path.exists(path):
        shutil.rmtree(path)
        os.mkdir(path)
    else:
        os.mkdir(path)


mkr(img_dir)
mkr(anno_dir)


def id2name(coco):
    classes = dict()
    for cls in coco.dataset['categories']:
        classes[cls['id']] = cls['name']
    return classes


def write_xml(anno_path, head, objs, tail):
    f = open(anno_path, "w")
    f.write(head)
    for obj in objs:
        f.write(objstr % (obj[0], obj[1], obj[2], obj[3], obj[4]))
    f.write(tail)


def save_annotations_and_imgs(coco, dataset, filename, objs):
    anno_path = anno_dir + filename[:-3] + 'xml'
    print('anno_path:%s' % anno_path)
    # img_path=dataDir+'/'+'images'+'/'+dataset+'/'+filename
    img_path = dataDir + '/' + dataset + '/' + filename
    print('img_path:%s' % img_path)
    print('step3-image-path-OK')
    dst_imgpath = img_dir + filename

    img = cv2.imread(img_path)
    '''if (img.shape[2] == 1):
        print(filename + " not a RGB image")     
        return'''
    print('img_path:%s' % img_path)
    print('dst_imgpath:%s' % dst_imgpath)
    shutil.copy(img_path, dst_imgpath)

    head = headstr % (filename, img.shape[1], img.shape[0], img.shape[2])
    tail = tailstr
    write_xml(anno_path, head, objs, tail)


def showimg(coco, dataset, img, classes, cls_id, show=True):
    global dataDir
    # I=Image.open('%s/%s/%s/%s'%(dataDir,'images',dataset,img['file_name']))
    I = Image.open('%s/%s/%s' % (dataDir, dataset, img['file_name']))  ########may be you can changed
    annIds = coco.getAnnIds(imgIds=img['id'], catIds=cls_id, iscrowd=None)
    anns = coco.loadAnns(annIds)
    objs = []
    for ann in anns:
        class_name = classes[ann['category_id']]
        if class_name in classes_names:
            print(class_name)
            if 'bbox' in ann:
                bbox = ann['bbox']
                xmin = int(bbox[0])
                ymin = int(bbox[1])
                xmax = int(bbox[2] + bbox[0])
                ymax = int(bbox[3] + bbox[1])
                obj = [class_name, xmin, ymin, xmax, ymax]
                objs.append(obj)
                # draw = ImageDraw.Draw(I)
                # draw.rectangle([xmin, ymin, xmax, ymax])
    # if show:
    # plt.figure()
    # plt.axis('off')
    # plt.imshow(I)
    # plt.show()
    return objs


for dataset in datasets_list:
    annFile = 'D:/openvino_test/VOCdevkit/COCO2017/annotations/instances_train2017.json'  # 你放json文件的路徑
    print('annFile:%s' % annFile)
    coco = COCO(annFile)
    '''
    loading annotations into memory...
    Done (t=0.81s)
    creating index...
    index created!
    '''
    classes = id2name(coco)
    print("classes:%s" % classes)
    classes_ids = coco.getCatIds(catNms=classes_names)
    print(classes_ids)
    for cls in classes_names:
        cls_id = coco.getCatIds(catNms=[cls])
        img_ids = coco.getImgIds(catIds=cls_id)
        print(cls, len(img_ids))
        # imgIds=img_ids[0:10]
        for imgId in tqdm(img_ids):
            img = coco.loadImgs(imgId)[0]
            filename = img['file_name']
            # print(filename)
            objs = showimg(coco, dataset, img, classes, classes_ids, show=False)
            # print(objs)
            save_annotations_and_imgs(coco, dataset, filename, objs)

 

三、生成 train_all.txtval_all.txt (別忘完成後拷一份出來)

import os
from os import getcwd

# wd = getcwd()
wd = 'D:/openvino_test/VOCdevkit/VOC2017'

mulu = ['/' + 'train_annotations_xml', '/' + 'val_annotations_xml']
count = 0
for i in mulu:
    count += 1
    dir = wd + i
    print(dir)
    filenames = os.listdir(dir)
    if count == 1:
        f = open('train_all.txt', 'w')
        count_1 = 0
        for filename in filenames:
            count_1 += 1
            out_path = dir + '/' + filename.replace('xml', 'jpg')
            out_path = out_path.replace('train_annotations_xml', 'train_images')
            f.write(out_path + '\n')
        f.close()
        print('done!,total:%s' % count_1)
    elif count == 2:
        f = open('val_all.txt', 'w')
        count_1 = 0
        for filename in filenames:
            count_1 += 1
            out_path = dir + '/' + filename.replace('xml', 'jpg')
            out_path = out_path.replace('val_annotations_xml', 'val_images')
            f.write(out_path + '\n')
        f.close()
        print('done!,total:%s' % count_1)


 

四、生成 annotations (不按darknet 數據集目錄結構)

例如:生成 train集 annotations ,val 集 annotations 雷同 ,合體就是 darknet labels

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import shutil

sets = ['train']
# classes = ['1', '2', '3','4','5','6','7','8','9','10','11', '12', '13','14','15','16','17','18','19','20']
classes = ['car',  'bus',  'truck']

def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)

def mkr(path):
    if os.path.exists(path):
        shutil.rmtree(path)
        os.mkdir(path)
    else:
        os.mkdir(path)

def convert_annotation(image_id):
    in_file = open('D:/openvino_test/VOCdevkit/VOC2017/train_annotations_xml/%s.xml' % (image_id))  ##########
    out_file = open('D:/openvino_test/VOCdevkit/VOC2017/train_annotations_txt/%s.txt' % (image_id), 'w')  #######
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult) == 1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
        # print("write ok")


# wd = getcwd()
wd = " "

for image_set in sets:

    image_ids = open('train_all.txt').read().strip().split()  ######
    mkr('D:/openvino_test/VOCdevkit/VOC2017/train_annotations_txt')
    # image_ids = open('%s.txt'%(image_set)).read().strip().split()
    print("start ")
    # list_file = open('%s.txt'%(image_set), 'w')

    for image_id in image_ids:
        # print("again write")
        # print('image_id:%s'%image_id)
        # list_file.write('%s/%s.jpg\n'%(wd, image_id))
        id = image_id.split('/')[-1].replace('jpg', 'xml')
        id = id.split('.')[0]
        print('id:%s' % id)
        convert_annotation(id)
    # list_file.close()

 

五、效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章