marta-0's picture
add files
6da6215
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
def get_files(root_path):
res = []
for root, dirs, files in os.walk(root_path, followlinks=True):
for f in files:
if f.endswith(('.jpg', '.png', '.jpeg', 'JPG')):
res.append(os.path.join(root, f))
return res
def get_image_list(image_path):
"""Get image list"""
valid_suffix = [
'.JPEG', '.jpeg', '.JPG', '.jpg', '.BMP', '.bmp', '.PNG', '.png'
]
image_list = []
image_dir = None
if os.path.isfile(image_path):
if os.path.splitext(image_path)[-1] in valid_suffix:
image_list.append(image_path)
else:
image_dir = os.path.dirname(image_path)
with open(image_path, 'r') as f:
for line in f:
line = line.strip()
if len(line.split()) > 1:
raise RuntimeError(
'There should be only one image path per line in `image_path` file. Wrong line: {}'
.format(line))
image_list.append(os.path.join(image_dir, line))
elif os.path.isdir(image_path):
image_dir = image_path
for root, dirs, files in os.walk(image_path):
for f in files:
if '.ipynb_checkpoints' in root:
continue
if os.path.splitext(f)[-1] in valid_suffix:
image_list.append(os.path.join(root, f))
image_list.sort()
else:
raise FileNotFoundError(
'`image_path` is not found. it should be an image file or a directory including images'
)
if len(image_list) == 0:
raise RuntimeError('There are not image file in `image_path`')
return image_list, image_dir
def mkdir(path):
sub_dir = os.path.dirname(path)
if not os.path.exists(sub_dir):
os.makedirs(sub_dir)