__init__.py 1016 B

123456789101112131415161718192021222324252627282930313233343536
  1. import cv2
  2. import numpy as np
  3. from json import dump, load
  4. from pycs.interfaces.MediaFile import MediaFile
  5. from pycs.interfaces.MediaStorage import MediaStorage
  6. from pycs.interfaces.Pipeline import Pipeline as Interface
  7. from .detector import Detector
  8. class Scanner(Interface):
  9. def __init__(self, root_folder: str, configuration: dict):
  10. super().__init__(root_folder, configuration)
  11. self.detector = Detector(configuration["detector"])
  12. def close(self):
  13. pass
  14. def execute(self, storage: MediaStorage, file: MediaFile):
  15. im = self.read_image(file.path)
  16. detections = self.detector(im)
  17. for bbox, info in detections:
  18. if not info.selected:
  19. continue
  20. x0, y0, x1, y1 = bbox
  21. w, h = x1-x0, y1-y0
  22. file.add_bounding_box(x0, y0, w, h)
  23. def read_image(self, path: str) -> np.ndarray:
  24. im = cv2.imread(path, cv2.IMREAD_COLOR)
  25. im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
  26. return im