File size: 2,115 Bytes
bfed184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-

import os
import errno
import pickle
from collections import deque
import torch


def mkdirs(path):
    try:
        os.makedirs(path)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise


def pickle_load(file_path):
    assert os.path.isfile(file_path), file_path
    with open(file_path, 'rb') as f:
        file = pickle.load(f)
    return file


def pickle_dump(file_path, file):
    if os.path.isfile(file_path):
        os.remove(file_path)
    with open(file_path, 'wb') as f:
        pickle.dump(file, f, protocol=2)


def walk_all_files_with_suffix(dir,
                               suffixs=('.jpg', '.png', '.jpeg'),
                               sort=False):
    paths = []
    names = []
    assert os.path.isdir(dir), '%s is not a valid directory' % dir
    for root, _, fnames in sorted(os.walk(dir)):
        for fname in fnames:
            path = os.path.join(root, fname)
            if os.path.splitext(fname)[1].lower() in suffixs:
                paths.append(path)
                if not sort:
                    names.append(fname)

    if sort:
        paths.sort()
        for i in range(len(paths)):
            names.append(os.path.basename(paths[i]))
    return len(names), names, paths


def get_dirs(root_dir):
    dir_paths = []
    dir_names = []
    for lists in os.listdir(root_dir):
        path = os.path.join(root_dir, lists)
        if os.path.isdir(path):
            dir_paths.append(path)
            dir_names.append(os.path.basename(path))
    dir_names.sort()
    dir_paths.sort()
    return len(dir_names), dir_names, dir_paths


def get_leave_dirs(root_dir):
    leave_dirs = []
    for dirpath, dirnames, filenames in os.walk(root_dir):
        if not dirnames: 
            leave_dirs.append(dirpath)
    return leave_dirs


def merge_pkl_dict(paths):
    dicts = []
    for i in range(len(paths)):
        dicts.append(pickle_load(paths[i]))
    for i in range(len(paths) - 1):
        dicts[0].update(dicts[i + 1])
    return dicts[0]