
<fT                 @   s   d  d l  Z  d  d l Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l m	 Z	 d  d l m
 Z
 d  d l m Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d S)    N)render)settings)redirect)HttpResponsec             C   s
   t  d  S)Nz(Hello, world. You're at the polls index.)r   )request r   Y/home/adriano.carvalho/ftp/files/ProjetoGabarito/ifmuz/gabarito/gabarito_imagens/views.pyindex   s    r	   c             C   s   t  |  d  S)Nz
index.html)r   )r   r   r   r   home   s    r
   c             C   s   t  |  d  S)Nztransformacao.html)r   )r   r   r   r   	transform   s    r   c             C   s1   g  } x$ t  j |   D] } | j |  q W| S)N)oslistdirappend)	directory	filenamesfilenamer   r   r   read_directories   s    r   c             C   s   t  j |  d d  } t  j | t  j t  j  \ } } t j |   } | r| t | d t  j } t  j	 | | g d d d  |  | | f S)	N2      key         )r   r   r   )
cv2ZCannyZfindContoursZRETR_EXTERNALZCHAIN_APPROX_SIMPLEnpZ
zeros_likemaxcontourAreaZdrawContours)imageedgescontours_contour_imagelargest_contourr   r   r   detecta_borda_principal"   s    !r$   c             C   sl   t  j |   } t  j |  } t j |  } | d } | d k rX d | } | | f S| } | | f Sd  S)Nr   -   Z   )r   ZminAreaRectZ	boxPointsr   Zintp)ZcontourZrectboxZanglnewr   r   r   calculate_main_border_angle5   s    

r)   c             C   s[   |  d \ } } |  d \ } } | | } | | } | | k  rM |  d } n
 |  d } | S)Nr   r   r   )Zvetorxyx1y1ZsomaZsoma1Zmenorr   r   r   retorna_menorE   s    


r.   c             C   sy  |  j  d k r t |  d  S|  j  d k ru|  j j d  } t j   } t j | d  } x| D]} | j } | j	   } t
 j | t
 j  } t j | t j  } t j | d d  }	 t j |	 d  }
 t |
  \ } } } | rt | d
 t j } t |  \ } } n d  } |
 j d  d  \ } } | d | d f } t j | | d  } t j |
 | | | f  } t |  \ } } } t | d
 t j } t |  \ } } t |  } | d } | d } | | d d  | d d  f } | } t j | |  t j d |  \ } } | j | | j    qe WWd  QRX| j d  t | d d } d | d <| Sd  S)NGETztransformacao.htmlPOSTzgabaritos[]w   r     c  r   r   g      ?r   i)  i  z.pngcontent_typezapplication/zipz*attachment; filename="modified_images.zip"zContent-Disposition)r2   r2   )r3   r4   ) methodr   FILESgetlistioBytesIOzipfileZipFilenamereadr   Z
frombufferZuint8r   ZimdecodeZIMREAD_GRAYSCALEZGaussianBlurresizer$   r   r   r)   shapeZgetRotationMatrix2DZ
warpAffiner.   ZimwriteZimencodeZwritestrtobytesseekr   )r   filesZ
zip_bufferZzip_filefiler=   Z
file_bytesZnp_arrr   Zblurred_imageZimage_resizedZoriginal_imager"   r    r#   Zangler'   hr1   centerZrotation_matrixZrotated_imageZminimoZlinhaZcolunaZcropped_rectr   r!   bufferresponser   r   r   	ler_pastaP   sJ    	

$!
rI   )r9   r;   Zdjango.shortcutsr   Znumpyr   r   r   django.confr   r   django.httpr   r	   r
   r   r   r$   r)   r.   rI   r   r   r   r   <module>   s    