"official/vision/modeling/__init__.py" did not exist on "7793eb27bcf16f792baa2e0c0646a5730a9c2dd9"
utils.py 1.3 KB
Newer Older
HHL's avatar
v  
HHL committed
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
import os


def get_filepaths(file_dir, ext='.pdf'):
    '''
        goal: 提取当前文件夹及其子文件夹下的所有'.ext'文件
        param: file_dir, 需要提取的文件夹路径
        param: ext, 需要提取的文件类型
        output: 文件类型为ext的所有文件路径
    '''
    all_files = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[-1].lower() == ext:
                all_files.append(root+"/"+file)
    return all_files


def get_image_file_list(img_file):
    imgs_lists = []
    if img_file is None or not os.path.exists(img_file):
        raise Exception("not found any img file in {}".format(img_file))

    img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
    if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
        imgs_lists.append(img_file)
    elif os.path.isdir(img_file):
        for single_file in os.listdir(img_file):
            file_path = os.path.join(img_file, single_file)
            if os.path.isfile(file_path) and imghdr.what(file_path) in img_end:
                imgs_lists.append(file_path)
    if len(imgs_lists) == 0:
        raise Exception("not found any img file in {}".format(img_file))
    imgs_lists = sorted(imgs_lists)
    return imgs_lists