project.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. """project: Encapsulates a project with all its properties."""
  3. import json
  4. import os
  5. from pycs.pipeline.tf1 import Pipeline
  6. from ..utils import Errorable
  7. from ..utils import Video
  8. class Project(Errorable):
  9. def __init__(self, project_file, config=None):
  10. Errorable.__init__(self)
  11. self._update_ui_fn = lambda: None
  12. new = config is not None
  13. # (1) Check if directory exists
  14. self.__project_file = project_file
  15. self.__project_path, self.__project_filename = os.path.split(project_file)
  16. if not os.path.exists(self.__project_path) and not new:
  17. self._report_error("Cannot open directory: %s" % self.__project_path)
  18. return
  19. elif not os.path.exists(self.__project_path) and new:
  20. try:
  21. os.mkdir(self.__project_path)
  22. except:
  23. self._report_error("Cannot create directory: %s" % self.__project_path)
  24. return
  25. # (2) Load project json
  26. if new:
  27. self._config = config
  28. else:
  29. try:
  30. with open(self.__project_file, 'r') as c:
  31. self._config = json.load(c)
  32. except:
  33. self._report_error("Cannot open configuration for project: %s" % self.__project_path)
  34. return
  35. # (3) Load detection/extraction pipeline
  36. pipeline_config = self._config["pipeline"]
  37. self._pipeline = Pipeline(pipeline_config)
  38. self._err_children += [self._pipeline]
  39. def save(self):
  40. try:
  41. with open(self.__project_file, 'w') as c:
  42. json.dump(self._config, c, indent=2)
  43. except:
  44. self._report_error("Cannot save configuration for project: %s" % self.__project_path)
  45. def execute(self, jobs, callback=lambda progress: True):
  46. all_subjobs = []
  47. for job in jobs:
  48. job['prediction'] = {}
  49. job['jobinfo'] = {}
  50. for subjob in job['jobs']:
  51. if job['filetype'] == 'image':
  52. pipeline_subjob = {'filetype': 'image',
  53. 'filename': job['filename'],
  54. 'prediction': job['prediction'],
  55. 'jobinfo': job['jobinfo'],
  56. 'subjob': subjob}
  57. all_subjobs += [pipeline_subjob]
  58. # TODO split video into frames
  59. elif job['filetype'] == 'video':
  60. cap = Video(job['filename'])
  61. if cap.framecount > 0:
  62. job['prediction-by-frame'] = [{} for frame in range(cap.framecount)]
  63. job['jobinfo-by-frame'] = [{} for frame in range(cap.framecount)]
  64. for frame in range(cap.framecount):
  65. pipeline_subjob = {'filetype': 'video',
  66. 'cap': cap,
  67. 'frame': frame,
  68. 'filename': job['filename'],
  69. 'prediction': job['prediction-by-frame'][frame],
  70. 'jobinfo': job['jobinfo-by-frame'][frame],
  71. 'subjob': subjob}
  72. all_subjobs += [pipeline_subjob]
  73. self._pipeline.execute(all_subjobs, callback)
  74. callback(1)
  75. def close(self):
  76. self.detector.close()
  77. self.features.close()
  78. def close(self):
  79. self._pipeline.close()