Ë
    ðì�i%  ã                   ó¸   — d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlZddlmZ ddlZddlmZmZ ddlZddlmZmZ d„ Zd	„ Zd
„ Zd„ Zy)z�
PassportEye::MRZ: Machine-readable zone extraction and parsing.
Command-line scripts

Author: Konstantin Tretyakov
License: MIT
é    N)ÚCounter)Úio)ÚTesseractNotFoundErrorÚTesseractErroré   )Úread_mrzÚMRZPipelinec                 óŠ   — t        j                   «       }| \  }}}t        |||¬«      }t        j                   «       |z
  }|||fS )zï
    Processes a file and returns the parsed MRZ (or None if no candidate regions were even found).

    The input argument is a list (filename, save_roi, extra_params).
    (Because we need to use this function within imap_unordered)
    )Úsave_roiÚextra_cmdline_params)Útimer   )ÚparamsÚticÚfilenamer   Úextra_paramsÚresultÚwalltimes          ú|/var/www/ITT-ServiceAPI-Services/ITT-Document-Scan/mrzscanner/mrzenv/lib/python3.12/site-packages/passporteye/mrz/scripts.pyÚprocess_filer      sF   € ô �)‰)‹+€CØ'-Ñ$€Hˆh˜Ü�h¨ÈÔU€FÜ�y‰y‹{˜SÑ €HØ�f˜hÐ'Ð'ó    c                  ó8  ‡— t        j                  d¬«      } | j                  dddt        d¬«       | j                  dd	t	        j
                  d
d«      d¬«       | j                  dddd¬«       | j                  dddd¬«       | j                  dddd¬«       | j                  dddt        d¬«       | j                  ddd¬«       | j                  «       }t        t        j                  t        j                  j                  |j                  d «      «      «      }|j                  d!k\  r|d!|j                   }t        j                  t        j                   ¬"«       t        j"                  d#«      }t%        j$                  «       }t'        j(                  |j*                  «      }|j-                  d$t/        |«      |j                  «       |j-                  d%|j*                  «       g }|j0                  du}|j2                  |j4                  |j0                  fD ]:  }|€Œt        j                  j7                  |«      rŒ&t        j8                  |«       Œ< d&„ Šˆfd'„}	t;        «       }
|j<                  rd(nd)}|j?                  t@        |D �cg c]  }|||f‘Œ	 c}«      D �]  }|\  }}}|jC                  |«       |j-                  d*t        j                  jE                  |«      | ‰|«       |	||«      «       |jG                  d+|«        ‰|«      }|j2                  �e|d!kD  r`tI        jJ                  |t        j                  j                  |j2                  d,|t        j                  jE                  |«      fz  «      «       |j4                  �e|d!k(  r`tI        jJ                  |t        j                  j                  |j4                  d,|t        j                  jE                  |«      fz  «      «       |j0                  �|�}d-|jL                  v rod.|t        j                  jE                  |«      fz  }tO        jP                  t        j                  j                  |j0                  |«      |jL                  d-   «       |d!kD  s�Œèd/|jL                  v s�Œø|
|jL                  d/   xx   dz  cc<   �Œ t/        |«      }|D ���cg c]  \  }}} |	||«      ‘Œ }}}}|D ���cg c]  \  }}} ‰|«      ‘Œ }}}}|jS                  d0«      }|jS                  d!«      }tU        |«      }tU        |D ���cg c]  \  }}}|‘Œ
 c}}}«      }t%        j$                  «       |z
  }|j-                  d1«       tW        d2|z  «       tW        d3|z  «       tW        d4|z  «       tW        d5|z  «       tW        d6|z  «       tW        d7t/        |D �cg c]
  }|d8k(  sŒ	|‘Œ c}«      z  «       tW        d9t/        |D �cg c]
  }|d:k(  sŒ	|‘Œ c}«      z  «       tW        d;|z  «       tW        d<tY        |«      |z  z  «       tW        d=||z  z  «       tW        d>«       |
j[                  «       D ]  }tW        d?|z  «       Œ yc c}w c c}}}w c c}}}w c c}}}w c c}w c c}w )@z—
    A script for evaluating the current MRZ recognition pipeline by applying it to a list of files in a directory and reporting how well it went.
    z]Run the MRZ OCR recognition algorithm on the sample test data, reporting the quality summary.©Údescriptionz-jz--jobsr   zNumber of parallel jobs to run)ÚdefaultÚtypeÚhelpz-ddz
--data-dirzpassporteye.mrzÚtestdataz@Read files from this directory instead of the package test files©r   r   z-sdz--success-dirNzOCopy files with successful (nonzero score) extraction results to this directoryz-fdz
--fail-dirzNCopy files with unsuccessful (zero score) extraction resutls to this directoryz-rdz	--roi-dirzExtract ROIs to this directoryz-lz--limitéÿÿÿÿz6Only process the first <limit> files in the directory.ú--legacyÚ
store_trueák  Use the "legacy" Tesseract OCR engine (--oem 0). Despite the name, it most often results in better results. It is not the default option, because it will only work if your Tesseract installation includes the legacy *.traineddata files. You can download them at https://github.com/tesseract-ocr/tesseract/wiki/Data-Files#data-files-for-version-400-november-29-2016©Úactionr   z*.*r   )ÚlevelÚevaluate_mrzz*Preparing computation for %d files from %szRunning %d workersc                 ó"   — | €dS | j                   S )Nr   )Úvalid_score)Úmrz_s    r   r(   z!evaluate_mrz.<locals>.valid_scoreN   s   € Ø�LˆqÐ6 d×&6Ñ&6Ð6r   c                 óÒ   •— 	  ‰|«      }t        t        j                  j                  | «      j	                  d«      d   «      }||z
  }|dk(  rdS |dkD  rdS dS # t
        $ r Y yw xY w)NÚ_r   ú=ú>ú<ú?)ÚintÚosÚpathÚbasenameÚsplitÚ	Exception)r   r)   Ú	new_scoreÚ	old_scoreÚschanger(   s        €r   Úscore_change_typez'evaluate_mrz.<locals>.score_change_typeQ   ss   ø€ ð	Ù# DÓ)ˆIÜœBŸG™G×,Ñ,¨XÓ6×<Ñ<¸SÓAÀ!ÑDÓEˆIØ )Ñ+ˆGØ! Qš,�3ÐI°G¸a²K¨SÐIÀSÐIøÜò 	Ùð	ús   ƒAA ÁA ÁA Á	A&Á%A&ú--oem 0Ú z&Processed %s in %0.2fs (score %d) [%s]z	%sz%d_%sÚroiz%d_roi_%s.pngÚmethodéd   Ú	CompletedzWalltime:          %0.2fszCompute walltime:  %0.2fszProcessed files:   %dzPerfect parses:    %dzInvalid parses:    %dzImproved parses:   %dr-   zWorsened parses:   %dr.   zTotal score:       %dzMean score:        %0.2fzMean compute time: %0.2fszMethods used:z  %s: %d).ÚargparseÚArgumentParserÚadd_argumentr0   Úpkg_resourcesÚresource_filenameÚ
parse_argsÚsortedÚglobr1   r2   ÚjoinÚdata_dirÚlimitÚloggingÚbasicConfigÚINFOÚ	getLoggerr   ÚmultiprocessingÚPoolÚjobsÚinfoÚlenÚroi_dirÚsuccess_dirÚfail_dirÚisdirÚmkdirr   ÚlegacyÚimap_unorderedr   Úappendr3   ÚdebugÚshutilÚcopyfileÚauxr   ÚimsaveÚcountÚsumÚprintÚfloatÚmost_common) ÚparserÚargsÚfilesÚlogr   ÚpoolÚresultsr   Údr9   Úmethod_statsr   Úfr   r   r)   r   ÚvsÚroi_fnÚ	num_filesÚfnÚwtÚscore_changesÚscoresÚnum_perfectÚnum_invalidÚtotal_scoreÚtotal_computation_walltimeÚtotal_walltimeÚxÚstatr(   s                                   @r   r&   r&   (   s¾  ø€ ô ×$Ñ$ð  2Qô  R€FØ
×Ñ˜˜h°¼ÐBbÐÔcØ
×Ñ˜˜|´]×5TÑ5TÐUfÐhrÓ5sØ_ð ô aà
×Ñ˜˜¸Ønð ô pà
×Ñ˜˜|°TØmð ô oà
×Ñ˜˜{°DØ=ð ô ?à
×Ñ˜˜i°¼#ÐD|ÐÔ}Ø
×Ñ˜
¨<ðAð ô Bð
 ×ÑÓ€DÜ”4—9‘9œRŸW™WŸ\™\¨$¯-©-¸Ó?Ó@ÓA€EØ‡z�z�Q‚Ø�a˜Ÿ
™
Ð#ˆä×ÑœgŸl™lÕ+Ü
×
Ñ
˜NÓ
+€Cä
�)‰)‹+€CÜ×Ñ §	¡	Ó*€DØ‡H�HÐ9¼3¸u»:ÀtÇ}Á}ÔUØ‡H�HÐ! 4§9¡9Ô-Ø€GØ�|‰| 4Ð'€HØ×Ñ §¡¨t¯|©|Ð<ò ˆØ‰=¤§¡§¡¨qÕ!1Ü�H‰H�Q�Kðò7ôô “9€Là $§¢‘9°€LØ×%Ñ%¤lÐZ_Ö4`ÐUV°a¸À<Ò5PÒ4`Óaó 2ˆØ#)Ñ ˆ�$˜Ø�‰�vÔØ�‰Ð9¼2¿7¹7×;KÑ;KÈHÓ;UÐW_ÑalÐmqÓarñ  uFð  GOð  QUó  uVô  	WØ�	‰	�&˜$Ôá˜ÓˆØ×ÑÐ'¨B°ªFÜ�O‰O˜H¤b§g¡g§l¡l°4×3CÑ3CÀWÐPRÔTV×T[ÑT[×TdÑTdÐemÓTnÐOoÑEoÓ&pÔqØ�=‰=Ð$¨¨qªÜ�O‰O˜H¤b§g¡g§l¡l°4·=±=À'ÈRÔQS×QXÑQX×QaÑQaÐbjÓQkÐLlÑBlÓ&mÔnØ�<‰<Ð#¨Ð(8¸UÀdÇhÁhÑ=NØ$¨¬B¯G©G×,<Ñ,<¸XÓ,FÐ'GÑGˆFÜ�I‰I”b—g‘g—l‘l 4§<¡<°Ó8¸$¿(¹(À5¹/ÔJà�Œ6�h $§(¡(Ó*Ø˜Ÿ™ (Ñ+Ó,°Ñ1Õ,ð!2ô$ �G“€IØEL×MÐM±\°R¸¸rÑ& r¨4Õ0ÐM€MÒMØ4;×<Ð<¡L B¨¨b‰k˜$ÕÐ<€FÒ<Ø—,‘,˜sÓ#€KØ—,‘,˜q“/€KÜ�f“+€KÜ!$¸W×%EÐ%E©\¨R°°r¢bÔ%EÓ!FÐÜ—Y‘Y“[ 3Ñ&€NØ‡H�Hˆ[ÔÜ	Ð
%¨Ñ
6Ô7Ü	Ð
%Ð(BÑ
BÔCÜ	Ð
! IÑ
-Ô.Ü	Ð
! KÑ
/Ô0Ü	Ð
! KÑ
/Ô0Ü	Ð
!¤C°MÖ(N¨qÀQÈ#ÃXªÒ(NÓ$OÑ
OÔPÜ	Ð
!¤C°MÖ(N¨qÀQÈ#ÃXªÒ(NÓ$OÑ
OÔPÜ	Ð
! KÑ
/Ô0Ü	Ð
$¬¨kÓ(:¸9Ñ(DÑ
EÔFÜ	Ð
%Ð)CÀIÑ)MÑ
NÔOÜ	ˆ/ÔØ×(Ñ(Ó*ò !ˆÜˆj˜4ÑÕ ñ!ùòM 5aùô& NùÜ<ùô &Fùò )OùÚ(Ns0   ÊY8
Ó	Y=Ó'ZÔ6Z×	
Z×Z×4
Z×?Zc            	      óÀ  — t        j                  d¬«      } | j                  d«       | j                  ddd¬«       | j                  ddd	¬«       | j                  d
ddd¬«       | j                  dddt        j                  z  ¬«       | j                  «       }	 |j                  rdnd}t        |j                  |j                  du|f«      \  }}}�|j%                  «       nddddœ}|d<   |d<   |j                  �]|�[d|j&                  v rMt)        j*                  |j                  |j&                  d   dz  j-                  t.        j0                  «      «       |j2                  s4|D ].  }t5        |›dt7        ||   «      j9                  dd «      ›�«       Œ0 yt5        t3        j:                  |d!¬"«      «       y# t        $ r8 t        j                  j                  d«       t        j                  d«       Y �Œ,t        $ rV}t        j                  j                  d|j                   z  «       t        j                  |j"                  «       Y d}~�Œ…d}~ww xY w)#zC
    Command-line script for extracting MRZ from a given image
    z9Run the MRZ OCR recognition algorithm on the given image.r   r   z--jsonr!   z)Produce JSON (rather than tabular) outputr#   r    r"   z-rz
--save-roiNzYOutput the region of the image that is detected to contain the MRZ to the given png file.r   ú	--versionÚversionzPassportEye MRZ v%s©r$   r   r:   r;   z¥ERROR: The tesseract executable was not found.
Please, make sure Tesseract is installed and the appropriate directory is included in your PATH environment variable.
r   z	ERROR: %sFr   )Úmrz_typeÚvalidr(   r   r<   éÿ   ú	ú
ú é   )Úindent)r@   rA   rB   ÚpassporteyeÚ__version__rE   rY   r   r   r   r   ÚsysÚstderrÚwriteÚexitr   ÚmessageÚstatusÚto_dictr_   r   r`   ÚastypeÚnpÚuint8Újsonrc   ÚstrÚreplaceÚdumps)	rf   rg   r   r   r)   r   Úexrl   Úks	            r   Úmrzr›   ‡   s  € ô ×$Ñ$Ð1lÔm€FØ
×Ñ˜
Ô#Ø
×Ñ˜¨Ð<gÐÔhØ
×Ñ˜
¨<ðAð ô Bð
 ×Ñ˜˜l°DØxð ô zà
×Ñ˜¨IÐ?TÔWb×WnÑWnÑ?nÐÔoØ×ÑÓ€Dð
Ø$(§K¢K‘y°RˆÜ#/°·±ÀÇÁÐUYÐ@YÐ[gÐ0hÓ#iÑ ˆ�$˜ð Ð*ˆ�‰Œ¸TÈEÐbcÑ0d€AØ€A€j�MØ€A€j�Mà‡}�}Ð  TÐ%5¸%À4Ç8Á8Ñ:KÜ
�	‰	�$—-‘- $§(¡(¨5¡/°CÑ"7×!?Ñ!?ÄÇÁÓ!IÔJà�9Š9Øò 	@ˆAÜša¤ Q q¡T£×!2Ñ!2°4¸Ô!=Ð>Õ?ñ	@ô 	Œd�j‰j˜ 1Ô%Õ&øô) "ò Ü�
‰
×Ñð @ô 	Aô 	�‰��Üò Ü�
‰
×Ñ˜ r§z¡zÑ1Ô2Ü�‰�—‘×Òûðús   Â8F? Æ?=IÇ?IÈAIÉIc                  ó„  — t        j                  d¬«      } | j                  d«       | j                  dddd¬«       | j                  d	d
dd¬«       | j                  dddt        j                  z  ¬«       | j                  «       }t        |j                  «      d   }|j                  r!t        j                  |j                  d¬«       t        |d«      D ]f  \  }}t        j                  t        j                  j!                  |j                  d|z  «      |dz  j#                  t$        j&                  «      «       Œh y)zc
    Command-line script for extracting all potential MRZ regions from a given image as images
    zXExtract all potential MRZ regions from a given image and output as individual PNG files.r   r   z-dz--output-dirú.z¡The directory where the images for the potential MRZ regions will be output (default is the current directory). The files created will be named 1.png, 2.png, ...r   z-cz--create-dirr!   zKForce the creation of the directory (and all parents) if it does not exist.r#   r~   r   zPassprtEye extract_mrz_rois v%sr€   ÚroisT)Úexist_okr   z%d.pngrƒ   N)r@   rA   rB   r‰   rŠ   rE   r	   r   Ú
create_dirr1   ÚmakedirsÚ
output_dirÚ	enumerater   r`   r2   rH   r’   r“   r”   )rf   rg   rž   ÚnÚimgs        r   Úextract_mrz_roisr¦   ²   s  € ô ×$Ñ$ð  2Lô  M€FØ
×Ñ˜
Ô#Ø
×Ñ˜˜n°cðLð ô Mð ×Ñ˜˜n°\ð  IVÐô  WØ
×Ñ˜¨IÐ?`Ôcn×czÑczÑ?zÐÔ{Ø×ÑÓ€Dä�t—}‘}Ó% fÑ-€DØ‡‚Ü
�‰�D—O‘O¨dÕ3ä˜D !Ó$ò ]‰ˆˆ3Ü
�	‰	”"—'‘'—,‘,˜tŸ™°¸1±Ó=ÀÀcÁ	×?QÑ?QÔRT×RZÑRZÓ?[Õ\ñ]r   )Ú__doc__r@   rG   r•   rK   rO   r1   r]   r‹   r   Úcollectionsr   rC   Úskimager   Únumpyr“   Úpytesseract.pytesseractr   r   r‰   Úimager   r	   r   r&   r›   r¦   © r   r   ú<module>r®      sT   ðñó Û Û Û Û Û 	Û Û 
Û Ý Û Ý Û ß JÛ ß (ò(ò\!ò~('óV]r   