6
0

Pipeline.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. raise NotImplementedError
  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. raise NotImplementedError
  22. def execute(self, file: MediaFile) -> List[dict]:
  23. """
  24. receive a job, execute it and return the predicted result
  25. :param file: which should be analyzed
  26. :return:
  27. """
  28. raise NotImplementedError
  29. def fit(self, files: List[AnnotatedMediaFile]):
  30. """
  31. receive a list of annotated media files and adapt the underlying model
  32. :param files: list of annotated media files
  33. :return:
  34. """
  35. raise NotImplementedError
  36. @staticmethod
  37. def create_labeled_image_result(label: int):
  38. """
  39. create a labeled-image result dictionary
  40. :param label: label identifier
  41. :return: dict
  42. """
  43. return {
  44. 'type': 'labeled-image',
  45. 'label': label
  46. }
  47. @staticmethod
  48. def create_bounding_box_result(x: float, y: float, w: float, h: float, label=None, frame=None):
  49. # pylint: disable=too-many-arguments
  50. # pylint: disable=invalid-name
  51. """
  52. create a bounding-box result dictionary
  53. :param x: relative x coordinate [0, 1]
  54. :param y: relative y coordinate [0, 1]
  55. :param w: relative width [0, 1]
  56. :param h: relative height [0, 1]
  57. :param label: label identifier
  58. :param frame: frame index
  59. :return: dict
  60. """
  61. result = {
  62. 'type': 'bounding-box',
  63. 'x': x,
  64. 'y': y,
  65. 'w': w,
  66. 'h': h
  67. }
  68. if label is not None:
  69. result['label'] = label
  70. if frame is not None:
  71. result['frame'] = frame
  72. return result