|
@@ -1,67 +1,55 @@
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
"""pipeline: Detection and other model pipeline."""
|
|
|
-
|
|
|
-import json
|
|
|
-import os.path
|
|
|
-
|
|
|
from PIL import Image
|
|
|
|
|
|
-from pycs.utils import Errorable
|
|
|
+from pycs.pipeline.Pipeline import Pipeline as PipelineInterface
|
|
|
from pycs.utils import Video
|
|
|
from .detection import Detector
|
|
|
from .features import Features
|
|
|
+from ..Job import Job
|
|
|
+from ..Result import Result
|
|
|
|
|
|
|
|
|
-class Pipeline(Errorable):
|
|
|
- def __init__(self, config):
|
|
|
- Errorable.__init__(self)
|
|
|
- self.config = config
|
|
|
-
|
|
|
- self.detector, self.features = self._load_distribution()
|
|
|
- self._err_children += [self.detector, self.features]
|
|
|
-
|
|
|
- def _load_distribution(self):
|
|
|
- try:
|
|
|
- distribution_path = self.config['model-distribution']
|
|
|
- with open(os.path.join(distribution_path, 'distribution.json'), 'r') as distribution_json_file:
|
|
|
- distribution_json = json.load(distribution_json_file)
|
|
|
-
|
|
|
- detector_config = distribution_json['detection']
|
|
|
- features_config = distribution_json['features']
|
|
|
-
|
|
|
- except:
|
|
|
- self._report_error("Could not parse the distribution configuration")
|
|
|
- # TODO nothing is returned if no exception occurs
|
|
|
- return None, None
|
|
|
-
|
|
|
- try:
|
|
|
- detector = self._load_detector(detector_config)
|
|
|
- except:
|
|
|
- self._report_error("Could not load the detector")
|
|
|
- return None, None
|
|
|
+class Pipeline(PipelineInterface):
|
|
|
+ def __init__(self):
|
|
|
+ self.__detector = None
|
|
|
+ self.__features = None
|
|
|
|
|
|
- try:
|
|
|
- features = self._load_features(features_config)
|
|
|
- except:
|
|
|
- # TODO detector should not be closed manually
|
|
|
- detector.close()
|
|
|
- self._report_error("Could not load the feature extraction mechanism")
|
|
|
- return None, None
|
|
|
+ def load(self, root: str, distribution: dict):
|
|
|
+ print('tf1 load')
|
|
|
+ detector_config = distribution['detection']
|
|
|
+ features_config = distribution['features']
|
|
|
|
|
|
- return detector, features
|
|
|
+ self.__detector = Detector(config={
|
|
|
+ **detector_config,
|
|
|
+ 'distribution-root': root
|
|
|
+ })
|
|
|
+ self.__features = Features(config={
|
|
|
+ **features_config,
|
|
|
+ 'distribution-root': root
|
|
|
+ })
|
|
|
|
|
|
- def _load_detector(self, config):
|
|
|
- detector = Detector(config={**config, 'distribution-root': self.config['model-distribution']})
|
|
|
-
|
|
|
- return detector
|
|
|
-
|
|
|
- def _load_features(self, config):
|
|
|
- features = Features(config={**config, 'distribution-root': self.config['model-distribution']})
|
|
|
-
|
|
|
- return features
|
|
|
-
|
|
|
- def execute(self, subjobs, callback):
|
|
|
+ def close(self):
|
|
|
+ print('tf1 close')
|
|
|
+ if self.__detector is not None:
|
|
|
+ self.__detector.close()
|
|
|
+ if self.__features is not None:
|
|
|
+ self.__features.close()
|
|
|
+
|
|
|
+ def execute(self, job: Job) -> Result:
|
|
|
+ subjobs = [{
|
|
|
+ 'subjob': job.type,
|
|
|
+ 'prediction': {},
|
|
|
+ 'jobinfo': {},
|
|
|
+ 'filetype': 'image',
|
|
|
+ 'filename': job.object_full_path
|
|
|
+ }]
|
|
|
+ self.__execute(subjobs, lambda x: 0)
|
|
|
+
|
|
|
+ return Result(job, subjobs[0]['prediction']['faces'])
|
|
|
+
|
|
|
+ def __execute(self, subjobs, callback):
|
|
|
callback(0)
|
|
|
subjob_count = float(len(subjobs))
|
|
|
for index, subjob in enumerate(subjobs):
|
|
@@ -72,12 +60,13 @@ class Pipeline(Errorable):
|
|
|
|
|
|
if subjob_name == 'detect-faces':
|
|
|
# Run face detection
|
|
|
- if self.detector.last_error is not None:
|
|
|
- jobinfo[subjob_name]['error'] = self.detector.last_error
|
|
|
+ if self.__detector.last_error is not None:
|
|
|
+ jobinfo[subjob_name]['error'] = self.__detector.last_error
|
|
|
jobinfo[subjob_name]['result'] = False
|
|
|
|
|
|
else:
|
|
|
filename = subjob['filename']
|
|
|
+ print(filename)
|
|
|
|
|
|
# Acquire image
|
|
|
if subjob['filetype'] == 'image':
|
|
@@ -99,10 +88,10 @@ class Pipeline(Errorable):
|
|
|
jobinfo[subjob_name]['result'] = False
|
|
|
continue
|
|
|
|
|
|
- faces = self.detector.detect_faces(img)
|
|
|
+ faces = self.__detector.detect_faces(img)
|
|
|
|
|
|
- if self.detector.last_error is not None:
|
|
|
- jobinfo[subjob_name]['error'] = self.detector.last_error
|
|
|
+ if self.__detector.last_error is not None:
|
|
|
+ jobinfo[subjob_name]['error'] = self.__detector.last_error
|
|
|
jobinfo[subjob_name]['result'] = False
|
|
|
else:
|
|
|
prediction['faces'] = faces
|
|
@@ -112,7 +101,3 @@ class Pipeline(Errorable):
|
|
|
|
|
|
callback(float(index) / subjob_count)
|
|
|
callback(1)
|
|
|
-
|
|
|
- def close(self):
|
|
|
- self.detector.close()
|
|
|
- self.features.close()
|