You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Processing."""
  2. import os
  3. import time
  4. from config import Config as Conf
  5. from utils import camel_case_to_str, cv2_supported_extension
  6. class Processing:
  7. """ Abstract Processing Class """
  8. def run(self, *args, config=None):
  9. """
  10. Run the Image Transform.
  11. :param args: <dict> settings for the transformation
  12. :return: <RGB> image
  13. """
  14. self.running = True
  15. self._start = time.time()
  16. self._args = Conf.args.copy() if config is None else config.copy()
  17. self._info_start_run()
  18. self._setup(*args)
  19. r = self._execute(*args)
  20. self._clean(*args)
  21. self._info_end_run()
  22. self.running = False
  23. return r
  24. def _info_start_run(self):
  25. """
  26. Log info at the start of the run.
  27. :return: None
  28. """
  29. self.__start = time.time()
  30. Conf.log.info("Executing {}".format(camel_case_to_str(self.__class__.__name__)))
  31. def _info_end_run(self):
  32. """
  33. Log info at the end of the run.
  34. :return: None
  35. """
  36. Conf.log.debug("{} Done in {} seconds".format(
  37. camel_case_to_str(self.__class__.__name__), round(time.time() - self.__start, 2)))
  38. def _setup(self, *args):
  39. """
  40. Configure the transformation.
  41. :param args: <dict> settings for the transformation
  42. :return: None
  43. """
  44. pass
  45. def _execute(self, *args):
  46. """
  47. Execute the transformation.
  48. :param args: <dict> settings for the transformation
  49. :return: None
  50. """
  51. pass
  52. def _clean(self, *args):
  53. """
  54. Clean the transformation.
  55. :param args: <dict> settings for the transformation
  56. :return: None
  57. """
  58. pass
  59. class SimpleProcessing(Processing):
  60. """Simple Transform Class."""
  61. def __init__(self, args=None):
  62. """
  63. Construct a Simple Processing .
  64. :param args: <dict> args parameter to run the image transformation (default use Conf.args)
  65. """
  66. super().__init__(args)
  67. def __new__(cls, args=None):
  68. """
  69. Create the correct SimpleTransform object corresponding to the input_path format.
  70. :param args: <dict> args parameter to run the image transformation (default use Conf.args)
  71. :return: <ImageProcessing|GiftProcessing|None> SimpleTransform object corresponding to the input_path format
  72. """
  73. args = Conf.args.copy() if args is None else args.copy()
  74. if os.path.splitext(args['input'])[1] == ".gif":
  75. from processing.gif import GifProcessing
  76. return GifProcessing()
  77. elif os.path.splitext(args['input'])[1] in cv2_supported_extension():
  78. from processing.image import ImageProcessing
  79. return ImageProcessing()
  80. else:
  81. return None