1
1

Pipeline.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from typing import List
  2. from pycs.interfaces.AnnotatedMediaFile import AnnotatedMediaFile
  3. from pycs.interfaces.MediaFile import MediaFile
  4. class Pipeline:
  5. """
  6. pipeline interface that should be implemented by model developers
  7. """
  8. def __init__(self, root_folder: str, distribution: dict):
  9. """
  10. prepare everything needed to run jobs later
  11. :param root_folder: relative path to model folder
  12. :param distribution: dict parsed from distribution.json
  13. """
  14. pass
  15. def close(self):
  16. """
  17. is called everytime a pipeline is not needed anymore and should be used
  18. to free native resources
  19. :return:
  20. """
  21. pass
  22. def collections(self) -> List[dict]:
  23. """
  24. is called while initializing a pipeline to receive available
  25. collections
  26. :return: list of collections or None
  27. """
  28. return []
  29. @staticmethod
  30. def create_collection(reference: str,
  31. name: str,
  32. description: str = None,
  33. autoselect: bool = False) -> dict:
  34. """
  35. create a collection dict
  36. :param reference: unique reference
  37. :param name: collection name
  38. :param description: collection description
  39. :param autoselect: show this collection by default if it contains elements
  40. :return: collection dict
  41. """
  42. return {
  43. 'reference': reference,
  44. 'name': name,
  45. 'description': description,
  46. 'autoselect': autoselect
  47. }
  48. def execute(self, file: MediaFile) -> List[dict]:
  49. """
  50. receive a job, execute it and return the predicted result
  51. :param file: which should be analyzed
  52. :return:
  53. """
  54. raise NotImplementedError
  55. @staticmethod
  56. def create_collection_result(reference: str) -> dict:
  57. """
  58. create a collection result dictionary
  59. :param reference: use None to remove this file's collection
  60. :return: dict
  61. """
  62. return {
  63. 'type': 'collection',
  64. 'reference': reference
  65. }
  66. @staticmethod
  67. def create_labeled_image_result(label: int) -> dict:
  68. """
  69. create a labeled-image result dictionary
  70. :param label: label identifier
  71. :return: dict
  72. """
  73. return {
  74. 'type': 'labeled-image',
  75. 'label': label
  76. }
  77. @staticmethod
  78. def create_bounding_box_result(x: float, y: float, w: float, h: float,
  79. label=None, frame=None) -> dict:
  80. # pylint: disable=too-many-arguments
  81. # pylint: disable=invalid-name
  82. """
  83. create a bounding-box result dictionary
  84. :param x: relative x coordinate [0, 1]
  85. :param y: relative y coordinate [0, 1]
  86. :param w: relative width [0, 1]
  87. :param h: relative height [0, 1]
  88. :param label: label identifier
  89. :param frame: frame index
  90. :return: dict
  91. """
  92. result = {
  93. 'type': 'bounding-box',
  94. 'x': x,
  95. 'y': y,
  96. 'w': w,
  97. 'h': h
  98. }
  99. if label is not None:
  100. result['label'] = label
  101. if frame is not None:
  102. result['frame'] = frame
  103. return result
  104. def fit(self, files: List[AnnotatedMediaFile]):
  105. """
  106. receive a list of annotated media files and adapt the underlying model
  107. :param files: list of annotated media files
  108. :return:
  109. """
  110. raise NotImplementedError