o
    ;ήc-                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZm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mZmZmZmZmZ d dlm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 ddl m!Z! ddl"m#Z# ddl$m%Z% e	& e	j'e( d ddZddZ)e)j*dde	j+ddde	,g ddddde	j+ddddd d!e	j+d"d#d$e-dd%d&e	j+d'd(d)e-dd*d&e	j+d+d,d)e-dd-d&e	j+d.d/ddd0d!e	j+d1d2ddd3d!e	j.d4ej/0 rdnd5e	d6d7e	j.d8ej/0 rdnd5e	jd9dd:d7d;e1d4ed8eddfd<d=Z2e)j*d>de	j+ddde	,g ddddde	j+ddddd d!e	j+d"d#d$e-dd%d&e	j+d'd(d)e-dd*d&e	j+d+d,d)e-dd-d&e	j+d.d/ddd0d!e	j+d1d2ddd3d!e	j+d?d@dAdddBdCe	j.d4e	j3de j3dAdddDdEe	j.d8e	j3dAe j3dAdddFdEd;e1d4e j3d8e j3dGe4ddf
dHdIZ5e)j*dJde	j+dKdLdMe-ddNd&e	j+dOdPdQe1ddRd&e	j+dSdTde-ddUd&dNe-dVe1dWe-ddfdXdYZ6dS )[    N)Enum)IOcastasyncify)DependsFastAPIFileFormQuery)CORSMiddleware)Response)tqdm)FileSystemEventFileSystemEventHandler)Observer   )_version)remove)BaseSession)new_sessionversion)r   returnc                   C   s   d S N r   r   r   0/tmp/pip-target-vg8gfxp4/lib/python/rembg/cli.pymain   s   r   zfor a file as input)helpz-mz--modelu2net)r   u2netpu2net_human_segu2net_cloth_segsiluetaTz
model name)defaulttypeshow_defaultshow_choicesr   z-az--alpha-mattingzuse alpha matting)is_flagr%   r   z-afz$--alpha-matting-foreground-threshold   ztrimap fg threshold)r#   r$   r%   r   z-abz$--alpha-matting-background-threshold
   ztrimap bg thresholdz-aez--alpha-matting-erode-sizez
erode sizez-omz--only-maskzoutput only the maskz-ppmz--post-process-maskzpost process the maskinput-rb)r#   r$   outputwb)lazymodelc                 K   s&   | t| fdt| i| d S )Nsession)writer   readr   )r0   r*   r-   kwargsr   r   r   i   s   &Br5   zfor a folder as inputz-wz--watchFzwatches a folder for changes)r#   r'   r%   r   )exists	path_type	file_okaydir_okayreadable)r$   )r6   r7   r8   r9   writablewatchc           
         s   t | dtjdd f fddt|d}s t|}|D ]
}| s,| q"r\t }G fdddt}| }	|j	|	|dd	 |
  z	 td qM|  |  w d S )N
each_inputr   c              
      s   zXt | }|d u rW d S |jddk rW d S | j d}|jd jddd | sS|	t
tt|  fdi  rVtd|   d|   W d S W d S W d S  tyo } zt| W Y d }~d S d }~ww )	Nimager   z.pngT)parentsexist_okr1   zprocessed: z -> )filetypeguessmimefindnamewith_suffixr?   mkdirr6   write_bytesr   bytesr   
read_bytesprintabsolute	Exception)r=   mimetypeeach_outpute)r4   r-   r1   r<   r   r   process   s2   

zp.<locals>.processz**/*c                       s"   e Zd Zdeddf fddZdS )zp.<locals>.EventHandlereventr   Nc                    s,   |j s|jdv s t|j d S d S d S )N)deletedclosed)is_directory
event_typepathlibPathsrc_path)selfrR   rQ   r   r   on_any_event   s   z$p.<locals>.EventHandler.on_any_event)__name__
__module____qualname__r   r\   r   r[   r   r   EventHandler   s    r`   F)	recursiveTr   )r   rW   rX   listglobr   is_dirr   r   schedulestarttimesleepstopjoin)
r0   r*   r-   r<   r4   inputsr=   observerr`   event_handlerr   )r4   r-   rQ   r1   r<   r   pd   s,   X

rn   zfor a http serverz-pz--porti  portz-lz--log_levelinfoz	log levelz-tz	--threadsznumber of worker threads	log_levelthreadsc           
         sL  i ddddddg}t ddt d	 d
dddddd|d}|jtddgdgdgd G dd dtt G  fddd}G  fddd}dtd|dtffdd|	d fd!d"}|j
d#dgd$d%d&td'd(d)t fd*td|ffd+d,}|jd#dgd-d.d&td'd/d)t fd0td|ffd1d2}	tj|d3| |d4 d S )5NzBackground RemovalzGEndpoints that perform background removal with different image sources.zGitHub Sourcez$https://github.com/danielgatis/rembg)descriptionurl)rE   rs   externalDocsRembgz8Rembg is a tool to remove images background. That is it.r   zDaniel Gatiszhttps://github.com/danielgatiszdanielgatis@gmail.com)rE   rt   emailzMIT Licensez:https://github.com/danielgatis/rembg/blob/main/LICENSE.txt)rE   rt   )titlers   r   contactlicense_infoopenapi_tagsT*)allow_credentialsallow_originsallow_methodsallow_headersc                   @   s    e Zd ZdZdZdZdZdZdS )zs.<locals>.ModelTyper   r   r    r!   r"   N)r]   r^   r_   r   r   r    r!   r"   r   r   r   r   	ModelType4  s    r   c                          e Zd Ze jddedddeddddd	ed
dddd	ed
dddedddedddfd dedededededefddZdS )zs.<locals>.CommonQueryParams"Model to use when processing imager#   rs   FEnable Alpha Mattingr(   r      $Alpha Matting (Foreground Threshold)r#   gelers   r)   $Alpha Matting (Background Threshold)$Alpha Matting (Erode Structure Size)r#   r   rs   	Only MaskPost Process Maskr0   aafabaeomppmc                 S   .   || _ || _|| _|| _|| _|| _|| _d S r   r0   r   r   r   r   r   r   rZ   r0   r   r   r   r   r   r   r   r   r   __init__<     
z%s.<locals>.CommonQueryParams.__init__N)r]   r^   r_   r   r   boolintr   r   r   r   r   CommonQueryParams;  L    


r   c                       r   )z s.<locals>.CommonQueryPostParamsr   r   Fr   r(   r   r   r   r   r)   r   r   r   r   r   r0   r   r   r   r   r   r   c                 S   r   r   r   r   r   r   r   r   ^  r   z)s.<locals>.CommonQueryPostParams.__init__N)r]   r^   r_   r
   r   r   r   r   r   r   r   r   CommonQueryPostParams]  r   r   contentcommonsr   c                    s@   t t|  |jjt|jj|j|j|j|j	|j
|jdddS )N)r1   alpha_matting"alpha_matting_foreground_threshold"alpha_matting_background_thresholdalpha_matting_erode_size	only_maskpost_process_maskz	image/png)
media_type)r   r   
setdefaultr0   valuer   r   r   r   r   r   r   )r   r   )sessionsr   r   im_without_bg  s   zs.<locals>.im_without_bgstartupc                     s:    d urddl m}  ddlm} |d|   d S d S )Nr   )CapacityLimiter)RunVar_default_thread_limiter)anyior   anyio.lowlevelr   set)r   r   )rr   r   r   r     s
   zs.<locals>.startup/zRemove from URLzCRemoves the background from an image obtained by retrieving an URL.)pathtagssummaryrs   .z*URL of the image that has to be processed.r   rt   c              
      s   t  4 I d H F}|| 4 I d H &}| I d H }t ||I d H W  d   I d H  W  d   I d H  S 1 I d H s?w   Y  W d   I d H  d S 1 I d H sUw   Y  d S r   )aiohttpClientSessiongetr3   r   )rt   r   r1   responsefiler   r   r   	get_index  s   .zs.<locals>.get_indexzRemove from StreamzDRemoves the background from an image sent within the request itself.z2Image file (byte stream) that has to be processed.r   c                    s   t  | |I d H S r   r   )r   r   r   r   r   
post_index  s   zs.<locals>.post_indexz0.0.0.0)hostro   rq   )r   r   get_versionsadd_middlewarer   strr   rI   r   on_eventr   r   r   postr	   uvicornrun)
ro   rq   rr   tags_metadataappr   r   r   r   r   r   )r   r   r   rr   r   s   s   

""	r   )r   N)7rW   sysrg   enumr   typingr   r   r   clickrA   r   asyncerr   fastapir   r   r	   r
   r   fastapi.middleware.corsr   starlette.responsesr   r   watchdog.eventsr   r   watchdog.observersr    r   bgr   session_baser   session_factoryr   groupversion_optionr   r   commandoptionChoicer   argumentstdinisattyr   r5   rX   r   rn   r   r   r   r   r   <module>   s   
.


 
>&