U
    h                     @   sv   d dl Z d dlZd dlmZ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 edd	d
ZG dd deZdS )    N)ArgumentParser	Namespace   )MaskRenderer)
RecBuilder   )BaseInsightFaceCLICommandargsc                 C   s   t | j| jS N)RecAddMaskParamCommandinputoutputr	    r   K/tmp/pip-unpacked-wheel-5oclok7i/insightface/commands/rec_add_mask_param.py"rec_add_mask_param_command_factory   s     r   c                   @   s6   e Zd ZeedddZeedddZdd Zd	S )
r   )parserc                 C   s:   |  d}|jdtdd |jdtdd |jtd d S )Nzrec.addmaskparamr   z	input rec)typehelpr   zoutput rec, with mask param)func)
add_parseradd_argumentstrset_defaultsr   )r   _parserr   r   r   register_subcommand   s    
z*RecAddMaskParamCommand.register_subcommand)r   r   c                 C   s   || _ || _d S r   )_input_output)selfr   r   r   r   r   __init__   s    zRecAddMaskParamCommand.__init__c                 C   s(  t  }|jddd | j}tj|d}tj|d}tj||d}| j	}t
|d}|d}tj|\}	}
|	jdkrt|	jdkrttd	t|	jd }qtt| jj}ntt| jj}ddg}td
t| t|D ]\}}|d dkr
td| ||}tj|\}	}|	j}t|tjs@|d }tj| }|d d d d d d df }||}|d	  d	7  < |d kr|gdgd  }|d  d	7  < n6| |}|dg| }|dkrtdt|t|| t|dkst!|"|| q|#  td| j	d|d  d S )Nr   )   r    )Zctx_idZdet_sizez	train.recz	train.idxr)pathr   r   ztotal:i  zprocessing:g         g        zparam size:   zfinished onz	, failed:)$r   preparer   osr"   joinmxZrecordioZMXIndexedRecordIOr   r   Zread_idxunpackflaglenlabelnparrayrangeintlistimgreckeysprint	enumerate
isinstancenumbersNumberimageZimdecodeZasnumpyZbuild_paramsZencode_paramsAssertionErrorZ	add_imageclose)r   Ztoolroot_dirZpath_imgrecZpath_imgidxr3   Z	save_pathZwrecsheader_ZimgidxstatZiididximgr-   sampleZbgrparamsZwlabelZ
mask_labelr   r   r   run&   sP    








zRecAddMaskParamCommand.runN)	__name__
__module____qualname__staticmethodr   r   r   r   rF   r   r   r   r   r      s   	r   )r8   r'   argparser   r   Zmxnetr)   Znumpyr.   Zappr   Zdata.rec_builderr    r   r   r   r   r   r   r   <module>   s   