o
    Wi                  
   @   s   d dl mZ d dlmZ d dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ dadd
dZdedee fddZdedee fddZddefddZd dedededee fddZd dedee fddZdS )!    )Path)ListN)convert_from_path)ThreadPoolExecutoras_completed)
preprocess)	OCREngine	tesseractenc                 C   s   t d u rt| ||da t S )Npreferlangtesseract_cmd)_enginer   r    r   5/var/www/html/id_ocr_project_final/ocr/pdf_helpers.pyget_shared_engine   s   r   pdf_pathreturnc                 C   s6   zt t| }dd |D W S  ty   g  Y S w )Nc                 S   s   g | ]
}t |  qS r   )boolget_textstrip.0pager   r   r   
<listcomp>   s    z)pdf_has_text_per_page.<locals>.<listcomp>)fitzopenstr	Exceptionr   docr   r   r   pdf_has_text_per_page   s   r"   c                 C   s   t t| }dd |D S )Nc                 S   s   g | ]	}| d p
dqS )text )r   r   r   r   r   r      s    z-pdf_extract_text_per_page.<locals>.<listcomp>)r   r   r   r    r   r   r   pdf_extract_text_per_page   s   r%   Fpage_idxc                 C   s:   t | ||d\}}}}t||d}	|	j||||d}
|
jS )N)
save_debugidx)r   r   )pil_rgbpil_bwnp_rgbnp_gray)r   r   runr#   )	pil_imager&   r   r   r'   r)   r*   r+   r,   engineresr   r   r   ocr_image_page_from_pil   s   r1   ,     dpimax_workersc                    s   t t| |d}d gt| }t|dF  fddt|D }t|D ]+}	||	 }
z|	 ||
< W q' tyR } zd|
 d| d||
< W Y d }~q'd }~ww W d    n1 s]w   Y  dd	 |D S )
N)r4   )r5   c              
      s&   i | ]\}}  t|||qS r   )submitr1   )r   ipilexer   r   r'   r   r   
<dictcomp>&   s   & z,pdf_ocr_pages_via_images.<locals>.<dictcomp>z[ERROR on page z: ]c                 S   s   g | ]}|pd qS )r$   r   )r   tr   r   r   r   -   s    z,pdf_ocr_pages_via_images.<locals>.<listcomp>)r   r   lenr   	enumerater   resultr   )r   r4   r5   r   r   r'   pages
page_textsfuturesfr7   er   r9   r   pdf_ocr_pages_via_images"   s   "rF   c                 C   s   t | } z>t| }|r8t| }t| |||||d}g }	tt|D ]}
|	||
  r/||
 n||
  q!|	W S t| |||||dW S  tyU   t| |||||d Y S w )N)r4   r5   r   r   r'   )	r   r"   r%   rF   ranger>   appendr   r   )r   r4   r5   r   r   r'   
text_flags	raw_texts	ocr_textsmergedr7   r   r   r   process_pdf_pages/   s   $rM   )r	   r
   N)r	   r
   F)r2   r3   r	   r
   F)pathlibr   typingr   r   	pdf2imager   concurrent.futuresr   r   ocr.preprocr   ocr.enginesr   r   r   r   r"   r   r%   intr1   rF   rM   r   r   r   r   <module>   s    
 