o
    o«Ïi  ã                   @   s^   d Z ddlZddlZddlmZ ddlZdd„ Zdd„ Zdd	„ Zd
d„ Ze	dkr-eƒ  dS dS )zè
Created by Jaided AI
Released Date: 19/08/2022
Description:
A wrapper for DCN operator for DBNet. This script is called inside the setup.py
of EasyOCR. It can also be called as a standalone script to compile the operator
manually.
é    N)Údatetimec                    sÜ   t | tƒs| g} dd„ | D ƒ} d}tdƒ t|dƒ0‰ ˆ  t ¡  d¡d  d¡¡ ˆ  d	 d¡¡ ˆ  d
 d¡¡ ‡ fdd„| D ƒ W d   ƒ n1 sMw   Y  td 	t
j |¡¡ƒ tdƒ tdƒ td 	|¡ƒ d S )Nc                 S   s$   g | ]}t |tƒr|n| d ¡‘qS )úutf-8)Ú
isinstanceÚbytesÚencode©Ú.0Úerror© r
   úk/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/easyocr/scripts/compile_dbnet_dcn.pyÚ
<listcomp>   s   $ zprint_error.<locals>.<listcomp>z=https://github.com/JaidedAI/EasyOCR/tree/master/easyocr/DBNetz)Failed to compile dcn operator for DBNet.Úwbú%H:%M:%S - %d %b %YÚ
r   zCFailed to compile dcn operator for DBNet with the following error.
z+##########################################
c                    s   g | ]}ˆ   |¡‘qS r
   )Úwriter   ©Úfidr
   r   r      s    z!Error message can be found in {}.z*##########################################z=EasyOCR can still be used with CRAFT text detector (default).z_To use DBNet text detector, please check {} for troubleshoot and compile dcn operator manually.)r   ÚlistÚprintÚopenr   r   ÚnowÚstrftimer   ÚformatÚosÚpathÚabspath)ÚerrorsÚlog_pathÚurlr
   r   r   Úprint_error   s   
ür   c                 C   sh   t |dƒ!}| t ¡  d¡d  d¡¡ | | d  d¡¡ W d   ƒ n1 s)w   Y  t| ƒ d S )Nr   r   r   r   )r   r   r   r   r   r   r   )Útextr   r   r
   r
   r   Úprint_success   s
   þr!   c           	      C   sØ   t j | dddd¡}|dkr9t t j |d¡¡}t t j |d¡¡}d t j t j | d¡¡¡}t||ƒ |o8|S |d	krht t j |d
¡¡}t t j |d¡¡}d t j t j | d¡¡¡}t||ƒ |og|S tdƒ‚)NÚDBNetÚassetsÚopsÚdcnÚcpuzdeform_conv_cpu.*.sozdeform_pool_cpu.*.soz0DCN CPU operator is compiled successfully at {}.Úcudazdeform_conv_cuda.*.sozdeform_pool_cuda.*.soz1DCN CUDA operator is compiled successfully at {}.z,'cpu_or_cuda' must be either 'cpu' or 'cuda')r   r   ÚjoinÚglobr   r   r!   Ú
ValueError)	Ú
parent_dirr   Úcpu_or_cudaÚdcn_dirÚconv_cpu_existÚpool_cpu_existÚsuccess_messageÚconv_cuda_existÚpool_cuda_existr
   r
   r   Úvalidate_compilation$   s   

r3   c               
   C   sB  t  ¡ } t j t j t j t¡¡¡}t j |dd¡}z€zUtdƒ t  t j |dddd¡¡ t	j
dddd	}|jd
krat  t j |d¡¡ t||dƒrRt	j
dddd	}t||dƒr`t	j
dddd	}nt|jƒ t|j|jg|ƒ W n tyŠ } ztd |¡|ƒ W Y d }~nd }~ww W t  | ¡ d S W t  | ¡ d S t  | ¡ w )Nr"   zlog.txtzCompiling DCN operators...r#   r$   r%   z#python setup.py build_ext --inplaceT)ÚshellÚcapture_outputr   r&   ztouch dcn_cpu_compiling_successr'   z touch dcn_cuda_compiling_successz{})r   Úgetcwdr   Údirnamer   Ú__file__r(   r   ÚchdirÚ
subprocessÚrunÚ
returncoder3   Ú__dict__r   ÚstdoutÚstderrÚ	Exceptionr   )Úcwdr+   r   Úresultr	   r
   r
   r   Úmain6   s>   ÿ
ÿÿ€
€€ÿ€þrC   Ú__main__)
Ú__doc__r   r)   r   r:   r   r!   r3   rC   Ú__name__r
   r
   r
   r   Ú<module>   s    
ÿ