
    j5j'                       d Z ddlm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lmZ ddlZddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8  ejr                  e:      Z;	 	 	 	 	 	 ddZ<ddZ=ddZ>	 	 	 	 	 	 ddZ?	 	 	 	 	 	 ddZ@	 	 	 	 	 	 ddZAy)zEImplements the concurrent and page synchronous parts of the pipeline.    )annotationsN)Sequence)partial)Path)mkdtemp)Executor)
OcrGrafter)PageContext
PdfContext)
OcrOptions)
copy_finalis_ocr_requiredmerge_sidecarsocr_engine_directocr_engine_hocrocr_engine_textonly_pdftriagevalidate_pdfinfo_options)
PageResultcli_exception_handlerdo_get_pdfinfomanage_debug_log_handlermanage_work_folderpostprocessprocess_pagereport_output_pdfset_thread_pagenosetup_pipelineworker_init)OcrmypdfPluginManager)ProgressBar)check_requested_output_filecreate_input_file)ExitCode)available_cpu_count)
OcrElementc                .   | j                   }|j                  }|dv rR| j                  j                  |      }|r$|j	                         rt        ||       \  }}d||fS t        ||       \  }}n#|dk(  rt        ||       \  }}nt        d|       ||dfS )z8Run OCR engine on image to create OCR PDF and text file.)autofpdf2)optionsNsandwichzpdf_renderer )	r*   pdf_rendererplugin_managerget_ocr_enginesupports_generate_ocrr   r   r   NotImplementedError)page_contextocr_image_outr*   r,   
ocr_engineocr_treetext_outocr_outs           M/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/_pipelines/ocr.py_image_to_ocr_textr8   :   s     ""G''L ((!00???P
*::<!2=,!OHh8+++M<H		#3M<P!M,"@AAHd""    c                    t        | j                  dz          t        |       st        | j                        S t	        |       \  }}}t        | |      \  }}}t        | j                  |||||      S )z3Execute a pipeline for a single page synchronously.   )pageno)r<   pdf_page_from_imageocrtextorientation_correctionr4   )r   r<   r   r   r   r8   )r1   r2   pdf_page_from_image_outr@   r6   r5   r4   s          r7   _exec_page_syncrB   P   s    l))A-.<(!4!455EQFBM*,B #5\="QGXx""35 r9   c           
     v  	
 | j                   }|j                  xs
 t               }t        t	        | j
                        |      }|dkD  rt        j                  d|       dgt	        | j
                        z  
t        |       	d	
fd} ||j                  |t        t	        | j
                        |j                  dk7  rdndd|j                   	      t        t        t        j                   j"                        t$        | j'                         |
       |j(                  r-t+        
|       }t-        ||j(                  |j.                         	j1                         }g }|j2                  dk7  rFt        j                  d       t5        || |      \  }}t-        ||j6                  |j.                         |S )z&Execute the OCR pipeline concurrently.r;   z0Starting processing with %d workers concurrentlyNc                t   	 t        | j                  dz          | j                  | j                  <   |j                  d       j	                  | j                  | j
                  | j                  | j                  | j                         |j                  d       t        d       y# t        d       w xY w)z1After OCR is complete for a page, update the PDF.r;   g      ?)r<   image
ocr_outputr4   autorotate_correctionN)	r   r<   r?   update
graft_pager=   r>   r4   r@   )resultpbarocrgraftsidecarss     r7   update_pagez$exec_concurrent.<locals>.update_pagep   s    	$fmma/0&,kkHV]]#KK}}00!::&,&C&C    KKd#d#s   BB* *B7noneOCRzImage processingpage)totaldescunitdisable)use_threadsmax_workersprogress_kwargsworker_initializertasktask_argumentstask_finishedzPostprocessing...)rJ   r   rK   r!   )r*   jobsr%   minlenpdfinfologinfor	   rV   dictr3   progress_barr   r   PILImageMAX_IMAGE_PIXELSrB   get_page_context_argssidecarr   r   
input_filefinalizeoutput_typer   output_file)contextexecutorr*   r]   rW   rN   r?   pdfmessagesrL   rM   s            @@r7   exec_concurrentrr   e   sm   ooG<<0.0Dc'//*D1KQC[Q#'&3w+?"?H'"H$" ''goo&!,,6<N,,,	
 #;		0J0JK446!  h04'*<*<= 


C Hf$$%#C(;X 	3++W-?-?@Or9   c           	        t        t        t        d            | j                  | j                        5 }t	        | |      5  t        | |      }t        |        t        | |      \  }}t        |||dz  |       }t        |||       }t        | ||||      }t        |       t        ||      }	t        | ||	      }
|
cd d d        cd d d        S # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)Nzocrmypdf.io.)prefix)work_folderretainprint_location)r*   ru   z
origin.pdf)r   r   r   keep_temporary_filesr   r   r"   r#   r   r   r   r   rr   r   )r*   r-   ru   ro   start_input_fileoriginal_filename
origin_pdfr`   rn   optimize_messagesexitcodes              r7   _run_pipeliner~      s    
 	WN;<//"77	

  kJ "'>:#G,.?.U++ /|1KW


 !Xw?Wk:wW 	!) ,GX>$W.>@QR9      s$   C%A9C=	C%C	C%%C.c               $    t        t        | |      S )zRun the OCR pipeline with command line exception handling.

    Args:
        options: The parsed OCR options.
        plugin_manager: The plugin manager to use. If not provided, one will be
            created.
    )r   r~   r*   r-   s     r7   run_pipeline_clir      s     !HHr9   c                   t        | |      S )zRun the OCR pipeline without command line exception handling.

    Args:
        options: The parsed OCR options.
        plugin_manager: The plugin manager to use. If not provided, one will be
            created.
    )r~   r   s     r7   run_pipeliner      s     .11r9   )r1   r
   r2   r   returnz+tuple[Path | None, Path, OcrElement | None])r1   r
   r   r   )rn   r   ro   r   r   zSequence[str])r*   r   r-   r    r   r$   )B__doc__
__future__r   logginglogging.handlerscollections.abcr   	functoolsr   pathlibr   tempfiler   re   ocrmypdf._concurrentr   ocrmypdf._graftr	   ocrmypdf._jobcontextr
   r   ocrmypdf._optionsr   ocrmypdf._pipeliner   r   r   r   r   r   r   r   ocrmypdf._pipelines._commonr   r   r   r   r   r   r   r   r   r   r   ocrmypdf._plugin_managerr    ocrmypdf._progressbarr!   ocrmypdf._validationr"   r#   ocrmypdf.exceptionsr$   ocrmypdf.helpersr%   ocrmypdf.models.ocr_elementr&   	getLogger__name__ra   r8   rB   rr   r~   r   r    r9   r7   <module>r      s
  
 L "   $    
 ) & 8 (	 	 	    ; - ) 0 2g!##.2#0#,*<~  )   FII *I 	I22 *2 	2r9   