6
0

PipelineManager.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from os import path
  2. from eventlet import tpool
  3. from pycs.pipeline.Job import Job
  4. class PipelineManager:
  5. def __init__(self, project):
  6. code_path = path.join(project['model']['path'], project['model']['code']['module'])
  7. module_name = code_path.replace('/', '.').replace('\\', '.')
  8. class_name = project['model']['code']['class']
  9. mod = __import__(module_name, fromlist=[class_name])
  10. cl = getattr(mod, class_name)
  11. self.project = project
  12. self.pipeline = cl(project['model']['path'], project['model'])
  13. def close(self):
  14. print('PipelineManager', 'close')
  15. self.pipeline.close()
  16. def run(self, 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. # remove existing pipeline predictions from media_fle
  22. media_file.remove_pipeline_results()
  23. # add new predictions
  24. for prediction in result.predictions:
  25. media_file.add_result(prediction, origin='pipeline')
  26. def fit(self):
  27. print('PipelineManager', 'fit')
  28. data = []
  29. for identifier in self.project['data']:
  30. obj = self.project['data'][identifier]
  31. media = {
  32. 'type': obj['type'],
  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. o = {
  45. 'type': 'labeled-image',
  46. 'label': prediction['label']
  47. }
  48. else:
  49. o = {
  50. 'type': 'bounding-box',
  51. 'x': prediction['x'],
  52. 'y': prediction['y'],
  53. 'w': prediction['w'],
  54. 'h': prediction['h']
  55. }
  56. if 'label' in prediction:
  57. o['type'] = 'labeled-bounding-box'
  58. o['label'] = prediction['label']
  59. if 'frame' in prediction:
  60. o['frame'] = prediction['frame']
  61. media['predictionResults'].append(o)
  62. data.append(media)
  63. self.pipeline.fit(data)