a
    -g	                     @   sv   d dl mZ d dlmZmZ d dlmZ d dlZd dl	Z	edddZ
e
  dd	 Zd
d Zedkrreejd  dS )    )image)array_to_imgimg_to_array)
load_modelNzdata/cnn/model.h5F)compilec                 C   s  | d u rt d d S t| tj}t|dd}t|dd}t|tjtj\}}t	|tj
dd}d }|D ]6}dt|d }t||d}	t|	d	krl|	} qql|d urt|\}
}}}| ||| |
|
| f }tt|tj}|S t d
 d S d S )Nz8Error: Unable to load image. Please check the file path.)   r   r   2      T)keyreverseg{Gz?   zID card not found in the image.)printcv2cvtColorCOLOR_BGR2GRAYGaussianBlurCannyfindContoursRETR_EXTERNALCHAIN_APPROX_SIMPLEsortedcontourArea	arcLengthapproxPolyDPlenboundingRectImage	fromarrayCOLOR_BGR2RGB)r   grayZblurrededgesZcontours_Zid_card_contourZcontourepsilonZapproxxywhZcropped_id_cardZcropped_image r'   -/var/www/html/ocr/e-KTP-OCR-CNN/cnn_detect.pycrop_id_card   s,    r)   c                 C   sT   t |  | d}t|}tj|dd}t|}t d t | |d d dkS )N)r	   r	   r   )axiszprediction:)r   resizenparrayexpand_dimssaved_modelpredict)r   imgZ
predictionr'   r'   r(   main=   s    


r2   __main__   )keras.preprocessingr   Zkeras.preprocessing.imager   r   Zkeras.modelsr   numpyr,   r   r/   Zmake_predict_functionr)   r2   __name__sysargvr'   r'   r'   r(   <module>   s   2