from skimage.feature import hog
from skimage import colos

from pelops.etl.feature_producer import FeatureProducer


class HOGFeatureProducer(FeatureProducer):

    def __init__(self, chip_producer):
        super().__init__(chip_producer)

    def produce_features(self, chip):
        """Takes a chip object and returns a feature vector of size
        self.feat_size. """
        img = PIL_Image.open(chip.filepath)
        img = img.resize((256, 256), PIL_Image.BICUBIC)
        img_x, img_y = img.size
        img = color.rgb2gray(np.array(img))
        features = hog(
            img,
            orientations=8,
            pixels_per_cell=(int(img_x / 16), int(img_y / 16)),
            cells_per_block=(16, 16),  # Normalize over the whole image
        )
        return features

    def set_variables(self):
        """Child classes should use this to set self.feat_size, and any other
        needed variables. """
        self.feat_size = 2048  # Set this in your inherited class

from skimage.feature import hog
from skimage import colos

from pelops.etl.feature_producer import FeatureProducer


class HOGFeatureProducer(FeatureProducer):

    def __init__(self, chip_producer, image_size=(256,256), cells=(16, 16), orientations=8):
        super().__init__(chip_producer)
        self.image_size = image_size
        self.cells = cells
        self.orientations = orientations

    def produce_features(self, chip):
        """Takes a chip object and returns a feature vector of size
        self.feat_size. """
        img = PIL_Image.open(chip.filepath)
        img = img.resize(self.image_size, PIL_Image.BICUBIC)
        img_x, img_y = img.size
        img = color.rgb2gray(np.array(img))
        features = hog(
            img,
            orientations=self.orientations,
            pixels_per_cell=(img_x / cells[0], img_y / cells[1]),
            cells_per_block=cells,  # Normalize over the whole image
        )
        return features

    def set_variables(self):
        self.feat_size = self.cells[0] * self.cells[1] * orientations

Replace magic numbers in `HOGFeatureProducer`
Replace magic numbers in `HOGFeatureProducer`

  from skimage.feature import hog
  from skimage import colos
  
  from pelops.etl.feature_producer import FeatureProducer
  
  
  class HOGFeatureProducer(FeatureProducer):
  
-     def __init__(self, chip_producer):
+     def __init__(self, chip_producer, image_size=(256,256), cells=(16, 16), orientations=8):
          super().__init__(chip_producer)
+         self.image_size = image_size
+         self.cells = cells
+         self.orientations = orientations
  
      def produce_features(self, chip):
          """Takes a chip object and returns a feature vector of size
          self.feat_size. """
          img = PIL_Image.open(chip.filepath)
-         img = img.resize((256, 256), PIL_Image.BICUBIC)
+         img = img.resize(self.image_size, PIL_Image.BICUBIC)
          img_x, img_y = img.size
          img = color.rgb2gray(np.array(img))
          features = hog(
              img,
-             orientations=8,
+             orientations=self.orientations,
-             pixels_per_cell=(int(img_x / 16), int(img_y / 16)),
+             pixels_per_cell=(img_x / cells[0], img_y / cells[1]),
-             cells_per_block=(16, 16),  # Normalize over the whole image
+             cells_per_block=cells,  # Normalize over the whole image
          )
          return features
  
      def set_variables(self):
+         self.feat_size = self.cells[0] * self.cells[1] * orientations
-         """Child classes should use this to set self.feat_size, and any other
-         needed variables. """
-         self.feat_size = 2048  # Set this in your inherited class
  
Replace magic numbers in `HOGFeatureProducer`
## Code Before:
from skimage.feature import hog
from skimage import colos

from pelops.etl.feature_producer import FeatureProducer


class HOGFeatureProducer(FeatureProducer):

    def __init__(self, chip_producer):
        super().__init__(chip_producer)

    def produce_features(self, chip):
        """Takes a chip object and returns a feature vector of size
        self.feat_size. """
        img = PIL_Image.open(chip.filepath)
        img = img.resize((256, 256), PIL_Image.BICUBIC)
        img_x, img_y = img.size
        img = color.rgb2gray(np.array(img))
        features = hog(
            img,
            orientations=8,
            pixels_per_cell=(int(img_x / 16), int(img_y / 16)),
            cells_per_block=(16, 16),  # Normalize over the whole image
        )
        return features

    def set_variables(self):
        """Child classes should use this to set self.feat_size, and any other
        needed variables. """
        self.feat_size = 2048  # Set this in your inherited class

## Instruction:
Replace magic numbers in `HOGFeatureProducer`
## Code After:
from skimage.feature import hog
from skimage import colos

from pelops.etl.feature_producer import FeatureProducer


class HOGFeatureProducer(FeatureProducer):

    def __init__(self, chip_producer, image_size=(256,256), cells=(16, 16), orientations=8):
        super().__init__(chip_producer)
        self.image_size = image_size
        self.cells = cells
        self.orientations = orientations

    def produce_features(self, chip):
        """Takes a chip object and returns a feature vector of size
        self.feat_size. """
        img = PIL_Image.open(chip.filepath)
        img = img.resize(self.image_size, PIL_Image.BICUBIC)
        img_x, img_y = img.size
        img = color.rgb2gray(np.array(img))
        features = hog(
            img,
            orientations=self.orientations,
            pixels_per_cell=(img_x / cells[0], img_y / cells[1]),
            cells_per_block=cells,  # Normalize over the whole image
        )
        return features

    def set_variables(self):
        self.feat_size = self.cells[0] * self.cells[1] * orientations
