PipelineManager.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from os import path
  2. from eventlet import tpool
  3. from pycs.pipeline.Job import Job
  4. from pycs.projects.Project import Project
  5. class PipelineManager:
  6. def __init__(self, project: Project):
  7. code_path = path.join(project['model']['path'], project['model']['code']['module'])
  8. module_name = code_path.replace('/', '.').replace('\\', '.')
  9. class_name = project['model']['code']['class']
  10. mod = __import__(module_name, fromlist=[class_name])
  11. cl = getattr(mod, class_name)
  12. self.project = project
  13. self.pipeline = cl(project['model']['path'], project['model'])
  14. def close(self):
  15. print('PipelineManager', 'close')
  16. self.pipeline.close()
  17. def run(self, 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. # remove existing pipeline predictions from media_fle
  23. media_file.remove_pipeline_results()
  24. # add new predictions
  25. for prediction in result.predictions:
  26. media_file.add_result(prediction, origin='pipeline')
  27. def fit(self):
  28. print('PipelineManager', 'fit')
  29. data = []
  30. for identifier in self.project['data']:
  31. obj = self.project['data'][identifier]
  32. media = {
  33. 'name': obj['name'],
  34. 'extension': obj['extension'],
  35. 'size': obj['size'],
  36. 'path': path.join('projects', self.project['id'], 'data', identifier + obj['extension']),
  37. 'predictionResults': []
  38. }
  39. for prediction_identifier in obj['predictionResults']:
  40. prediction = obj['predictionResults'][prediction_identifier]
  41. if prediction['origin'] != 'user':
  42. continue
  43. if 'x' not in prediction:
  44. media['predictionResults'].append({
  45. 'type': 'labeled-image',
  46. 'label': prediction['label']
  47. })
  48. else:
  49. if 'label' in prediction:
  50. media['predictionResults'].append({
  51. 'type': 'labeled-bounding-box',
  52. 'x': prediction['x'],
  53. 'y': prediction['y'],
  54. 'w': prediction['w'],
  55. 'h': prediction['h'],
  56. 'label': prediction['label']
  57. })
  58. else:
  59. media['predictionResults'].append({
  60. 'type': 'bounding-box',
  61. 'x': prediction['x'],
  62. 'y': prediction['y'],
  63. 'w': prediction['w'],
  64. 'h': prediction['h']
  65. })
  66. data.append(media)
  67. self.pipeline.fit(data)