Beispiel #1
0
def rot90(parent_dir, image_dir):  #旋转90度
    imgs_path = os.path.join(parent_dir, image_dir)
    imgs_out_path = os.path.join(parent_dir,
                                 '{}{}'.format(image_dir, '_with_rot90'))
    io_utils.delete_file_folder(imgs_out_path)
    io_utils.mkdir(imgs_out_path)
    #print(imgs_out_path)

    images = [os.path.join(imgs_path, s) for s in os.listdir(imgs_path)]
    for image_file in images:
        try:
            img = cv2.imread(image_file)
            width = img.shape[0]
            height = img.shape[1]
            if width > height:
                image = np.array(np.rot90(img, 1))
                image = image.copy()
            else:
                image = img
            name = image_file.split('/')[-1]
            save_path = os.path.join(imgs_out_path, name)

            # don't need resize
            # image = cv2.resize(image, (int(image.shape[1] * 0.5), int(image.shape[0]*0.5)), interpolation=cv2.INTER_CUBIC)
            # print('resize:{}'.format(image.shape))
            cv2.imwrite(save_path, image)

            #print(save_path)
        except Exception as e:
            print('Exception in pascal_voc_parser: {}'.format(e))
            continue

    return imgs_out_path
Beispiel #2
0
def create_subs(src_dir):
    # root_home = os.path.dirname(args.parent_dir)

    # JPEGImages_dir = os.path.join(args.parent_dir,'-'.format(idx) ,'JPEGImages\\')
    # Annotations_dir = os.path.join(args.parent_dir, '-'.format(idx) ,'Annotations\\')
    # io_utils.mkdir(JPEGImages_dir)
    # io_utils.mkdir(Annotations_dir)
    # io_utils.remove_all(JPEGImages_dir)
    # io_utils.remove_all(Annotations_dir)

    idx = 0
    folder = 10
    for s in os.listdir(src_dir):
        if idx % 200 == 0:
            JPEGImages_dir = os.path.join(
                args.parent_dir + '-{}'.format(folder), 'JPEGImages/')
            Annotations_dir = os.path.join(
                args.parent_dir + '-{}'.format(folder), 'Annotations/')
            io_utils.mkdir(JPEGImages_dir)
            io_utils.mkdir(Annotations_dir)
            io_utils.remove_all(JPEGImages_dir)
            io_utils.remove_all(Annotations_dir)
            folder += 1
        idx += 1
        file = os.path.join(src_dir, s)
        io_utils.copy(file, JPEGImages_dir)
Beispiel #3
0
def copy_to_JPEGImages(src_dir):  #把处理好的图片放到JPEGImages
    target_dir = os.path.join(args.parent_dir, 'JPEGImages/')
    io_utils.mkdir(target_dir)
    io_utils.remove_all(target_dir)

    for s in os.listdir(src_dir):
        file = os.path.join(src_dir, s)
        io_utils.copy(file, target_dir)
Beispiel #4
0
def _create_Main(path):
    '''
    create the trainval.txt and test.txt for train.
    trainval data : test data = 5:1
    :param path:
    :return:
    '''
    scale = 3
    image_dir = os.path.join(path, 'JPEGImages')
    anno_dir = os.path.join(path, 'Annotations')
    ImageSets_path = os.path.join(path, 'ImageSets')
    main_dir = os.path.join(ImageSets_path, 'Main')

    mkdir(main_dir)

    imgs = os.listdir(image_dir)
    random.shuffle(imgs)

    trainval_test_images = []
    trainval_images = []
    test_images = []

    for i in range(len(imgs)):
        s = imgs[i]
        trainval_test_images.append(s.split('.')[0] + '\n')

    for i in range(len(imgs) // scale, len(imgs)):
        s = imgs[i]
        trainval_images.append(s.split('.')[0] + '\n')

    for i in range(len(imgs) // scale):
        s = imgs[i]
        test_images.append(s.split('.')[0] + '\n')

    with open(main_dir + '/trainval_test.txt', 'w+') as f:
        f.writelines(trainval_test_images)
        print("{}, numbers:{}".format(main_dir + '/trainval_test.txt',
                                      len(trainval_test_images)))

    with open(main_dir + '/trainval.txt', 'w+') as f:
        f.writelines(trainval_images)
        print("{}, numbers:{}".format(main_dir + '/trainval.txt',
                                      len(trainval_images)))
    with open(main_dir + '/test.txt', 'w+') as f:
        f.writelines(test_images)
        print("{}, numbers:{}".format(main_dir + '/test.txt',
                                      len(test_images)))

    print('total: {}'.format(len(imgs)))
    print('step: {}'.format(len(trainval_images) // 2 + 1))
Beispiel #5
0
def create_zip(src_dir, parent_dir):  #分包加压缩
    # root_home = os.path.dirname(args.parent_dir)
    # JPEGImages_dir = os.path.join(args.parent_dir,'-'.format(idx) ,'JPEGImages\\')
    # Annotations_dir = os.path.join(args.parent_dir, '-'.format(idx) ,'Annotations\\')
    # io_utils.mkdir(JPEGImages_dir)
    # io_utils.mkdir(Annotations_dir)
    # io_utils.remove_all(JPEGImages_dir)
    # io_utils.remove_all(Annotations_dir)
    idx = 0
    folder = 0

    for s in os.listdir(src_dir):
        if idx % 200 == 0:  #每200张图片操作一次,从第0张开始
            if folder >= 1:
                parent = temp_dir + '-{}'.format(folder - 1)
                parent_zip = temp_dir + '-{}.zip'.format(folder - 1)
                zip_dir(parent, parent_zip)

            temp_dir = os.path.join(
                parent_dir,
                str_date[0:4] + '-' + str_date[4:6] + '-' + str_date[6:8])
            JPEGImages_dir = os.path.join(temp_dir + '-{}'.format(folder),
                                          'JPEGImages/')
            #Annotations_dir = os.path.join(temp_dir + '-{}'.format(folder), 'Annotations\\')
            io_utils.mkdir(JPEGImages_dir)
            #io_utils.mkdir(Annotations_dir)
            io_utils.remove_all(JPEGImages_dir)  #确保目标文件夹是空白的
            #io_utils.remove_all(Annotations_dir)
            folder += 1
        idx += 1
        file = os.path.join(src_dir, s)
        io_utils.copy(file, JPEGImages_dir)
        #full_path = Annotations_dir + '\\' + 'test' + '.txt'  # 加入了一个txt
        #open(full_path, 'w')

    #
    parent = temp_dir + '-{}'.format(folder - 1)
    parent_zip = temp_dir + '-{}.zip'.format(folder - 1)
    zip_dir(parent, parent_zip)
Beispiel #6
0
def move_little_size(image_files, move_path):
    log_file = os.path.join(os.path.dirname(move_path), 'check_log.txt')
    log_list = []
    valid_num = 0
    size_num = 0
    io_utils.mkdir(move_path)
    for path in image_files:
        isValid = IsValidImage(path)
        if isValid:
            log_list.append("{} is valid,move!")
            valid_num += 1
            io_utils.move(path, move_path)
        else:
            im_size = PIL.Image.open(path).size
            min_size = min(im_size[0], im_size[1])
            if min_size < 2000:
                size_num += 1
                io_utils.move(path, move_path)
                log_list.append("{} size({},{}) is less than 800,move!".format(
                    os.path.split(path)[1], im_size[0], im_size[1]))
    print(valid_num, size_num)
    with open(log_file, 'w+') as f:
        f.write('\n'.join(log_list))
Beispiel #7
0
def rename_image(parent_dir, image_dir_name):  #重命名
    # image_dir_name = 'JPEGImages'
    data_dir = os.path.join(parent_dir, image_dir_name)
    data_rename_dir = os.path.join(parent_dir,
                                   '{}_rename'.format(image_dir_name))

    io_utils.delete_file_folder(data_rename_dir)
    io_utils.mkdir(data_rename_dir)
    prefix = 'train'
    idx = 1000  #起始编码id
    cur_date = datetime.datetime.now()
    # str_date = '{year}{month}{day}'.format(year=cur_date.year, month=cur_date.month, day=cur_date.day)
    for s in os.listdir(data_dir):
        old = os.path.join(data_dir, s)
        new = os.path.join(
            data_rename_dir, '{}_{}_{}.jpg'.format(
                prefix,
                str_date[0:4] + '-' + str_date[4:6] + '-' + str_date[6:8],
                idx))
        io_utils.copy(old, new)
        idx = idx + 1

    return data_rename_dir
Beispiel #8
0
    for image_path in image_files:
        process_choice(image_path)
        count += 1
        if count % 5000 == 0:
            print(count)


def mutil_process(func):
    all_data = get_images_file(input)
    cpus = os.cpu_count() - 1
    p = multiprocessing.pool.Pool(cpus)
    p.map_async(func, all_data)

    p.close()
    p.join()


input = '/home/ljk/data/train_data/background'
output = '/home/ljk/data/train_data/background_min_2000'
io_utils.mkdir(output)

if __name__ == "__main__":

    # # single process
    # # main(input,output)
    #
    #
    # # mutil process
    # # mutil_process()
    mutil_process(process_choice)
Beispiel #9
0
def create_origin(parent_dir, origin_dir):  #创建一个origin文件夹
    origin_pic_dir = os.path.join(parent_dir, origin_dir)
    io_utils.mkdir(origin_pic_dir)
    for s in os.listdir(parent_dir):
        file = os.path.join(parent_dir, s)
        io_utils.move(file, origin_pic_dir)