Pipeline.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from json import dump, load
  2. from os import path
  3. from time import sleep
  4. from pycs.interfaces.MediaFile import MediaFile
  5. from pycs.interfaces.MediaStorage import MediaStorage
  6. from pycs.interfaces.Pipeline import Pipeline as Interface
  7. class Pipeline(Interface):
  8. def __init__(self, root_folder, distribution):
  9. print('fmv1 init')
  10. self.root_folder = root_folder
  11. def close(self):
  12. print('fmv1 close')
  13. def execute(self, storage: MediaStorage, file: MediaFile):
  14. print('fmv1 execute')
  15. data_file = path.join(self.root_folder, 'data.json')
  16. if path.exists(data_file):
  17. with open(data_file, 'r') as f:
  18. result = load(f)
  19. else:
  20. result = {}
  21. if file.path in result:
  22. for r in result[file.path]:
  23. if r['type'] == 'MediaBoundingBox':
  24. file.add_bounding_box(r['x'], r['y'], r['w'], r['h'], r['label'], r['frame'])
  25. if r['type'] == 'MediaImageLabel':
  26. file.set_image_label(r['label'], r['frame'])
  27. def fit(self, storage: MediaStorage):
  28. print('fmv1 fit')
  29. for i in range(10):
  30. yield i / 10
  31. sleep(1)
  32. result = {}
  33. for f in storage.files().iter():
  34. result[f.path] = list(map(lambda r: dict(r.__dict__, **{'type': type(r).__name__}),
  35. f.results()))
  36. data_file = path.join(self.root_folder, 'data.json')
  37. with open(data_file, 'w') as file:
  38. dump(result, file, indent=4)