get_image_list.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import argparse
  16. import base64
  17. import numpy as np
  18. def get_image_list(img_file):
  19. imgs_lists = []
  20. if img_file is None or not os.path.exists(img_file):
  21. raise Exception("not found any img file in {}".format(img_file))
  22. img_end = ['jpg', 'png', 'jpeg', 'JPEG', 'JPG', 'bmp']
  23. if os.path.isfile(img_file) and img_file.split('.')[-1] in img_end:
  24. imgs_lists.append(img_file)
  25. elif os.path.isdir(img_file):
  26. for single_file in os.listdir(img_file):
  27. if single_file.split('.')[-1] in img_end:
  28. imgs_lists.append(os.path.join(img_file, single_file))
  29. if len(imgs_lists) == 0:
  30. raise Exception("not found any img file in {}".format(img_file))
  31. imgs_lists = sorted(imgs_lists)
  32. return imgs_lists
  33. def get_image_list_from_label_file(image_path, label_file_path):
  34. imgs_lists = []
  35. gt_labels = []
  36. with open(label_file_path, "r") as fin:
  37. lines = fin.readlines()
  38. for line in lines:
  39. image_name, label = line.strip("\n").split()
  40. label = int(label)
  41. imgs_lists.append(os.path.join(image_path, image_name))
  42. gt_labels.append(int(label))
  43. return imgs_lists, gt_labels