• widerface数据库转voc2007数据集(python/matlab实现)


    python实现基本需求,可以在此基础上修改

    # -*- coding: utf-8 -*-
    """
    Created on 17-5-27
    
    @author: zly 
    """
    from skimage import io
    import shutil
    import random
    import os
    import string
    
    headstr = """
    <annotation>
        <folder>VOC2007</folder>
        <filename>%06d.jpg</filename>
        <source>
            <database>My Database</database>
            <annotation>PASCAL VOC2007</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 all_path(filename):
        return os.path.join('widerface', filename)
    
    def writexml(idx, head, bbxes, tail):
        filename = all_path("Annotations/%06d.xml" % (idx))
        f = open(filename, "w")
        f.write(head)
        for bbx in bbxes:
            f.write(objstr % ('face', bbx[0], bbx[1], bbx[0] + bbx[2], bbx[1] + bbx[3]))
        f.write(tail)
        f.close()
    
    
    def clear_dir():
        if shutil.os.path.exists(all_path('Annotations')):
            shutil.rmtree(all_path('Annotations'))
        if shutil.os.path.exists(all_path('ImageSets')):
            shutil.rmtree(all_path('ImageSets'))
        if shutil.os.path.exists(all_path('JPEGImages')):
            shutil.rmtree(all_path('JPEGImages'))
    
        shutil.os.mkdir(all_path('Annotations'))
        shutil.os.makedirs(all_path('ImageSets/Main'))
        shutil.os.mkdir(all_path('JPEGImages'))
    
    
    def excute_datasets(idx, datatype):
        f = open(all_path('ImageSets/Main/' + datatype + '.txt'), 'a')
        f_bbx = open(all_path('wider_face_split/wider_face_' + datatype + '_bbx_gt.txt'), 'r')
    
        while True:
            filename = string.strip(f_bbx.readline(), '
    ')
            if not filename:
                break
            im = io.imread(all_path('WIDER_' + datatype + '/images/'+filename))
            head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2])
            nums = string.strip(f_bbx.readline(), '
    ')
            bbxes = []
            for ind in xrange(string.atoi(nums)):
                bbx_info = string.split(string.strip(f_bbx.readline(), ' 
    '), ' ')
                bbx = [string.atoi(bbx_info[i]) for i in range(len(bbx_info))]
                #x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose
                if bbx[7]==0:
                    bbxes.append(bbx)
            writexml(idx, head, bbxes, tailstr)
            shutil.copyfile(all_path('WIDER_' + datatype + '/images/'+filename), all_path('JPEGImages/%06d.jpg' % (idx)))
            f.write('%06d
    ' % (idx))
            idx +=1
        f.close()
        f_bbx.close()
        return idx
    
    
    # 打乱样本
    def shuffle_file(filename):
        f = open(filename, 'r+')
        lines = f.readlines()
        random.shuffle(lines)
        f.seek(0)
        f.truncate()
        f.writelines(lines)
        f.close()
    
    
    if __name__ == '__main__':
        clear_dir()
        idx = 1
        idx = excute_datasets(idx, 'train')
        idx = excute_datasets(idx, 'val')

    目录结构如下

    以下python代码为读取mat,废弃,仅供学习

    import h5py
    from skimage import io
    import shutil
    import random
    
    headstr = """
    <annotation>
        <folder>VOC2007</folder>
        <filename>%06d.jpg</filename>
        <source>
            <database>My Database</database>
            <annotation>PASCAL VOC2007</annotation>
            <image>flickr</image>
            <flickrid>NULL</flickrid>
        </source>
        <owner>
            <flickrid>NULL</flickrid>
            <name>facevise</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 writexml(idx, head, objs, tail):
        filename = "Annotations/%06d.xml" % (idx)
        f = open(filename, "w")
        f.write(head)
        f.write(objs)
        f.write(tail)
        f.close()
        
    def clear_dir():
        if shutil.os.path.exists('Annotations'):
            shutil.rmtree('Annotations')
        if shutil.os.path.exists('ImageSets'):
            shutil.rmtree('ImageSets')
        if shutil.os.path.exists('JPEGImages'):
            shutil.rmtree('JPEGImages')
        
        shutil.os.mkdir('Annotations')
        shutil.os.makedirs('ImageSets/Main')
        shutil.os.mkdir('JPEGImages')
        
    def excute_datasets(idx, datatype):
        f = open('ImageSets/Main/'+datatype+'.txt', 'a')
        mat = h5py.File('wider_face_split/wider_face_'+datatype+'.mat', 'r')
        file_list = mat['file_list'][:]
        event_list = mat['event_list'][:]
        bbx_list = mat['face_bbx_list'][:]
        for i in range(file_list.size):        
            file_list_sub = mat[file_list[0,i]][:]
            bbx_list_sub = mat[bbx_list[0, i]][:]
            event_value = ''.join(chr(x) for x in mat[event_list[0,i]][:])
            for j in range(file_list_sub.size):
                root = 'WIDER_'+datatype+'/images/'+event_value+'/'
                filename = root + ''.join([chr(x) for x in mat[file_list_sub[0, j]][:]])+'.jpg'
                im = io.imread(filename)
                head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2])            
                bboxes = mat[bbx_list_sub[0, j]][:]
                objs = ''.join([objstr % ('face', 
                       bboxes[0,k],bboxes[1,k], bboxes[0,k]+bboxes[2,k]-1,bboxes[1,k]+bboxes[3,k]-1) 
                       for k in range(bboxes.shape[1])])
                writexml(idx, head, objs, tailstr)
                shutil.copyfile(filename, 'JPEGImages/%06d.jpg' % (idx))
                f.write('%06d
    ' % (idx))
                idx +=1
        f.close()   
        return idx
    #打乱样本    
    def shuffle_file(filename):
        f = open(filename, 'r+')
        lines = f.readlines()
        random.shuffle(lines)
        f.seek(0)
        f.truncate()
        f.writelines(lines)
        f.close()
                
    if __name__ == '__main__':
        clear_dir()
        idx = 1
        idx = excute_datasets(idx, 'train')
        idx = excute_datasets(idx, 'val')

    matlab实现

    head.xml

    <annotation>
        <folder>widerface</folder>
        <filename>%06d.jpg</filename>
        <source>
            <database>My Database</database>
            <annotation>VOC2007</annotation>
            <image>flickr</image>
            <flickrid>NULL</flickrid>
        </source>
        <owner>
            <flickrid>NULL</flickrid>
            <name>facevise</name>
        </owner>
        <size>
            <width>%d</width>
            <height>%d</height>
            <depth>%d</depth>
        </size>
        <segmented>0</segmented>

     object.xml

        <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>


    tail.xml

    </annotation>
    function WiderFace2VOC()
    %% wider face
    % The corresponding annotations are in the following format:
    % Here, each face bounding boxe is denoted by:
    % <x_left y_top width height>.
    
    %% voc
    % 000001.jpg car 44 28 132 121  
    %前面是图片名,中间是目标类别,最后是目标的包围框坐标(左上角和右下角坐标)。
    
    %% 
    clc;
    clear;
    fclose all;
    [~, ~, ~] = rmdir('Annotations', 's');
    [~, ~, ~] = rmdir('ImageSets', 's');
    [~, ~, ~] = rmdir('JPEGImages', 's');
    
    [~, ~, ~] = mkdir('Annotations');
    [~, ~, ~] = mkdir('ImageSets/Main');
    [~, ~, ~] = mkdir('JPEGImages');
    
    train_root = 'WIDER_train/images';
    split_file = 'wider_face_split/wider_face_train';
    data = load(split_file);
    
    headXml = fopen('head.xml', 'r');
    headXmlFormat = fread(headXml, Inf, '*char');
    fclose(headXml);
    
    objectXml = fopen('object.xml', 'r');
    objectXmlFormat = fread(objectXml, Inf, '*char');
    fclose(objectXml);
    
    tailXml = fopen('tail.xml', 'r');
    tailXmlFormat = fread(tailXml, Inf, '*char');
    fclose(tailXml);
    
    trainID =  fopen('ImageSets/Main/train.txt', 'w');
    trainvalID =  fopen('ImageSets/Main/trainval.txt', 'w');
    valID =  fopen('ImageSets/Main/val.txt', 'w');
    testID =  fopen('ImageSets/Main/test.txt', 'w');
    
    idx = 1;
    for i=1:numel(data.event_list)
        for j=1:numel(data.file_list{i})
            imagename = fullfile(train_root, data.event_list{i}, strcat(data.file_list{i}{j}, '.jpg'));
            sz = size(imread(imagename));
            AnnotationsXml = fopen(sprintf('Annotations/%06d.xml', idx), 'w');
            fprintf(AnnotationsXml, headXmlFormat, idx, sz(2), sz(1),sz(3));
            for k = 1:size(data.face_bbx_list{i}{j}, 1)
                rc = data.face_bbx_list{i}{j}(k, :);
                rc = round([rc(1), rc(2), rc(1)+rc(3)-1, rc(2)+rc(4)-1]);
                fprintf(AnnotationsXml, objectXmlFormat, 'face', rc(1), rc(2), rc(3), rc(4));
            end
            fprintf(AnnotationsXml, tailXmlFormat);
            fprintf(trainID, '%06d
    ', idx);
            fprintf(trainvalID, '%06d
    ', idx);
            fclose(AnnotationsXml);
            copyfile(imagename, sprintf('JPEGImages/%06d.jpg', idx));
            idx = idx + 1;
        end  
        disp(i);
    end
    
    train_root = 'WIDER_val/images';
    split_file = 'wider_face_split/wider_face_val';
    data = load(split_file);
    
    for i=1:numel(data.event_list)
        for j=1:numel(data.file_list{i})
            imagename = fullfile(train_root, data.event_list{i}, strcat(data.file_list{i}{j}, '.jpg'));
            sz = size(imread(imagename));
            AnnotationsXml = fopen(sprintf('Annotations/%06d.xml', idx), 'w');
            fprintf(AnnotationsXml, headXmlFormat, idx, sz(2), sz(1),sz(3));
            for k = 1:size(data.face_bbx_list{i}{j}, 1)
                rc = data.face_bbx_list{i}{j}(k, :);
                rc = round([rc(1), rc(2), rc(1)+rc(3)-1, rc(2)+rc(4)-1]);
                fprintf(AnnotationsXml, objectXmlFormat, 'face', rc(1), rc(2), rc(3), rc(4));
            end
            fprintf(AnnotationsXml, tailXmlFormat);
            if mod(idx, 2)
                fprintf(valID, '%06d
    ', idx);
                fprintf(trainvalID, '%06d
    ', idx);
            else
                fprintf(testID, '%06d
    ', idx);
            end
            fclose(AnnotationsXml);
            copyfile(imagename, sprintf('JPEGImages/%06d.jpg', idx));
            idx = idx+1;
        end        
        disp(i);
    end
    fclose(trainID);
    fclose(trainvalID);
    fclose(valID);
    fclose(testID);
    fclose all;

  • 相关阅读:
    为什么使用指针比使用对象本身更好?
    基于回调的事件处理——基于回调的事件传播
    基于回调的事件处理——回调机制与监听机制
    基于监听的事件处理——直接绑定到标签
    基于监听的事件处理——匿名内部类作为事件监听器类
    基于监听的事件处理——Activity本身作为事件监听器
    基于监听的事件处理——外部类作为事件监听器类
    基于监听的事件处理——内部类作为事件监听器类
    基于监听的事件处理——事件和事件监听器
    基于监听的事件处理——监听的处理模型
  • 原文地址:https://www.cnblogs.com/linyuanzhou/p/6043436.html
Copyright © 2020-2023  润新知