PipelineManager.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from os import getcwd
  2. from os import path
  3. from eventlet import tpool
  4. from pycs.ApplicationStatus import ApplicationStatus
  5. from pycs.pipeline.Job import Job
  6. from pycs.pipeline.tf1.pipeline import Pipeline as TF1Pipeline
  7. from pycs.projects.Project import Project
  8. class PipelineManager:
  9. def __init__(self, project: Project):
  10. self.project = project
  11. self.pipeline = tpool.execute(self.__load_pipeline, project['pipeline']['model-distribution'])
  12. def __enter__(self):
  13. return self
  14. def __exit__(self, type, value, traceback):
  15. self.pipeline.close()
  16. def run(self, media_file):
  17. print('>>>', media_file)
  18. # create job list
  19. # TODO update job progress
  20. job = Job('detect-faces', self.project['id'], media_file)
  21. result = tpool.execute(lambda p, j: p.execute(j), self.pipeline, job)
  22. media_file['predictionResults'] = result.predictions
  23. print('<<<', media_file)
  24. def __load_pipeline(self, pipeline_identifier):
  25. model_distribution = self.project.parent.parent['models'][pipeline_identifier]
  26. if model_distribution['mode'] == 'tf1':
  27. model_root = path.join(getcwd(), 'models', model_distribution['name'])
  28. pipeline = TF1Pipeline()
  29. pipeline.load(model_root, model_distribution['pipeline'])
  30. return pipeline
  31. '''
  32. def __update(self, data):
  33. # get current project path
  34. opened_projects = list(filter(lambda x: x['status'] == 'open', data))
  35. if len(opened_projects) == 0:
  36. return
  37. current_project = opened_projects[0]
  38. # find images to predict
  39. if 'data' not in current_project.keys() or len(current_project['data']) == 0:
  40. return
  41. # load pipeline
  42. pipeline = tpool.execute(self.__load_pipeline, current_project['pipeline']['model-distribution'])
  43. # create job list
  44. for d in current_project['data']:
  45. print('keys:', d.keys())
  46. if 'result' not in d.keys():
  47. # TODO update job progress
  48. job = Job('detect-faces', current_project['id'], d)
  49. result = tpool.execute(lambda p, j: p.execute(j), pipeline, job)
  50. d['result'] = result.predictions
  51. # close pipeline
  52. pipeline.close()
  53. '''
  54. '''
  55. def __update(self, data):
  56. for current_project in data:
  57. print('>>>>>>>>>>')
  58. # find images to predict
  59. if 'data' not in current_project.keys() or len(current_project['data']) == 0:
  60. return
  61. # load pipeline
  62. pipeline = tpool.execute(self.__load_pipeline, current_project['pipeline']['model-distribution'])
  63. # create job list
  64. for d in current_project['data']:
  65. print('keys:', d.keys())
  66. if 'result' not in d.keys():
  67. # TODO update job progress
  68. job = Job('detect-faces', current_project['id'], d)
  69. result = tpool.execute(lambda p, j: p.execute(j), pipeline, job)
  70. d['result'] = result.predictions
  71. # close pipeline
  72. pipeline.close()
  73. '''