Parcourir la source

Add of test configuration using build_py

Jerome Buisine il y a 5 ans
Parent
commit
a8c9096ee7
4 fichiers modifiés avec 89 ajouts et 3 suppressions
  1. 1 0
      README.rst
  2. 56 1
      ipfml/image_processing.py
  3. 9 0
      main.py
  4. 23 2
      setup.py

+ 1 - 0
README.rst

@@ -25,6 +25,7 @@ This project contains modules.
     - get_LAB_L_SVD_U(image): *Returns U SVD from L of LAB Image information*
     - get_LAB_L_SVD_U(image): *Returns U SVD from L of LAB Image information*
     - get_LAB_L_SVD_s(image): *Returns s (Singular values) SVD from L of LAB Image information*
     - get_LAB_L_SVD_s(image): *Returns s (Singular values) SVD from L of LAB Image information*
     - get_LAB_L_SVD_V(image): *Returns V SVD from L of LAB Image information*
     - get_LAB_L_SVD_V(image): *Returns V SVD from L of LAB Image information*
+    - divide_in_blocks(image, block_size): Divide image into equal size blocks
 
 
 - **metrics** : *Metrics computation of PIL image*
 - **metrics** : *Metrics computation of PIL image*
     - get_SVD(image): *Transforms PIL Image into SVD*
     - get_SVD(image): *Transforms PIL Image into SVD*

+ 56 - 1
ipfml/image_processing.py

@@ -1,8 +1,10 @@
 from PIL import Image
 from PIL import Image
+from matplotlib import cm
 
 
 import numpy as np
 import numpy as np
 import ipfml.metrics as metrics
 import ipfml.metrics as metrics
 
 
+
 def fig2data(fig):
 def fig2data(fig):
     """
     """
     @brief Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it
     @brief Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it
@@ -66,4 +68,57 @@ def get_LAB_L_SVD_V(image):
     @return vector of singular values
     @return vector of singular values
     """
     """
     L = metrics.get_LAB_L(image)
     L = metrics.get_LAB_L(image)
-    return metrics.get_SVD_V(L)
+    return metrics.get_SVD_V(L)
+
+def divide_in_blocks(image, block_size):
+    '''
+    @brief Divide image into equal size blocks
+    @param img - PIL Image
+    @param block - tuple (width, height) representing the size of each dimension of the block
+    @return list containing all PIL Image block (in RGB)
+
+    Usage :
+
+    >>> import numpy as np
+    >>> from PIL import Image
+    >>> from ipfml import image_processing
+    >>> image_values = np.random.randint(255, size=(800, 800, 3))
+    >>> img = Image.fromarray(image_values.astype('uint8'), 'RGB')
+    >>> blocks = divide_in_blocks(img, (20, 20))
+    >>> len(blocks)
+    1600
+    >>> blocks[0].width
+    20
+    >>> blocks[0].height
+    20
+    '''
+
+    blocks = []
+
+    # check size compatibility
+    width, height = block_size
+
+    if(image.width % width != 0):
+        raise "Width size issue, block size not compatible"
+    
+    if(image.height % height != 0):
+        raise "Height size issue, block size not compatible"
+
+    nb_block_width = image.width / width
+    nb_block_height = image.height / height
+
+    image_array = np.array(image)
+
+    for i in range(int(nb_block_width)):
+
+        begin_x = i * width
+
+        for j in range(int(nb_block_height)): 
+
+            begin_y = j * height
+
+            # getting subblock information
+            current_block = image_array[begin_x:(begin_x + width), begin_y:(begin_y + height)]
+            blocks.append(Image.fromarray(current_block.astype('uint8'), 'RGB'))
+
+    return blocks

+ 9 - 0
main.py

@@ -0,0 +1,9 @@
+from PIL import Image
+from ipfml import image_processing
+
+path = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection/img_train/final/appartAopt_00850.png'
+img = Image.open(path)
+blocks = image_processing.divide_in_blocks(img, (80, 80))
+print(len(blocks))
+
+blocks[60].show()

+ 23 - 2
setup.py

@@ -1,11 +1,29 @@
 from setuptools import setup
 from setuptools import setup
+import setuptools.command.build_py
 
 
 def readme():
 def readme():
     with open('README.rst') as f:
     with open('README.rst') as f:
         return f.read()
         return f.read()
 
 
+class BuildTestCommand(setuptools.command.build_py.build_py):
+  """Custom build command."""
+
+  def run(self):
+
+    # run tests using doctest
+    import doctest
+    from ipfml import image_processing
+    from ipfml import metrics
+    from ipfml import tf_model_helper
+
+    doctest.testmod(image_processing)
+    doctest.testmod(metrics)
+    doctest.testmod(tf_model_helper)
+
+    setuptools.command.build_py.build_py.run(self)
+
 setup(name='IPFML',
 setup(name='IPFML',
-      version='0.0.6',
+      version='0.0.7',
       description='Image Processing For Machine Learning',
       description='Image Processing For Machine Learning',
       long_description=readme(),
       long_description=readme(),
       classifiers=[
       classifiers=[
@@ -25,6 +43,9 @@ setup(name='IPFML',
           'Pillow',
           'Pillow',
           'sklearn',
           'sklearn',
           'scikit-image',
           'scikit-image',
-          'scipy'
+          'scipy',
       ],
       ],
+      cmdclass={
+        'build_py': BuildTestCommand,
+      },
       zip_safe=False)
       zip_safe=False)