
Vc           @@  s   d  d l  m Z d  d l Z d  d l Z y d  d l m Z Wn! e k
 r_ d  d l m Z n Xd  d l Z	 d  d l
 Z d  d l Z d Z d	   Z d
   Z d d d  Z d   Z e e d d d  Z d e d d  Z d   Z d S(   i    (   t   absolute_importN(   t   StringIOs   .pngs   .jpgs   .jpegs   .bmps   .ppmc         C@  sj  d } t j j |   rd y  t j j |   } | j   Wqt t k
 r` } t	 j
 d | j  qt Xn t	 j
 d |   | j d k r | S| j d k r | j d  S| j d k r t j j d | j d  } | j | d | j d	  | S| j d k r	| j d  S| j d k rSt j j d | j d  } | j | d | j d	  | St	 j
 d | j  d S(   s   
    Reads a file from `path` and returns a PIL.Image with mode 'L' or 'RGB'
    Raises LoadImageError

    Arguments:
    path -- path to the image, can be a filesystem path or a URL
    s   IOError: %ss   "%s" not foundt   Lt   RGBt   1t   LAi   t   maskt   RGBAt   CMYKt   YCbCrt   Ps   Image mode "%s" not supportedN(   R   s   RGB(   R   (   s   LA(   s   CMYKs   YCbCr(   R
   s   RGBA(   i   i   i   (   t   Nonet   ost   patht   existst   PILt   Imaget   opent   loadt   IOErrort   errorst   LoadImageErrort   messaget   modet   convertt   newt   sizet   paste(   R   t   imaget   eR   (    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyt
   load_image$   s.    c         C@  s  t  |  t j  s! t d   n  | d k  r@ t d |   n  t t j |  j d |   } t t j |  j d |   } |  j d } t j | | | f d t j } xW t j	 | | f  D]@ \ } } |  t j | |  t j | |  f | | | f <q W| S(   s{   
    return upscaled image array

    Arguments:
    image -- a (H,W,C) numpy.ndarray
    ratio -- scaling factor (>1)
    s   Expected ndarrayi   s'   Ratio must be greater than 1 (ratio=%f)i    i   t   dtype(
   t
   isinstancet   npt   ndarrayt
   ValueErrort   intt   matht   floort   shapet   uint8t   ndindex(   R   t   ratiot   widtht   heightt   channelst   outt   xt   y(    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyt   upscaleL   s      !"8c         C@  s  | d k r d } n  | d k r4 t d |   n  | d k rS t d |   n  t |  t j j  r| d k r |  j } | d	 k r d } q | d
 k r d } q t d |   n* | d k r d	 } n | d k r d
 } n  |  j | k r|  j |  }  n  t j |   }  nt |  t j	  r|  j
 t j k rP|  j t j  }  n  |  j d k r|  j d d k r|  j |  j d   }  n  | d k r|  j d k rd } q|  j d k r|  j d d k rd } qt d |  j f   q| d k rv|  j d k r|  j d k rZ|  j d d k rZt j |  d d d g  j t j  }  qst d |  j f   qq| d k r|  j d k rt j |  d  j |  j d  }  q|  j d d k rt d |  j f   qqn t d   |  j d | k r|  j d | k r|  Sd } t |  j d  | } t |  j d  | } | d k sj| | k rt j j |  | | f d | S| d k r| | k r| }	 t t |  j d |   }
 n# | }
 t t |  j d |   }	 t j j |  |	 |
 f d | }  | | k rOt t |
 | d   } |  d d  | | |  f St t |	 | d   } |  | | |  d d  f Sn)| d k rQ| | k r| }
 t t |  j d |   }	 | |	 d d k r-|	 d 7}	 q-nD | }	 t t |  j d |   }
 | |
 d d k r-|
 d 7}
 n  t j j |  |	 |
 f d | }  ne| d k r| | d } t t |  j d |   }
 t t |  j d |   }	 | | k r| |	 d d k r|	 d 7}	 n- | | k  r| |
 d d k r|
 d 7}
 n  t j j |  |	 |
 f d | }  | | k rit t |
 | d   } |  d d  | | |  f }  qt t |	 | d   } |  | | |  d d  f }  n t d |   | | k r:| |	 d } | | f } | d k r| | f 7} n  t j j d d |  j d  } t j | |  | f d d }  nu | |
 d } | | f } | d k rp| | f 7} n  t j j d d |  j d  } t j | |  | f d d }  |  Sd S(   sN  
    Resizes an image and returns it as a np.array

    Arguments:
    image -- a PIL.Image or numpy.ndarray
    height -- height of new image
    width -- width of new image

    Keyword Arguments:
    channels -- channels of new image (stays unchanged if not specified)
    resize_mode -- can be crop, squash, fill or half_crop
    t   squasht   cropt   fillt	   half_crops   resize_mode "%s" not supportedi   i   s"   unsupported number of channels: %sR   R   s   unknown image mode "%s"i   s   invalid image shape: %sgA`"?gbX9?gv/?s<   resize_image() expected a PIL.Image.Image or a numpy.ndarrayi    t   bilineart   interpg       @Ns   unrecognized resize_mode "%s"i   R(   t   axis(   s   crops   squashs   fills	   half_crop(   Ni   i   (   i   (   R   R#   R    R   R   R   R   R!   t   arrayR"   R   R(   t   astypet   ndimR'   t   reshapet   dott   repeatt   floatt   scipyt   misct   imresizeR$   t   roundt	   Exceptiont   randomt   randintt   concatenate(   R   R,   R+   R-   t   resize_modet
   image_modeR7   t   width_ratiot   height_ratiot   resize_heightt   resize_widtht   startt	   new_ratiot   paddingt
   noise_sizet   noise(    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyt   resize_image`   s    						"	"	"*%& !!$  !##!!!c         C@  s   |  d k r d St |  t j j  r( n3 t |  t j  rO t j j |   }  n t d   |  j } | ss d } n | j	   } t
   } |  j | d | | j   j d  j d d  } d | | f S(	   s   
    Returns an image embedded in HTML base64 format
    (Based on Caffe's web_demo)

    Arguments:
    image -- a PIL.Image or np.ndarray
    s)   image must be a PIL.Image or a np.ndarrayt   jpegt   formatt   base64s   
t    s   data:image/%s;base64,%sN(   R   R    R   R   R!   R"   t	   fromarrayR#   RU   t   lowerR   t   savet   getvaluet   encodet   replace(   R   t   fmtt
   string_buft   data(    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyt   embed_image_html   s    			!id   i  c         C@  s  |  j  d k r1 |  d d  t j t j f }  n|  j  d k rm |  j |  j d |  j d d d f  }  n|  j  d k r |  j d d k r|  d d d g d f }  |  j d d d  }  |  t j d f }  qn|  j  d k r|  j d d k rD|  j d d d d  }  |  d d  d d  d d  d d d g f }  q|  j d d k r|  j d d d d  }  |  d d  d d  d d  d d d g f }  q|  j |  j d |  j d |  j d |  j d f  }  n t d |  j f   d } |  j d } | | k r;|  d d  d |  d |  f }  nF | d k rZd } | d 7} n  t | | d  } | | 9} |  |  }  | r|  j  d k r|  d t j f }  n  t |  d	 | d
 | } t	 | j d   d }	 |	 | k  r
| t
 |	  }
 t | |
  } n  | S(   s2  
    Returns a vis_square for the given layer data

    Arguments:
    data -- a np.ndarray

    Keyword arguments:
    allow_heatmap -- if True, convert single channel images to heatmaps
    normalize -- whether to normalize the data when visualizing
    max_width -- maximum width for the vis_square
    i   Ni   i    i   .i   s   unrecognized data shape: %st   padsizet	   normalize(   R;   R!   t   newaxisR<   R'   t	   transposet   RuntimeErrort   maxt
   vis_squaret   sortedR?   R1   (   R`   t   allow_heatmapRc   t   min_img_dimt	   max_widthRb   R+   t   nt   vist   _minR*   (    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyt   get_layer_vis_square  sN    "-44;(

		i   t   jetc         C@  s  d |  j  k o d k n s+ t d   |  j d  }  | r |  |  j   8}  |  j   d k r |  |  j   :}  |  d 9}  q n  |  j  d k rt |  \ } } } t j |  t |  d d t	 t |   |  } t j |  t |  d d t	 t |   |  } t j |  t |  d d t	 t |   |  }	 t j
 | d	 t j f | d	 t j f |	 d	 t j f f d
 d }  t j |  d  }  t j |  d  }  n  |  j d  }  t t j t j |  j d    }
 |
 } |
 } |  j d } |
 |
 d | k r|
 d } n  d | | | f d | f d | f f d |  j  d } t j |  | d d d d } | j | | f | j d  j d t t d | j  d    } | j | | j d | | j d f | j d  } | j d d k rt j | j   g d  } n  | S(   s  
    Visualize each image in a grid of size approx sqrt(n) by sqrt(n)
    Returns a np.array image
    (Based on Caffe's filter_visualization notebook)

    Arguments:
    images -- an array of shape (N, H, W) or (N, H, W, C)
            if C is not set, a heatmap is computed for the result

    Keyword arguments:
    padsize -- how many pixels go inbetween the tiles
    normalize -- if true, scales (min, max) across all images out to (0, 1)
    colormap -- a string representing one of the supported colormaps
    i   i   s   images.ndim must be 3 or 4t   float32i    i   i   g     o@.R8   R(   R   t   constantt   constant_valuesi   i(   i    i    (   (   i    i    (   i    i   i   i   (   R;   t   AssertionErrorR:   t   minRg   t   get_color_mapR!   R7   t   lent   xrangeRG   Rd   t   minimumt   maximumR$   t   ceilt   sqrtR'   t   padR<   Re   t   tuplet   ranget   dstackt   squeeze(   t   imagesRb   Rc   t   colormapt   redmapt   greenmapt   bluemapt   redt   greent   blueRm   t   nyt   nxt   lengthRP   t   paddedt   tiles(    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyRh   h  s:    +333E%8C6c      @   C@  s+  d g } d g } d g } |  d k rN d d g } d d g } d d g } n|  d k r d d d d g } d d d d g } d d d d g } nd|  d k rd d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d g@ } d d d d d d d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d d d d d d d d d d d d d g@ } d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d8 d9 d: d; d< d= d> d? d@ dA dB dC d g@ } n|  dD k rUd d d d d d d d d d d d d d d d d d d d d d d d d d dE dF dG dH dI dJ dK dL dM dN dO dP d d d d d d d d d d d d d dQ dR dS dT dU dV dW dX dY d+ dZ d[ d\ g@ } d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d] d5 d^ d_ d\ d` d+ da dW d% dT db dQ d d d d d d d d d d d d d g@ } d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d dc dd de df dg dh di dj dk dl dm dn do d d d d d d d d d d d d d g@ } n |  dp k r|d d g } d dq g } n{ |  dr k rds |  GHn  d d d d dq d d d dq g	 } d d dq d d d dq d d g	 } dq d d d dq d d d d g	 } dt t  j |  dt t  j |  dt t  j |  f S(u   s   
    Return a colormap as (redmap, greenmap, bluemap)

    Arguments:
    name -- the name of the colormap. If unrecognized, will default to 'jet'.
    i    t   whitei   t   simplet   hotgEQEQ?gEQEQ?gyy?gEQEQ?gYeYe?gyy?grq?gEQEQ?gm۶m?gYeYe?g?gyy?g  ?grq?g00?gEQEQ?geYeY?gm۶m?g  ?gYeYe?g?g?gM4M4?gyy?g?g AA?g$I$I?g#qq?g4M4M?gaa?gXu]u?gQEQE?g<<?geYeY?g78?gضm۶m?gx}?gAA?gba?g,˲,?gAA?gTUUUUU?gii?g}}?gH$I$?gii?g캮뺮?g<<?g88?g}}?g aa?gAA?gii?g$I$I?gu]u]?gqq?g00?g4M4M?gx]u]?gaa?g뺮?gXu]u?t   rainbowg}?gܶm۶m?g88?geYeY?g<<?gTEQE?gau]u?g aa?g4M4M?gqq?g$I$I?g@AA?g@AA?gZk?gfě?g aa?gG'?gh/?geYeY?gțoF?gG'?g(|t?g@:?gXUUUUU?g}}?gii?g}}?g,˲,?gm۶m?g@qq?gAA?g aa?geYeY?gAA?gOUUUUU?gii?g}}?g$I$I?g4M4M?gu]u]?gxy?gqq?g?t   winterg      ?Rq   s8   Warning: colormap "%s" not supported. Using jet instead.g     o@(   R!   R9   (   t   nameR   R   R   (    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyRw     s8    			!!!(   s   .pngs   .jpgs   .jpegs   .bmps   .ppm(   t
   __future__R    R%   t   os.pathR   t	   cStringIOR   t   ImportErrort   numpyR!   t	   PIL.ImageR   t
   scipy.miscR@   t   SUPPORTED_EXTENSIONSR   R1   R   RS   Ra   t   TrueRp   t   FalseRh   Rw   (    (    (    s9   /home/mohanty/data/final_dataset/utils/image_processor.pyt   <module>   s.   	(		P?