pipeline.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. """pipeline: Detection and other model pipeline."""
  3. from PIL import Image
  4. from pycs.pipeline.Pipeline import Pipeline as PipelineInterface
  5. from pycs.utils import Video
  6. from .detection import Detector
  7. from .features import Features
  8. from ..Job import Job
  9. from ..Result import Result
  10. class Pipeline(PipelineInterface):
  11. def __init__(self):
  12. self.__detector = None
  13. self.__features = None
  14. def load(self, root: str, distribution: dict):
  15. print('tf1 load')
  16. detector_config = distribution['detection']
  17. features_config = distribution['features']
  18. self.__detector = Detector(config={
  19. **detector_config,
  20. 'distribution-root': root
  21. })
  22. self.__features = Features(config={
  23. **features_config,
  24. 'distribution-root': root
  25. })
  26. def close(self):
  27. print('tf1 close')
  28. if self.__detector is not None:
  29. self.__detector.close()
  30. if self.__features is not None:
  31. self.__features.close()
  32. def execute(self, job: Job) -> Result:
  33. subjobs = [{
  34. 'subjob': job.type,
  35. 'prediction': {},
  36. 'jobinfo': {},
  37. 'filetype': 'image',
  38. 'filename': job.object_full_path
  39. }]
  40. self.__execute(subjobs, lambda x: 0)
  41. return Result(job, subjobs[0]['prediction']['faces'])
  42. def __execute(self, subjobs, callback):
  43. callback(0)
  44. subjob_count = float(len(subjobs))
  45. for index, subjob in enumerate(subjobs):
  46. subjob_name = subjob['subjob']
  47. prediction = subjob['prediction']
  48. jobinfo = subjob['jobinfo']
  49. jobinfo[subjob_name] = {'done-by': 'pipeline'}
  50. if subjob_name == 'detect-faces':
  51. # Run face detection
  52. if self.__detector.last_error is not None:
  53. jobinfo[subjob_name]['error'] = self.__detector.last_error
  54. jobinfo[subjob_name]['result'] = False
  55. else:
  56. filename = subjob['filename']
  57. print(filename)
  58. # Acquire image
  59. if subjob['filetype'] == 'image':
  60. img = Image.open(filename)
  61. elif subjob['filetype'] == 'video':
  62. if 'cap' in subjob.keys():
  63. cap = subjob['cap']
  64. else:
  65. cap = Video(filename)
  66. if cap.last_error is None:
  67. jobinfo[subjob_name]['error'] = cap.last_error
  68. else:
  69. jobinfo[subjob_name]['result'] = False
  70. continue
  71. img = cap.get_frame(subjob['frame'])
  72. else:
  73. jobinfo[subjob_name]['error'] = 'File format not supported!'
  74. jobinfo[subjob_name]['result'] = False
  75. continue
  76. faces = self.__detector.detect_faces(img)
  77. if self.__detector.last_error is not None:
  78. jobinfo[subjob_name]['error'] = self.__detector.last_error
  79. jobinfo[subjob_name]['result'] = False
  80. else:
  81. prediction['faces'] = faces
  82. jobinfo[subjob_name]['result'] = True
  83. else:
  84. jobinfo[subjob_name]['result'] = False
  85. callback(float(index) / subjob_count)
  86. callback(1)