a
    gd4                     @   s   z$d dl mZ d dlmZ d dlZW n ey>   d dlZY n0 d dlZd dlmZ d dlZd dl	Z	d dl
mZ d dlT d dlZd dlZd dlZd dlT d dlZd dlmZ d dlmZ d	Zd
d ZG dd dZG dd deZdS )    )Image)uuid4N)Output)convert_from_path)*)datetime)CounterFc                   C   s   dS )N  r
   r
   r
   W/home/leo/remi/toosmart/soft/v2017b/4_specific/TOOSMART_DIGICOCHE/2_tools/ocr/Charal.pygetTime   s    r   c                   @   s0   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )bcolorsz[95mz[94mz[96mz[92mz[93mz[91mz[0mz[1mz[4mN)__name__
__module____qualname__HEADEROKBLUEOKCYANOKGREENWARNINGFAILENDCBOLD	UNDERLINEr
   r
   r
   r   r      s   r   c                   @   s   e Zd ZdZdZdZdZdZg Zg Z	i Z
dZdZdZdZdZdZd	ZdZg ZdZdZdZdZdZd
Zg Zdd Zdd Zdd Zdd Zdd ZdS )Charaltoosmartr	   i5Hf7MSHKkPN1hy   r   g        zTRUIE LBC VPFi^  z,/home/serge/www/toosmart_digicoche/data/tmp/Fc                 C   s   zl|| _ || _tj| j| j| j| j d| _| j | _|| _|| _	|| _
| d| j   | dt|  W n&   | d| j  d | j  Y n0 d S )N)hostuserpasswddbu$   Connexion à la base de données OK zoutputFileName = u+   Erreur de connexion à la base de données  )sqlDbsqlHostMySQLdbconnectsqlUser	sqlPasswdconncursorpdfoutputFileName	ocrFolder
printDebugstr)selfr+   r,   ocrDirectorydbNamer$   r
   r
   r   __init__F   s    zCharal.__init__c                 C   s    | j d | | _ trt| d S )Nz<br />)	_debugOCRdebugprint)r0   pTxtr
   r
   r   r.   W   s    zCharal.printDebugc                 C   s  zg }t | j| j}d}g }W nH tyf } z0| d|dt| | d W Y d }~n
d }~0 0 |D ]}zTdt| d }|| jd t| d d t	
| jd t| d }| }	t	|t	j}
t	|
dd	t	jt	j d
 }t	t	jd}t	j|t	j|d
d}t	|t	jt	j}t|dkr<|d n|d
 }|D ]}t	|	|gddd
 qHtt|	}t	| jd t| d |	 || |t !| jd t| d  |d
 }W ql ty } z0| d|dt| | d W Y d }~qld }~0 0 ql| d|d   z8t"#d|d t"j$}t|dkr^|D ]}|| _%qPW nP ty } z6d| _%| d|dt| | d W Y d }~n
d }~0 0 | j%dkr| & rt'd d| _(dS t"#d|d }t|dkrt|d | _)t"*| j+|d }|d ur.|, | _-nt'd d| _(dS zRt"#d|d t"j$}t|dkr|D ]&}t.t/|d d | _0|d
 | _1qhW nP ty } z6d| _0| d|dt| | d W Y d }~n
d }~0 0 d}|D ]}| j-dkrTt"#d|}t|dkr,t|| _2| j34| t"#d|}t|dkrTt|d | _5z&tj6|| tj7j8d}| 9|| W n   | d Y n0 |d
 }qz8t !| j| j: d }|j| j| j: d  d| j;d! W n   | d Y n0 d S )"Nr   Unexpected err=, type(err)=ERRORPage_.jpgJPEG         )(   r@   )
iterations   )r?   r?   r?   _result.jpgz1 : u   BL[ ]?N°[ ]?([0-9]+)r	   zUnexpected1 err=ERROR1z{"blEnDoublon":1}TFz+Livraison du ([0-9]{2}/[0-9]{1,2}/[0-9]{2})z{"blNonIntegrable":1}z^([0-9]+)\D?CR\D?([0-9,]+)$z(0000[0-9]{4})z([0-9]{8}.*([0-9]{2}/[0-9]{1,2}/[0-9]{4}))output_typez-color-0.jpgz-BL.pdf)save_allappend_images)<r   r+   ocrDpi	Exceptionr.   typer/   saver-   cv2imreadcopycvtColorCOLOR_BGR2GRAY	thresholdTHRESH_BINARY_INVTHRESH_OTSUgetStructuringElement
MORPH_RECTmorphologyEx
MORPH_OPENfindContoursRETR_EXTERNALCHAIN_APPROX_SIMPLElendrawContourspytesseractimage_to_stringnparrayimwriteappendr   openrefindall	MULTILINE_numBlblExistr6   nonIntegrabledateLivraisonsearchcleCochegrouptypeLivraisonroundintqttCocheDetectepoidsTotalDetecteqttCocheCalcule	linesLotsextend
dateTuerieimage_to_datar   DICTsurlignerZonesr,   mergedImage)r0   imageAmelioreeSurligneepagesPdfiPageocrReturnArrayerrpage
image_nameimgresultgraythreshhorizontal_kernelremove_horizontalcntscdfmelementjSfiletesseract_outputimr
   r
   r   read]   s     




"" 

 
"zCharal.readc                 C   s  z$t | jd t| d }g }W nD tyh } z,td|dt| td W Y d }~n
d }~0 0 t|d D ]R\}}|dkrvd|d	 |  d|d
 |  d|d |  d|d |  d|d | |d | d}t|d | dkrd}	n t|d | dkrd}	nd}	zl|	 }
t 
|
|d |d f|d |d  |d |d  f|	t j d}t |
||d| d}|| W qv ty } z,td|dt| td W Y d }~qvd }~0 0 qvzzt |t j}t|d}|j\}}|d |d f}||}|dkr"| j| || j| j d t| d  W nF ty } z,td td |dt| W Y d }~n
d }~0 0 d!S )"Nr;   rE   zUnexpectedC err=r9   ERRORClevel   r@   lefttopwidthheightr   conftext)xyr   r   rotation
confidencer   U   )*         2   )r      r?   )r   r   r?   r   r   g      ?zUnexpectedD err=ERRORDRGB   z-color-r<   erreurzUnexpectedE err=T)rN   rO   r-   r/   rK   r6   rL   	enumeraterr   rP   	rectangleFILLEDaddWeightedrd   rQ   COLOR_BGR2RGBr   	fromarraysizeresizer|   rM   r,   )r0   r   r   imageASurlignerblockASurlignerr   i	level_idxbboxcoloroverlayalphar   r   r   new_sizeresized_imager
   r
   r   r{      sZ    


$ 


&.zCharal.surlignerZonesc              
   C   sB  | j r
dS zt| jdd| _W n   d| _Y n0 zt| jdd| _W n   d| _Y n0 zt d}| j| _dt	| d t	| j
 d	 | j d
 t	| j d t	| j d t	| j d t	| j d t	| j d
 | j d | j d | jdd d }z| j| W nF tyj } z,td|dt| td W Y d }~n
d }~0 0 | jj}tdt	| d  | jD ]V}t	|dd  }dt	| d t	| d t	| j d | j d }| j| q| j  W nJ ty< } z0| d|dt| | d W Y d }~n
d }~0 0 d S )NFz%d/%m/%Yz%Y%m%dr	   z%d/%m/%yz
%Y%m%d%H%MzINSERT INTO tier_att(id_base, id_type, id_tier,attdatcre, id_work, actif, newobj, att1, att2, att3,att6,att7,att8,att9,attf100,att17,att90) VALUES (1, 10, NULL,'z', 16, 1, 0, z, 'z', 'z',''z\'z');zUnexpectedA err=r9   zERROR A2z{"blId":}zINSERT INTO prod_att(id_base, id_type, id_prod, id_work, actif, newobj, fid_prod, type, name, revision, `desc`, att1, att2, att3, att4, attf100, attf101,attf104) VALUES (1, 11, NULL, 11, 1, 0, null, null, null, null, null, 'z','', 'z', '', 'z	', '0', 'r8   r:   )rk   r   strptimerx   strftimerl   nowrt   poidsTotalCalculer/   
idAbattoirrp   rs   ru   ri   r4   replacer*   executerK   r6   rL   	lastrowidrv   r)   closer.   )r0   currentDatesqlr   blIdkeynumeroTuerier
   r
   r   	mergeData)  s    
zCharal.mergeDataN) r   r   r   r'   r$   r(   r#   r   
linesPoidsrv   
lineMergedrl   rp   
enAnomalie
poidsTotalrn   rJ   r-   r,   motsASurlignerrx   r   rt   ru   rs   rk   r|   r3   r.   r   r{   r   r
   r
   r
   r   r   *   s<    8r   )PILr   uuidr   jsonImportErrorr_   r   rN   rf   	pdf2imager   TSLibOCRtimeosnumpyra   TS_OCRr%   r   collectionsr   r5   r   r   r   r
   r
   r
   r   <module>   s,   