PipelineManager.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. 'name': obj['name'],
  33. 'extension': obj['extension'],
  34. 'size': obj['size'],
  35. 'path': path.join('projects', self.project['id'], 'data', identifier + obj['extension']),
  36. 'predictionResults': []
  37. }
  38. for prediction_identifier in obj['predictionResults']:
  39. prediction = obj['predictionResults'][prediction_identifier]
  40. if prediction['origin'] != 'user':
  41. continue
  42. if 'x' not in prediction:
  43. media['predictionResults'].append({
  44. 'type': 'labeled-image',
  45. 'label': prediction['label']
  46. })
  47. else:
  48. if 'label' in prediction:
  49. media['predictionResults'].append({
  50. 'type': 'labeled-bounding-box',
  51. 'x': prediction['x'],
  52. 'y': prediction['y'],
  53. 'w': prediction['w'],
  54. 'h': prediction['h'],
  55. 'label': prediction['label']
  56. })
  57. else:
  58. media['predictionResults'].append({
  59. 'type': 'bounding-box',
  60. 'x': prediction['x'],
  61. 'y': prediction['y'],
  62. 'w': prediction['w'],
  63. 'h': prediction['h']
  64. })
  65. data.append(media)
  66. self.pipeline.fit(data)