PipelineManager.py 3.1 KB

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