set_image_backend

paddle.vision.image. set_image_backend ( backend ) [源代码]

paddle.vision.datasets.ImageFolderpaddle.vision.datasets.DatasetFolder 类中指定用于加载图像的后端。 现在支持后端是 pillowopencv 。 如果未设置后端,则默认使用 pil

参数

  • backend (str) - 加载图像的后端,必须为 pil 或者 cv2

代码示例

  1. import os
  2. import shutil
  3. import tempfile
  4. import numpy as np
  5. from PIL import Image
  6. from paddle.vision import DatasetFolder
  7. from paddle.vision import set_image_backend
  8. set_image_backend('pil')
  9. def make_fake_dir():
  10. data_dir = tempfile.mkdtemp()
  11. for i in range(2):
  12. sub_dir = os.path.join(data_dir, 'class_' + str(i))
  13. if not os.path.exists(sub_dir):
  14. os.makedirs(sub_dir)
  15. for j in range(2):
  16. fake_img = Image.fromarray((np.random.random((32, 32, 3)) * 255).astype('uint8'))
  17. fake_img.save(os.path.join(sub_dir, str(j) + '.png'))
  18. return data_dir
  19. temp_dir = make_fake_dir()
  20. pil_data_folder = DatasetFolder(temp_dir)
  21. for items in pil_data_folder:
  22. break
  23. # should get PIL.Image.Image
  24. print(type(items[0]))
  25. # use opencv as backend
  26. # set_image_backend('cv2')
  27. # cv2_data_folder = DatasetFolder(temp_dir)
  28. # for items in cv2_data_folder:
  29. # break
  30. # should get numpy.ndarray
  31. # print(type(items[0]))
  32. shutil.rmtree(temp_dir)

使用本API的教程文档