
    #
3j                     b    S SK r S SKJr  S SKrS SKrS SKJr  S SK	Jr
  SSKJr   " S S\5      rg)    N)List)Image   )BaseSessionc                      ^  \ rS rSrSrS\S\R                  4U 4S jjrS\	S\
\	   4S jr\S	 5       r\S
 5       rSrU =r$ )BenCustomSession   z@This is a class representing a custom session for the Ben model.
model_name	sess_optsc                 l   > UR                  S5      nUc  [        S5      e[        TU ]  " X/UQ70 UD6  g)z
Initialize a new BenCustomSession object.

Parameters:
    model_name (str): The name of the model.
    sess_opts: The session options.
    *args: Additional positional arguments.
    **kwargs: Additional keyword arguments.

model_pathNmodel_path is required)get
ValueErrorsuper__init__)selfr
   r   argskwargsr   	__class__s         S/home/wildlama/miniconda3/lib/python3.13/site-packages/rembg/sessions/ben_custom.pyr   BenCustomSession.__init__   s<     ZZ-
566@@@    imgreturnc           	         U R                   R                  SU R                  USSS5      5      nUS   SS2SSS2SS24   n[        R                  " U5      n[        R
                  " U5      nXW-
  Xg-
  -  n[        R                  " U5      n[        R                  " US-  R                  S5      SS	9nUR                  UR                  [        R                  R                  5      nU/$ )
al  
Predicts the mask image for the input image.

This method takes a PILImage object as input and returns a list of PILImage objects as output. It performs several image processing operations to generate the mask image.

Parameters:
    img (PILImage): The input image.

Returns:
    List[PILImage]: A list of PILImage objects representing the generated mask image.
N)      ?r   r   )      ?r   r   )   r   r      uint8L)mode)inner_sessionrun	normalizenpmaxminsqueezer   	fromarrayastyperesizesize
ResamplingLANCZOS)	r   r   r   r   ort_outspredmamimasks	            r   predictBenCustomSession.predict   s     %%))NN3,O

 {1aA:&VVD\VVD\	bg&zz$s
227;#F{{388U%5%5%=%=>vr   c                    UR                  S5      nUc  [        S5      e[        R                  R	                  [        R                  R                  U5      5      n[        R                  R	                  [        R                  R                  U R                  5       5      5      nUR                  U[        R                  -   5      (       d  XE:w  a  [        SU 35      eU$ )z
Download the model files.

Parameters:
    *args: Additional positional arguments.
    **kwargs: Additional keyword arguments.

Returns:
    str: The absolute path to the model files.
r   r   z0model_path must be within the models directory: )	r   r   ospathabspath
expanduser
u2net_home
startswithsep)clsr   r   r   abs_pathallowed_dirs         r   download_models BenCustomSession.download_models>   s     ZZ-
56677??277#5#5j#ABggoobgg&8&89I&JK"";#788X=TB;-P  r   c                     g)z
Get the name of the model.

Parameters:
    *args: Additional positional arguments.
    **kwargs: Additional keyword arguments.

Returns:
    str: The name of the model.

ben_custom )r@   r   r   s      r   nameBenCustomSession.nameV   s     r   rG   )__name__
__module____qualname____firstlineno____doc__strortSessionOptionsr   PILImager   r6   classmethodrC   rH   __static_attributes____classcell__)r   s   @r   r   r      sa    JA3 A33E3E A 8 h >  .  r   r   )r9   typingr   numpyr'   onnxruntimerP   PILr   	PIL.ImagerR   baser   r   rG   r   r   <module>r\      s(    	     ' V{ Vr   