U
    fA                     @   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.htmlr   r   r
   r
   r   home   s    r   c                 C   s
   t | dS )Ntransformacao.htmlr   r   r
   r
   r   	transform   s    r   c                 C   s"   g }t | D ]}|| q|S )N)oslistdirappend)	directory	filenamesfilenamer
   r
   r   read_directories   s    r   c                 C   s^   t | dd}t |t jt j\}}t| }|rTt|t jd}t 	||gddd | ||fS )N2      key)   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   sP   t | }t |}t|}|d }|dkr@d|  }||fS |}||fS d 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   sF   | d \}}| d \}}|| }|| }||k r:| d }n| d }|S )Nr      r
   )Zvetorxyx1y1ZsomaZsoma1Zmenorr
   r
   r   retorna_menorE   s    
r3   c              	   C   s  dd l }|  | jdkr$t| dS | jdkr| jd}t }t	|dD}|D ]6}|
 }t|tj}t|tj}t|dd}	t|	d}
t|
\}}}|rt|tjd	}t|\}}nd }|
jd d
 \}}|d
 |d
 f}t||d}t|
|||f}t|\}}}t|tjd	}t|\}}t|}|d }|d }||d d|d df }td|\}}||j |  qXW 5 Q R X |d t |dd}d|d< |S d S )Nr   GETr   POSTzgabaritos[]w)   r7   )i  ic  r   r   g      ?r.   r7   i)  i  z.pngzapplication/zip)content_typez*attachment; filename="modified_images.zip"zContent-Disposition)!pdbZ	set_tracemethodr   FILESgetlistioBytesIOzipfileZipFilereadr   Z
frombufferZuint8r   ZimdecodeZIMREAD_GRAYSCALEZGaussianBlurZresizer(   r    r!   r-   shapeZgetRotationMatrix2DZ
warpAffiner3   ZimencodeZwritestrnametobytesseekr   )r	   r9   filesZ
zip_bufferZzip_filefileZ
file_bytesZnp_arrr"   Zblurred_imageZimage_resizedZoriginal_imager&   r$   r'   Zangler+   hr6   centerZrotation_matrixZrotated_imageZminimoZlinhaZcolunaZcropped_rectr%   bufferresponser
   r
   r   	ler_pastaP   sH     


 
rL   )r=   r?   Zdjango.shortcutsr   Znumpyr   r   r   django.confr   r   django.httpr   r   r   r   r   r(   r-   r3   rL   r
   r
   r
   r   <module>   s    