Ë
    ÜÓ^j=*  ã                   óÈ   — d 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
mZmZ ddlmZmZmZmZmZmZ dd	lmZmZ dd
lmZmZ  G d„ ded¬«      Ze G d„ de«      «       ZdgZy)z!Image processor class for Nougat.é    N)Ú
functionalé   )ÚTorchvisionBackend)ÚBatchFeature)Úget_resize_output_image_sizeÚgroup_images_by_shapeÚreorder_images)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDict)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringc                   ó0   — e Zd ZU dZeed<   eed<   eed<   y)ÚNougatImageProcessorKwargsaÀ  
    do_crop_margin (`bool`, *optional*, defaults to `self.do_crop_margin`):
        Whether to crop the image margins.
    do_thumbnail (`bool`, *optional*, defaults to `self.do_thumbnail`):
        Whether to resize the image using thumbnail method.
    do_align_long_axis (`bool`, *optional*, defaults to `self.do_align_long_axis`):
        Whether to align the long axis of the image with the long axis of `size` by rotating by 90 degrees.
    Údo_crop_marginÚdo_thumbnailÚdo_align_long_axisN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚboolÚ__annotations__© ó    ú}/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/nougat/image_processing_nougat.pyr   r   )   s   … ñð ÓØÓØÔr    r   F)Útotalc            !       óº  ‡ — e Zd ZeZej                  ZeZ	e
ZdddœZdZdZdZdZdZdZdZdee   fˆ fd„Zededee   d	efˆ fd
„«       Z	 	 d'd„Zd„ Z	 d(ddded	dfd„Zddded	dfd„Zddded	dfd„Z ddded	dfd„Z!	 	 d)dddeddde"d	df
ˆ fd„Z#	 	 	 d*de$d   de"deddde"de%de"de%e$e%   z  dz  de%e$e%   z  dz  d e"dz  d!e"dz  d"e&e'z  dz  d#e"d$e"d%e"d	ef d&„Z(ˆ xZ)S )+ÚNougatImageProcessori€  i   ©ÚheightÚwidthTFÚkwargsc                 ó$   •— t        ‰| �  di |¤Ž y )Nr   )ÚsuperÚ__init__)Úselfr(   Ú	__class__s     €r!   r+   zNougatImageProcessor.__init__G   s   ø€ Ü‰ÑÑ"˜6Ó"r    ÚimagesÚreturnc                 ó$   •— t        ‰| �  |fi |¤ŽS )N)r*   Ú
preprocess)r,   r.   r(   r-   s      €r!   r1   zNougatImageProcessor.preprocessJ   s   ø€ ä‰wÑ! &Ñ3¨FÑ3Ð3r    Úimageútorch.Tensorc                 óp   — t        j                  |d¬«      }|dd…ddgf   }|j                  ddd«      }|S )zGThis is a reimplementation of a findNonZero function equivalent to cv2.F)Úas_tupleNé   é   éÿÿÿÿ)ÚtorchÚnonzeroÚreshape)r,   r2   Únon_zero_indicesÚidxvecs       r!   Úpython_find_non_zeroz)NougatImageProcessor.python_find_non_zeroN   s>   € ô !Ÿ=™=¨¸Ô?ÐØ!¢! a¨ V )Ñ,ˆØ—‘  A qÓ)ˆØˆr    c                 ó  — t        j                  |d¬«      j                  t         j                  «      }t        j                  |d¬«      j                  t         j                  «      }|d   |d   }}|d   |z
  dz   }|d   |z
  dz   }||||fS )zHThis is a reimplementation of a BoundingRect function equivalent to cv2.)r   r7   )Úaxisr   r7   )r9   ÚaminÚtoÚintÚamax)r,   ÚcoordinatesÚ
min_valuesÚ
max_valuesÚx_minÚy_minr'   r&   s           r!   Úpython_bounding_rectz)NougatImageProcessor.python_bounding_rectY   sˆ   € ô —Z‘Z °&Ô9×<Ñ<¼U¿Y¹YÓGˆ
Ü—Z‘Z °&Ô9×<Ñ<¼U¿Y¹YÓGˆ
à! !‘} j°¡mˆuˆØ˜1‘ Ñ%¨Ñ)ˆØ˜A‘ Ñ&¨Ñ*ˆØ�e˜U FÐ*Ð*r    Úgray_thresholdc                 ó2  — t        j                  |d¬«      }t        j                  |«      }t        j                  |«      }||k(  r|S ||z
  ||z
  z  dz  }||k  }| j                  |«      }| j                  |«      \  }}	}
}|dd…|	|	|z   …|||
z   …f   }|S )am  
        Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the
        threshold).

        Args:
            image (`torch.Tensor`):
                The image to be cropped.
            gray_threshold (`int`, *optional*, defaults to `200`)
                Value below which pixels are considered to be gray.
        r7   )Únum_output_channelséÿ   N)ÚtvFÚrgb_to_grayscaler9   ÚmaxÚminr>   rJ   )r,   r2   rK   ÚdataÚmax_valÚmin_valÚgrayÚcoordsrH   rI   r'   r&   s               r!   Úcrop_marginz NougatImageProcessor.crop_margind   s°   € ô ×#Ñ# E¸qÔAˆä—)‘)˜D“/ˆÜ—)‘)˜D“/ˆà�gÒØˆLØ�w‘ 7¨WÑ#4Ñ5¸Ñ;ˆØ�nÑ$ˆØ×*Ñ*¨4Ó0ˆØ&*×&?Ñ&?ÀÓ&GÑ#ˆˆu�e˜VØ’a˜ ¨¡Ð/°¸À¹Ð1FÐFÑGˆàˆr    Úsizec                 ó¶   — |j                   dd \  }}|j                  |j                  }}||k  r||kD  s
||kD  r||k  rt        j                  |dddg¬«      }|S )aL  
        Align the long axis of the image to the longest axis of the specified size.

        Args:
            image (`torch.Tensor`):
                The image to be aligned.
            size (`SizeDict`):
                The size to align the long axis to.
        Returns:
            `torch.Tensor`: The aligned image.
        éþÿÿÿNr   r7   r6   )Údims)Úshaper&   r'   r9   Úrot90)r,   r2   rY   Úinput_heightÚinput_widthÚoutput_heightÚoutput_widths          r!   Úalign_long_axisz$NougatImageProcessor.align_long_axis‚   sc   € ð  %*§K¡K°°Ð$4Ñ!ˆ�kØ&*§k¡k°4·:±:�|ˆà˜=Ò(¨[¸<Ò-GØ˜=Ò(¨[¸<Ò-Gä—K‘K  q°°1¨vÔ6ˆEàˆr    c                 ó`  — |j                   dd \  }}|j                  |j                  }}t        ||«      }t        ||«      }||k(  r||k(  r|S ||kD  rt	        ||z  |z  «      }n||kD  rt	        ||z  |z  «      }||f}	t        j                  ||	t
        j                  j                  ¬«      S )aW  
        Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any
        corresponding dimension of the specified size.

        Args:
            image (`torch.tensor`):
                The image to be resized.
            size (`SizeDict`):
                The size to resize the image to.
        r[   N)Úinterpolation)	r]   r&   r'   rR   rC   rO   ÚresizeÚInterpolationModeÚBICUBIC)
r,   r2   rY   r_   r`   ra   rb   r&   r'   Únew_sizes
             r!   Ú	thumbnailzNougatImageProcessor.thumbnailœ   s¸   € ð  %*§K¡K°°Ð$4Ñ!ˆ�kØ&*§k¡k°4·:±:�|ˆô �\ =Ó1ˆÜ�K Ó.ˆà�\Ò! e¨{Ò&:ØˆLà˜+Ò%Ü˜ fÑ,¨|Ñ;Ó<‰EØ˜<Ò'Ü˜¨Ñ-°Ñ;Ó<ˆFà˜E�?ˆä�z‰z˜% ¼×9NÑ9N×9VÑ9VÔWÐWr    c                 óÊ   — |j                   dd \  }}|j                  |j                  }}||z
  }||z
  }|dz  }	|dz  }
||	z
  }||
z
  }|
|	||f}t        j                  ||«      S )a
  
        Pads a batch of images to the specified size at the top, bottom, left and right.

        Args:
            image (`torch.tensor`):
                The image to be padded.
            size (`SizeDict`):
                The size to pad the image to.
        r[   Nr6   )r]   r&   r'   rO   Úpad)r,   r2   rY   r_   r`   ra   rb   Údelta_widthÚdelta_heightÚpad_topÚpad_leftÚ
pad_bottomÚ	pad_rightÚpaddings                 r!   Ú
pad_imageszNougatImageProcessor.pad_images¿   s…   € ð %*§K¡K°°Ð$4Ñ!ˆ�kØ&*§k¡k°4·:±:�|ˆà" [Ñ0ˆØ$ |Ñ3ˆà !Ñ#ˆØ !Ñ#ˆà! GÑ+ˆ
Ø (Ñ*ˆ	à˜W i°Ð<ˆÜ�w‰w�u˜gÓ&Ð&r    NÚresamplez7PILImageResampling | tvF.InterpolationMode | int | NoneÚ	antialiasc                 óÈ   •— t        |j                  |j                  «      }t        ||dt        j
                  ¬«      }t        ‰| �  |t        |d   |d   ¬«      f||dœ|¤ŽS )a·  
        Resize an image to `(size.height, size.width)`.

        Args:
            image (`torch.Tensor`):
                Image to resize.
            size (`SizeDict`):
                Size of the output image.
            resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*):
                Resampling filter to use when resizing the image.
        Returns:
            `torch.Tensor`: The resized image.
        F)rY   Údefault_to_squareÚinput_data_formatr   r7   r%   )ru   rv   )	rR   r&   r'   r   r   ÚFIRSTr*   rf   r   )	r,   r2   rY   ru   rv   r(   Úshortest_edgeri   r-   s	           €r!   rf   zNougatImageProcessor.resizeÜ   sm   ø€ ô* ˜DŸK™K¨¯©Ó4ˆä/Ø˜¸ÔRb×RhÑRhô
ˆô ‰w‰~Ø”8 8¨A¡;°h¸q±kÔBð
ØMUÐajñ
Øntñ
ð 	
r    Ú	do_resizeÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdo_padÚdisable_groupingÚreturn_tensorsr   r   r   c           	      ó,  — |r|D �cg c]  }| j                  |«      ‘Œ }}t        ||¬«      \  }}i }|j                  «       D ]_  \  }}|r| j                  ||¬«      }|r| j	                  |||¬«      }|r| j                  ||¬«      }|
r| j                  ||¬«      }|||<   Œa t        ||«      }t        ||¬«      \  }}i }|j                  «       D ]   \  }}| j                  ||||||	«      }|||<   Œ" t        ||«      }t        d|i|¬«      S c c}w )N)rƒ   )r2   rY   )r2   rY   ru   Úpixel_values)rS   Útensor_type)
rX   r   Úitemsrc   rf   rj   rt   r	   Úrescale_and_normalizer   )r,   r.   r|   rY   ru   r}   r~   r   r€   r�   r‚   rƒ   r„   r   r   r   r(   r2   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedr]   Ústacked_imagesÚresized_imagesÚprocessed_images_groupedÚprocessed_imagess                             r!   Ú_preprocessz NougatImageProcessor._preprocessú   sc  € ñ( Ø;AÖB°%�d×&Ñ& uÕ-ÐBˆFÐBô 0EÀVÐ^nÔ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ó%;ò 		;Ñ!ˆE�>Ù!Ø!%×!5Ñ!5¸NÐQUÐ!5Ó!V�ÙØ!%§¡°>ÈÐW_ Ó!`�ÙØ!%§¡°nÈ4 Ó!P�ÙØ!%§¡°~ÈD Ó!Q�Ø,:Ð" 5Ò)ð		;ô (Ð(>Ð@TÓUˆô 0EÀ^ÐfvÔ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ó%;ò 	=Ñ!ˆE�>à!×7Ñ7Ø 
¨N¸LÈ*ÐV_óˆNð /=Ð$ UÒ+ð	=ô *Ð*BÐDXÓYÐä .Ð2BÐ!CÐQ_Ô`Ð`ùò= Cs   ‡D)r2   r3   )éÈ   )NT)FTT)*r   r   r   r   Úvalid_kwargsr   ÚBILINEARru   r
   r€   r   r�   rY   r|   r   r   r   r‚   r}   r   r   r+   r   r   r   r1   r>   rJ   rC   rX   r   rc   rj   rt   r   rf   ÚlistÚfloatÚstrr   r‘   Ú__classcell__)r-   s   @r!   r$   r$   8   sb  ø„ à-€LØ!×*Ñ*€HØ&€JØ$€IØ CÑ(€DØ€IØ€LØ€LØÐØ€FØ€JØ€Nð# Ð(BÑ!Cõ #ð ð4 ð 4°vÐ>XÑ7Yð 4Ð^jô 4ó ð4ð	àó	ò	+ð "ñàðð ðð 
ó	ð<àðð ðð 
ó	ð4!Xàð!Xð ð!Xð 
ó	!XðF'àð'ð ð'ð 
ó	'ðB OSØñ
àð
ð ð
ð Lð	
ð
 ð
ð 
õ
ðX $)Ø!Ø#ñ!3aà�^Ñ$ð3að ð3að ð	3að
 Lð3að ð3að ð3að ð3að ˜D ™KÑ'¨$Ñ.ð3að ˜4 ™;Ñ&¨Ñ-ð3að �t‘ð3að  ™+ð3að ˜jÑ(¨4Ñ/ð3að !ð3að ð3að  ð!3að$ 
÷%3ar    r$   )r   r9   Útorchvision.transforms.v2r   rO   Úimage_processing_backendsr   Úimage_processing_utilsr   Úimage_transformsr   r   r	   Úimage_utilsr
   r   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r$   Ú__all__r   r    r!   ú<module>r¡      so   ðñ (ã Ý 7å ;Ý 2÷ñ ÷
÷ ÷ 5÷ô °Uõ ð ôtaÐ-ó taó ðtaðn "Ð
"�r    